The AI agent loop explained, one lap at a time
By Christophe Paka · Facts checked on 23 October 2026
You ask a coding agent to fix a failing test. It runs the tests, reads two files, edits one, runs the tests again, and says it is done. Nobody told it that order. So who did? And how does a model run a command at all? Readers ask it in plain words: how does an agent call a tool?
The AI agent loop explained below answers both. An agent is a program where the AI model picks the next step. Get the loop wrong and you build an agent with no brakes: a bill with a chance of finishing. The figure shows the ring every agent runs, and the two ways out.

Use this diagram on your site
Free to share in your blog post, docs, course or slides, as long as the credit link stays. Paste the code below where the figure should go.
How to read this figure
Start at Goal in, on the left. The blue box on the left holds the goal: one task, stated in words. It enters once, at the start. Everything inside the dashed orange ring repeats. The label on the ring says it: one lap around this ring is one turn.
Box 1, context. Context is everything the model reads this turn: your instructions, the history so far, the tool schemas and the last observation. A tool schema is a short description of a tool: its name, what it does and its inputs. The context grows every turn, because each result is added to it.
Box 2, decision. The model sees the context and picks one of two things: call a tool, or answer and stop. If it answers with no tool call left, the arrow at the top leaves the ring for the green Final answer box. That is the first exit.
Box 3, tool call. A tool call is the model's written request to run one tool, such as
read_filewith a file path. Your code runs it. The model only asks; it never acts itself. That gap is what makes limits and permissions possible. See one tool call in four stages.Box 4, observation. The tool runs, then reports back. The observation is what came back: a result, an error, or silence. It passes to the green Stop condition box, then up the left side to context, so the model reads it on the next turn.
The stop condition and the second exit. The stop condition is checked before every new turn, not after the damage. Under every ceiling, the run goes round again. When a ceiling is hit, the arrow drops to The run ends here: max turns, spend cap, wall clock, an error, or a human stopping it.
Key takeaways
- Count the exits in your design; with no limit set, only the model decides when the run ends.
- Set your own caps on turns, spending and time, because a vendor's agent library (an SDK) may start with none.
- Treat every tool call as a request your code can check, log or refuse before it runs.
Worked example: one real run, lap by lap
Anthropic's agent docs follow one real run of the prompt "Fix the failing tests in auth.ts". Count the laps around the ring.
- Turn 1: the model asks for
npm test. Three tests fail. - Turn 2: it asks to read
auth.tsandauth.test.ts. The two files come back. - Turn 3: it edits
auth.ts, then runsnpm testagain. All three pass. - Turn 4: it writes text only, with no tool call: "Fixed the auth bug, all three tests pass now."
Result: Four laps: three with tool calls, then one with text only, which is the first exit. Nobody wrote "read the test file second". The model chose that during the run, after it saw the result of turn 1.
Questions readers ask
How does an agent "call" a tool?
It does not run anything. The model writes a small structured request, such as read_file with a path, and stops there. Your code reads that request, runs the tool, and adds the result to the context for the next turn. Some vendors call this function calling.
What happens when an AI agent gets stuck in a loop?
It keeps going round the ring until something stops it. If you set no limit, the only exit is the model deciding it is done. The stop condition is there for this case: a cap on turns, spending or time ends the run before the next lap. In the Claude Agent SDK, those caps start at no limit.
Can anyone explain loop engineering like I'm 5?
Loop engineering is readers' word for designing what goes around the agent loop: which tools it gets, which limits stop it, and what the model sees each turn. A harness, such as Claude Code, is the loop plus those tools, permissions and limits, already built for you.
Sources: The agent loop (Claude Agent SDK docs); Running agents (OpenAI Agents SDK); Building effective agents (Anthropic).
This figure is from AI Agents Made Crystal Clear, chapter 2.
The chapter builds the idea step by step, with recipes you can follow, a Try it to check yourself, and every source.
Get the book


