Strip away the mystique and an agent is a loop.

text
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

Short mental models: one idea, why it matters, what to do about it.

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.