The Agent Loop
In this article
- Trace an agent run step by step and say, for each step, what the agent saw, what it chose, what it did and what changed.
- Explain the difference between run state, the agent's own record of the run, and authoritative state, the system that holds the true value.
- Spot the step where an out-of-date fact in run state leads to a wrong action, and say what the runtime should read again.
- Terms defined here: run state, agent loop, state transition, agent run, agent trace, stale state.
Overview
A support agent refunds a customer twice for one order. Every call in the log succeeded, the model's reasoning reads sensibly, and nothing crashed. So where did it go wrong?
Almost always, the answer is in what the agent believed at the moment it acted. An agent here is a program in which the model picks the next action from what it observed. To debug one, you read its run as a list of numbered steps, each of which changes something, and you ask what each step knew.
By the end you can trace a run step by step, say what the agent saw, chose and did at each step, and point to the step where an out-of-date fact led to a wrong action.
The shop and its two tools
Take a made-up online shop with a support agent and two tools. get_order(id) reads an order from the shop's order system, and create_refund(id, amount) issues a refund. A tool call is the model's request to run one of them with arguments it fills in, and the tool result is what comes back. The agent can also do two things in plain words: ask a person, and reply to the customer.
A customer asks for a refund of order 1042. It was delivered 12 days ago, it is unused, and it cost 49.00 EUR. The shop refunds within 30 days of delivery, and create_refund will not pay more than 40.00 EUR without a person's approval. Those two checks live in the tool's code. The model can read the policy and conclude "this is allowed", but that is its reading; the tool decides.
Two places the order lives
The true record of order 1042 sits in the shop's order system. That is the authoritative state: the system that holds the correct current value, outside any model. The model never touches it. It cannot run anything itself: it emits a structured request, your code runs the tool, and the result flows back into the conversation. [1]
The program around the model keeps a second record. Run state is its record of this one run: the goal, the steps so far, and what each step returned. Real frameworks have the same thing under other names. In Google's Agent Development Kit (ADK), a session holds one conversation's current values and its event history, the list of everything that happened in it, and the OpenAI Agents SDK passes a RunState object to resume a paused run. [5,6]
The model reads neither record directly. On each step, the runtime builds what the model sees from run state plus the latest tool result. That bundle is the observation for the step. So the model's picture of order 1042 is only as fresh as the last time code called get_order and wrote the answer down.
One step, one change
Every turn of the agent loop has the same shape. Strands Agents spells it out: call the model, run a tool if it asks for one, call the model again with the result, and repeat until it gives a final reply. [7]
Seen this way, every turn of the loop is a state transition: a state before, an action, and a new state that the next turn reads. When the model asks for a tool, your code runs it and sends the result back together with the conversation so far. [1] [2]
Some actions only read. Others change the world: a side effect is a change a tool makes, not a read. create_refund has one when it succeeds; get_order never does.
The refund, step by step
An agent run is one execution from a starting input to its result, with every model call and tool call in between. [6] Its agent trace is the ordered record of what happened in it: inputs, model outputs, tool calls and their results.
Here is the trace of the shop's run, made up for this example. Read across each row. Rows 3, 5 and 7 are results arriving, so the model decides nothing in them.
| row | observation | decision | action | run state after | order system |
|---|---|---|---|---|---|
| 1 | customer asks for a refund of 1042 | - | goal recorded | goal: refund 1042 if eligible | not refunded |
| 2 | goal, no order data | look up the order | get_order(1042) | lookup sent | not refunded |
| 3 | delivered 12 days ago, unused, 49.00 EUR | - | result written back | order data, not refunded | not refunded |
| 4 | order data, 30-day policy | refund in full | create_refund(1042, 49.00) | refund requested | not refunded |
| 5 | "needs approval above 40.00 EUR" | - | result written back | refund blocked, needs approval | not refunded |
| 6 | blocked refund | ask a person | approval request; loop pauses | waiting for approval | not refunded |
| 7 | person approves | - | approval written back | approved | not refunded |
| 8 | approval, order data from row 3 | refund, then reply | create_refund(1042, 49.00) ok; reply | refunded, reply sent | refunded |
Look at the last column. The order system changes once, at row 8, the only row with a side effect that went through. Row 4 tried, but the tool refused, so the true state did not move. Row 8 sends the same call, and it goes through because, in this toy, the tool also receives the approval recorded at row 7, and an approved refund may pass 40.00 EUR.
What the next step can see
The model knows about row 5's refusal only because code wrote the result into run state. Drop that write and run state still says "refund requested"; the row 6 observation then shows no refusal, and the model might tell the customer the money is on its way. A side effect, or a refusal, reaches the next observation only if its result is written back.
Frameworks build this write-back in. In smolagents, each step runs the action and logs its result into the agent's memory, and the memory is turned into chat messages before the next model call. [4] ADK is strict about timing. Each step reports what it changed as an event, one record of something that happened, and the runner, the part of ADK that drives the loop, saves those changes to the session before the agent's code continues, so the next step reads the saved values. [5]
A refund during the pause
Now change one thing. While the run waits at row 6, a colleague who has the same customer on the phone refunds order 1042 directly in the order system. The approver does not know and approves at row 7. In this toy, a pause is not a stop: run state is kept while the run waits and comes back unchanged, because nothing in the loop calls get_order during the wait.
Before reading on, pick the step that goes wrong, and say why.
Here is the same run with the two records side by side, made up for this example:
| step | run state says | order system says | decision |
|---|---|---|---|
| 3 | not refunded | not refunded | - |
| 6 | not refunded, waiting | refunded by colleague | - |
| 7 | not refunded, approved | refunded | - |
| 8 | not refunded | refunded | `create_refund(1042, 49.00)` again |
The wrong row is 8. The reasoning is fine: given its observation, refunding is correct. The observation is the trouble. It was built from run state, and run state still held row 3's result, read before the colleague acted. The two records split at row 6, but nothing goes wrong until row 8, the first decision that reads the old value. The tool checks the window and the threshold, not earlier refunds, so the customer is paid twice.
That old value is stale state: a fact in run state whose authoritative value changed after it was read. In ADK, too, agent logic works from a snapshot: the session values current at the moment it picked up again. [5]
Read again after the pause
The fix is one cheap read. After any wait, before the next decision, read again every fact that may have changed. Here row 8 becomes get_order(1042); the result shows the order already refunded, and the model replies to the customer instead of paying again. A re-read narrows the gap; it does not close it. A colleague can still refund in the second between the read and the call, so when two people or programs can refund, create_refund itself must refuse a second refund of the same order.
Fresh data from a database is exactly what a tool is for: anything specific to your system has to be fetched by one. [1] ADK promises that agent code sees the session state its runner has committed. [5] That promise never reaches the shop's order system, so reading again is your design decision.
The habit to keep: treat every step as a transition, and make each one visible, with what the agent observed, what it did, and what changed in each record. If you cannot see a transition, you cannot tell which step acted on a stale fact.
Common mistakes
- Trusting a fact read before a wait. Symptom: a duplicate action after an approval or a slow reply. Fix: read again, after the wait, any fact the next decision depends on.
- Treating the loop as chat turns. Symptom: the log holds messages but no record of what each step changed, so nobody can say which fact was old. Fix: keep run state explicit and name, for each fact, the system it came from.
Sources
- How tool use works. Anthropic. Checked 24 Oct 2026.
- Tutorial: Build a tool-using agent. Anthropic. Checked 24 Oct 2026.
- How Claude Code works. Anthropic. Checked 24 Oct 2026.
- How do multi-step agents work? (smolagents). Hugging Face (smolagents). Checked 24 Oct 2026.
- Runtime Event Loop (ADK docs). Google (adk-docs). Checked 24 Oct 2026.
- Running agents (OpenAI Agents SDK docs). OpenAI. Checked 25 Oct 2026.
- Agent loop (Strands Agents). Strands Agents (AWS). Checked 18 Nov 2026.
Check yourself
Check yourself
Question 1 of 13
Where does the correct, current copy of a customer's purchase live?
Your first check counts towards your result.