← All AI Agents & Tool Use modules

Module 1 — The Agent Loop

The core loop · hands-on · about 25 minutes.

A chatbot answers and stops. An agent keeps going: it takes an action, looks at what happened, and decides what to do next — over and over — until it reaches its goal. That repeating cycle is the single most important idea in this whole course. Strip away the buzzwords and every agent ever built is just a loop with four moves:

  1. Perceive — read the current situation (the goal, and whatever the last action revealed).
  2. Decide — choose the next action. This is where a real agent calls a language model; here it follows a simple rule.
  3. Act — actually do the thing (call a tool, make a guess, take a step).
  4. Observe — read the result of that action, and feed it back into the next "perceive."

The magic is the feedback: because each observation flows into the next decision, the agent adapts. It is not running a fixed script — it is reacting, step by step, to a world it cannot fully see ahead of time.

Drive the loop yourself

Below is the simplest possible agent. It has one goal — guess a hidden number between 1 and 100 — and one move: guess a number and get told "too high," "too low," or "correct." Watch how it perceives the range it knows, decides on the middle, acts, and observes the answer narrowing the range. Press Step to walk one perceive–decide–act–observe cycle at a time, or Run to let it finish.

This activity needs JavaScript. The lesson below still covers everything.

Why "middle of the range" is a smart decision

The agent never guesses wildly. By always choosing the middle of the range it still believes possible, every guess throws away half of what is left — so it homes in on any number from 1 to 100 in at most seven steps. That is the agent's tiny "brain": a rule that turns the current observation into the best next action. In a real agent, that rule is a language model. The loop around it is identical.

The same loop in code — read only, nothing to install
while not done:
    obs   = perceive(state)        # what do I know right now?
    action = decide(obs)           # the "brain" — an LLM in a real agent
    result = act(action)           # take the step / call the tool
    state  = observe(result, state) # fold the result back in, then loop

Every framework — LangChain, the OpenAI Assistants API, Claude's tool use — is a more elaborate version of these four lines. The decide step gets smarter; the loop stays exactly this shape.

AI anchor — this loop is every agent you have heard of When an AI coding assistant edits your files, when a research agent browses the web, when Claude uses a computer — they are all running this loop. Perceive the state, decide an action (that is the model call), act on the world, observe the result, repeat. The reason agents can do things a plain chatbot cannot is not a smarter brain — it is that the brain is wrapped in a loop that lets it act, see what happened, and try again.

Check your understanding

A few questions about the agent loop. You will get a score.

This activity needs JavaScript.

Why this matters next Our agent could only make guesses. A real agent's actions are tool calls — running a calculator, fetching data, checking a calendar. Module 2 gives the agent its first real tool and shows it choosing to use it instead of guessing.
One-sentence summary: an agent is a language model (or any decision rule) wrapped in a loop — perceive, decide, act, observe — where each observation feeds the next decision, so the agent adapts step by step toward a goal instead of answering in one shot.

Next: Calling Tools →