This is the mechanism separating an AI agent from a conversational assistant, and it is also the main line of spend in any agent project. Understanding the loop means understanding both what makes an agent capable and what makes a bill run away.
Definition
The agent loop is the cycle an agent repeats to advance towards its goal: it decides the next action, executes it, observes the result, then starts again taking into account what it just learned.
Each pass through the loop is a full call to the model, carrying all the accumulated history. That detail explains an agent's economics: the tenth pass costs far more than the first, because it carries everything that came before.
A loop is not a fixed sequence. Two runs of the same agent on two different files will not take the same number of passes nor the same actions. That is its strength, and what makes its cost hard to predict in advance.
What stops a loop
| Stopping condition | What it protects |
|---|---|
| The goal is reached | The normal case |
| Maximum number of passes | The bill |
| Budget reached | The bill, differently |
| No progress for N passes | Silent deadlock |
| Unrecoverable error | Pointless persistence |
The last four lines are guardrails, and all of them are needed. An agent with no pass ceiling facing a problem it cannot solve does not stop on its own: it rephrases, retries, rephrases again, burning Token on every pass.
The trap of the pass that learns nothing
The classic case: the agent calls a service that returns an error, it retries in exactly the same way, gets the same error, starts again. Three identical passes and no progress.
The remedy is a simple rule to state in the instruction: if two successive attempts produce the same result, change approach or stop. It sounds obvious, but without an explicit instruction a model tends to persist.
Monitor the number of passes, not only total cost. An agent that moved from three to four passes on average has changed behaviour, and you will see it on that indicator weeks before you see it on the invoice.
Frequently asked questions
How many passes for a common task?
Three to five for a well-scoped task with two or three tools, and often fewer with a Reasoning model that plans its steps better. Beyond ten on average, it usually signals that the goal is too broad or that the supplied tools are not enough.
What happens when the history gets too long?
The Context window saturates and the earliest passes fall out of range. The agent then loses track of what it already tried, and can repeat an action already done. One more reason to cap the number of passes.
Can a loop be made reproducible?
Not entirely, that is the nature of an agent. If you need the same result every time, a classic Workflow suits your case better.
How do you build a loop that stays under control?
By setting the guardrails before the first run, not after the first nasty surprise. Our Claude Code course covers that alongside the build itself, ceilings and logging included.