ALLMAcademy

S4

Bounded Agent Loops: Five Budgets and Three Exits

14 min2 optional deep dives

Wrap every model–tool loop in external budgets, no-progress detection, and distinct terminal states.

  1. 01Explain why an agent loop is unbounded by default and where the external stop authority lives
  2. 02Enumerate the five layered budgets (iteration, tool, token, cost, time) and match each to the failure it catches first
  3. 03Distinguish the three termination states — success, exhaustion, failure — and why they must be reported separately
  4. 04Detect a no-progress loop by hashing tool-name+args or spotting state stagnation
Narration — Bounded Agent Loops: Five Budgets and Three Exits
0:00 / 0:00

The loop the model controls

topic 10

An 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.

The agent loop on a budgetInteractive · 2D

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.

Loading diagram…
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)

Retrieval Practice

Check one idea at a time

Question 1 of 2

A ReAct agent keeps calling the same search tool with identical arguments and never finishes. What is the right fix?