S4
Bounded Agent Loops: Five Budgets and Three Exits
Wrap every model–tool loop in external budgets, no-progress detection, and distinct terminal states.
- 01Explain why an agent loop is unbounded by default and where the external stop authority lives
- 02Enumerate the five layered budgets (iteration, tool, token, cost, time) and match each to the failure it catches first
- 03Distinguish the three termination states — success, exhaustion, failure — and why they must be reported separately
- 04Detect a no-progress loop by hashing tool-name+args or spotting state stagnation
The loop the model controls
topic 10An agent is a control loop: the model reasons → emits a tool call → the runtime executes it → the observation is fed back → repeat until the model signals it is done. The catch: the model decides when to stop. Your code must wrap that loop with an external step cap it cannot talk its way past.
A Reason-Act-Observe ring runs laps while four bars (iterations, tokens, cost, time) drain; the lap where any bar empties freezes and lights one of three outcomes — done, exhausted, or failed.
Text description
A Reason-Act-Observe ring runs laps while four bars (iterations, tokens, cost, time) drain; the lap where any bar empties freezes and lights one of three outcomes — done, exhausted, or failed.
Five layered budgets
Different failures bind on different limits, so you enforce all of them in the orchestrator:
- Iteration cap — stops oscillation / runaway loops.
- Tool-call cap — stops fan-out across too many calls.
- Token budget — stops a context-overflowing long crawl.
- Cost budget — protects the bill directly.
- Wall-clock timeout — stops a hung or slow tool.
Three exits, not one
Report success, exhaustion, or failure as distinct states. The dangerous middle case is neither-done-nor-progressing: detect it by hashing tool-name + args (halt on repeated identical calls) or watching for state stagnation.
Common MisconceptionA high max_iterations makes the agent safe.
CorrectionIteration count is only one budget; an agent can exhaust tokens, cost, or time long before it hits the step cap — you need all five.
Common MisconceptionThe model knows when to stop.
CorrectionStopping needs an explicit stop/done tool plus external no-progress detection — the model will happily repeat itself forever.
Production failure modes
- Oscillating / infinite tool loop calling the same tool with identical args.
- Context-window overflow mid-loop silently dropping earlier instructions.
- Premature 'looks done' termination returning a half-finished answer as success.
- Exhaustion reported as success because there is only one return state.
Framework defaults vary and drift between versions — for example LangChain’s max_iterations, the OpenAI Agents SDK’s max_turns, and LangGraph’s recursion_limit are different knobs with different defaults. Confirm them against your installed versions rather than trusting a remembered number.
References & deeper reading (2)
- Building Effective AgentsAnthropic Engineering
- Agents GuideOpenAI Developers
Retrieval Practice
Check one idea at a time
A ReAct agent keeps calling the same search tool with identical arguments and never finishes. What is the right fix?