12 · The agent loop
An agent is not a special model — it's an ordinary LLM put in a loop with memory and a stop condition. Reason → act → observe, repeat. The loop (and the guard that ends it) is the whole idea.
An agent = an LLM called repeatedly in a loop: each turn it reasons about the current state, takes an action, observes the result, and updates its memory — until a stop condition fires. A step guard caps the loop so it can't run forever.
Without this:
Without the loop mental model, agents look like a mysterious new capability. With it, you see they're plain control flow — and you know exactly where they can spin forever, lose track of state, or never decide to stop.
Until now every lesson has been a single call: build a prompt, send it, parse the answer, done. An agent breaks that one-shot pattern. Instead of asking the model for the whole answer at once, you put it in a loop and let it work toward a goal one step at a time.
The loop has three phases, repeated every turn:
- Reason — given the current state (the goal, plus everything observed so far), decide the next action. This is the model's job; the action might be "call this tool", "ask a follow-up", or "I'm done".
- Act — execute that action against the world (run the tool, query the database, write the file).
- Observe — take the result of the action and add it to memory (the running history the model sees next turn).
Then loop back to step 1 with the updated state. Three more pieces make this safe and finite:
- Memory — the accumulating record of actions and observations, so each turn the model can build on what it already learned.
- A stop condition — the model signals it's finished (e.g. emits a "final answer" action).
- A step guard — a hard
max_stepscap so a confused agent can't loop forever and burn your budget.
That is the entire concept. There is no special "agent model" — it's an ordinary next-token predictor wrapped in this loop. Below, the policy (the reasoning step) is a deterministic rule-based function so it runs offline; in production that one function is replaced by an LLM call.
Python (in browser)
The agent loop in full: reason→act→observe with a memory list and a max-steps guard. Swap the rule-based `policy` for an LLM call and you have a real agent.
Python runs entirely in your browser via Pyodide (~6 MB on first Run, cached after).
Notice what the loop did: it never planned the whole sequence up front. Each turn it looked only at the current state (the remaining amount), chose one action, observed the result, and reconsidered. That incremental, observe-and-adapt style is exactly why agents can handle tasks where you can't know the full plan in advance — the next step depends on what the previous one returned.
In production the only change is who plays policy. Below is the shape of a real agent loop where each turn is an LLM call: you send the conversation so far (the memory), the model replies, you append the reply, and you loop. Read it — the control flow is identical to the cell above.
Same loop, real model: the `messages` list is the memory, the LLM call is the policy, the step count is the guard. Agents are control flow, not magic.
What distinguishes an agent from an ordinary single LLM call?
- An agent = an LLM in a loop: reason → act → observe, repeated, with memory carried turn to turn.
- Two things make the loop finite and safe: a stop condition the model signals, and a hard `max_steps` guard.
- There's no special 'agent model' — only the same next-token predictor wrapped in control flow that decides one action at a time.
Every coding copilot, browser agent, and 'do-it-for-me' assistant is this loop; frameworks like LangChain, the OpenAI Agents SDK, and CrewAI all implement reason→act→observe with a step cap.
If you remove it: Without the loop you're limited to one-shot answers — the model can't gather information, react to results, or chain multiple steps toward a goal.