What Is an Agent Loop? β Definition and How It Works
Learn what the agent loop is, what each iteration contains, why stop conditions matter more than the model, and how loops fail in production.
The agent loop is the cycle that turns a language model into an agent: observe the current state, choose an action, execute it, observe the result, and repeat until a stop condition is met.
1. What One Iteration Contains
state β model call β chosen action β execution β new observation β state
Each pass costs one inference. Everything else β validation, authorisation, execution β is ordinary application code between the model's decision and its consequence.
2. The Parts
| Part | Responsibility |
|---|---|
| State | Goal, history of actions, and latest observation |
| Decision | The model selects the next action |
| Validation | Arguments checked against the tool's schema |
| Authorisation | Whether this caller may perform this action |
| Execution | The tool runs; the application owns this entirely |
| Observation | The result, summarised into the context |
| Stop test | Success, failure, or budget exhaustion |
3. Stop Conditions Matter Most
A loop with no stop condition either runs forever or ends when a model decides it is finished β and models declare success optimistically.
Robust loops carry several stops:
- A success test the application evaluates, not the model.
- A step cap.
- A time and cost budget.
- A no-progress detector, ending runs that repeat the same action.
The no-progress case is the one most often missing, and the one that quietly consumes budget.
4. Managing Context Across Iterations
Every observation added to the state consumes context. Long loops overflow the window, and truncation silently removes information the agent appears to have.
Practical measures: summarise older steps rather than dropping them, store large results externally and pass a reference, and keep observations to the fields the next decision needs.
5. Why Long Loops Degrade
Per-step reliability multiplies. A step that is right 95% of the time is right about 60% of the time over ten steps and 36% over twenty.
The implication is architectural: prefer several short, verified loops over one long one, and check intermediate results rather than only the final answer.
6. Common Failure Modes
- Oscillation β two actions alternating without progress.
- Premature success β the model declares completion before the goal is met.
- Tool thrashing β repeated calls with slightly different arguments after a failure.
- Context overflow β early steps truncated away mid-run.
- Injected redirection β content read during the loop steering subsequent actions.
7. Making Loops Debuggable
Log every iteration: state summary, chosen action, arguments, result, and the stop test's verdict. Without that trace, a failed run is unexplainable, because the decisions were made from a context nobody recorded.
Traces also reveal the cheapest optimisations β usually a tool description that causes repeated wrong selections.
8. Summary
The agent loop is observe, decide, act, repeat, with the application owning execution, validation, and authorisation.
Its reliability is determined by stop conditions, context management, and loop length rather than by model quality β and its debuggability is determined entirely by whether each iteration was recorded.