Why it matters
An agent loop has no natural end: it continues while the model emits actions. Left alone it can loop, spend, or stop early with a confident but wrong answer. The engineering problem is to define termination as a property of the system — a success predicate you can evaluate, budgets you enforce, and a policy for what the caller receives when a budget wins.
Mental model
A stop condition is a referee's whistle, not the player's opinion that the game is over. The metaphor breaks because a referee sees the same field as the players, while a success predicate is often a proxy, so a run can stop correctly by the rule and still be wrong in fact.
How it works
Event · step 1 of 8
Turn 1 begins
The first prompt is 9,400 tokens. The model has no view of its budget; it only sees the transcript. Steps used: 1 of 12.
In state
- Model turn
Next: Act
- Steps
- 1/12
- Tokens used
- 9.4k
Figure. The run loops between model turn, act and observe, and every pass through the gate can end it. Success, budget exhaustion and no-progress are three of the five exit classes; escalation and failure are the other two. The two budget exits drawn are the step cap and the token cap, not wall-clock or spend.
What to notice. The token budget fires at the gate after an oversized observation: the next prompt would start at 61,200 against a 60,000 cap, and the harness stops before the call. Steps are still at 8 of 12, which is why the exits are independent. The 12-step and 60k-token budgets and the 3-repeat window are example policy values, not measurements.
Text version
A state machine with eight states. Model turn goes to Act on propose, Act goes to Observe on result, Observe goes to Gate on check, and Gate loops back to Model turn on continue. Gate also fans out to four terminals, each carrying its condition: Answered with predicate true, Step cap with steps = 12, Token cap with 61.2k greater than 60k, and No progress with 3 repeats. The trace walks Model turn, Act, Observe, Gate, Model turn, Act, Observe, then exits at Token cap. Counters show 8 of 12 steps and 61.2k tokens used against a 60k cap.
- Write the success predicate as a check over the end state or the output, not over the model's text.
- Set budgets per run for steps, tool calls, tokens, wall-clock and money, with one enforcement point.
- Detect no progress by hashing actions and observations across a window of steps.
- Define escalation: what a human receives when a budget or ambiguity ends the run.
- Return a structured outcome of success, partial, blocked or failed, with the reason and the artifacts.
- Test each stop path by forcing it, including with a tool that always errors.
Trade-offs
| Choice | Good for | Cost | Fails when |
|---|---|---|---|
| Model-declared completion | No extra checks | Stops early or never stops | Outputs are consequential |
| Success predicate | Objective termination | Needs a checkable end state | Success is qualitative |
| Step cap | Simple and universal | Ends good runs early and bad runs late | Tasks vary widely in length |
| No-progress detection | Catches loops cheaply | Can stop legitimate slow progress | The task needs many similar steps |
Good for
No extra checks
Cost
Stops early or never stops
Fails when
Outputs are consequential
- Outputs are consequential
- Success is qualitative
- Tasks vary widely in length
- The task needs many similar steps
Figure. Each choice with what it buys, what it costs, and the case where it loses.
What to notice. The fourth column is the decision: if the losing case describes your situation, the choice is wrong.
Text version
Four trade-offs: Model-declared completion is good for No extra checks, costs Stops early or never stops, and fails when Outputs are consequential. Success predicate is good for Objective termination, costs Needs a checkable end state, and fails when Success is qualitative. Step cap is good for Simple and universal, costs Ends good runs early and bad runs late, and fails when Tasks vary widely in length. No-progress detection is good for Catches loops cheaply, costs Can stop legitimate slow progress, and fails when The task needs many similar steps
Failure modes
- Silent truncation. The step cap fires and the caller receives a partial answer as if complete. Debug: assert every response carries an outcome field and count capped runs.
- Proxy success. The predicate passes while the user's goal is unmet. Debug: compare predicate passes with human labels on a sample.
- Budget bypass. A retry path skips the step counter and the run continues. Debug: audit every loop and recursion site for a shared counter increment.
In production
Track the distribution of stop reasons and the share of runs ending at a cap. A rising cap rate means the task or the tools changed, not that the budget needs raising.
Common misconceptions
People think: A maximum step count is enough.
Actually: It bounds cost but not correctness; without a success predicate every run ends the same way.
People think: Stopping early is safer than looping.
Actually: An early stop that returns a confident partial answer is a failure the user cannot see unless the outcome says so.
Interview angle
Strong answers separate the success predicate from the budgets, explain how a loop is detected, and describe what the caller gets on each stop path. Weak answers say the agent stops when it says it is finished.
Sources
- Anthropic, Building effective agents — explicit stopping conditions and bounded agent loops.
- OpenAI, Agents SDK documentation — turn limits and run termination as runtime settings.