New here? Start this series at Day 31: Give an agent an outcome, a boundary and a stop condition first
- Day 31: Give an agent an outcome, a boundary and a stop condition first
- Day 32: Agent behavior is a loop, not a prompt
- Day 33: Plan before acting when actions cost money or carry risk
- Day 34: Tool access is where AI becomes operational risk
- Day 35: Agent memory is several stores, each with its own rules
- Day 36: Reflection is worth it only when it changes the next action
Strip away the mystique and an agent is a loop.
observe -> plan -> act -> check -> decide
^ |
|-------------------|One LLM call is a single shot that takes a prompt, returns text and stops. An agent keeps going. It looks at the world, chooses an action, takes it, looks at what changed, and revises. That cycle is what makes it an agent.
The loop is your debugging framework
This decomposition earns its keep the first time something goes wrong, because each station is a separate thing you can inspect:
- Wrong actions from sound reasoning? Look at what the agent can observe. Agents routinely act on a state snapshot that went stale three steps ago.
- Sensible plan, broken execution? The tool layer: definitions, schemas, permissions.
- Failures nobody noticed? The check station is missing. An "agent" that treats a tool's success response as proof the change happened fires an action and assumes it worked, because the happy path was the only path anyone tested.
- The same mistake, repeatedly? Results are not feeding back into the plan. Without the update step you have a script with retries wearing an agent's clothes.
All four are fixed by changing what the loop does.
Every loop needs three doors
The exits are done, blocked and out of budget, and each one has to be implemented and tested.
Once you have seen it, the failure mode is not subtle. An agent that cannot recognise it is stuck will keep paying for the privilege of being stuck. It will re-read the same file, re-run the same failing query, re-plan its way back to the identical dead end. Each individual step will look completely reasonable in the trace.
This matters for debugging, because runaway loops are rarely obviously broken at any single step. They are locally sensible and globally absurd, which is why the exit conditions have to be enforced from outside the loop rather than left to the agent's judgement.
What to actually do
Run the loop on a deliberately hard input and read the trace station by station, asking what evidence permitted each step, where it should have stopped and what it logged.
You are looking for a step that acted without checking and a point where it should have exited but had no door.
Fix the first by reading back the state that step was meant to change before the next action depends on it. Fix the second in the orchestrator, where the model cannot argue with it: count steps and elapsed time, and treat a repeated tool call, with the same arguments and the same result, as a sign the run is blocked. Set the step limit just above what successful runs need, and log every forced exit with its reason, because a limit set too tight kills runs that were about to finish.
Terms in this piece
About this explainer
- Format
- Explainer
- Level
- Practitioner
- Read
- 2 min read
- Published
- Jun 22, 2026
- Disciplines
Short mental models: one idea, why it matters, what to do about it.
More on these topics
Comparison · · 8 min read
Direct APIs, MCP tools and Code Mode: three ways an agent reaches your systems
How I choose between hand-written tools, MCP tool calls and Code Mode by where the plan runs, and why I built codemcp to test it.
Decision note · · 2 min read
We built our own TypeScript orchestrator, and stayed on it
Why our enterprise AI platform runs agents on a TypeScript execution layer we started almost two years ago, and why reaching near-parity with the frameworks argued for staying.
Comparison · · 1 min read
LangGraph vs writing the agent loop yourself
Two ways to write the same supervisor, an explicit state graph or a loop your own code owns on the model API. Compared on control flow, tracing, testing, checkpointing and what each makes hard.
Discussion