Name the three canonical agent stopping conditions and explain why cost cap is the hardest to implement correctly
What are the three canonical stopping conditions for an agent loop? For each, briefly describe how it is detected. Then explain why cost cap is harder to implement correctly than the other two.
The three stopping conditions are final-answer signal, step budget, and cost cap. Cost cap is hardest because next-step cost is unknown until after the call and compounds as context grows.
Imagine you give a helper a task and a few rules for stopping. First, they stop when they say they are finished. Second, they stop after a fixed number of tries, no matter what. Third, they stop once they have spent a set amount of money. The first two rules are easy to check. "Did they say done?" is a yes or no question. "Have they tried ten times?" is just counting. But the money rule is sneaky. You only learn the price of a try after it happens, not before. And each new try costs more than the last, because the helper carries a growing pile of notes along every time. So it is easy to blow past your budget on the very step that pushes you over, since you could not see the bill until the work was already done.
Detailed answer & concept explanation~8 min readEverything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
Name the three stops and their detection mechanisms, frame steps and cost as hard backstops layered under the model's own done signal, then spend most of the answer on why cost is hardest: post hoc knowledge of token cost, monotonic compounding of context, overshoot risk, and what to return on a forced stop.
Real products, models, and research that use this idea.
- LangGraph exposes a recursion_limit that caps loop iterations and raises a GraphRecursionError, the framework-level step budget acting as a hard backstop.
- The Anthropic SDK returns a usage field with input and output token counts on every call, letting a loop reconcile real cost after each step rather than guessing.
- OpenAI's Assistants and Responses APIs expose run-level step limits and token accounting so an agent can be cut off before it drains a project's spend.
- Cursor and Cline coding agents cap edit-test iterations and surface a token or request budget, halting the loop and returning partial diffs when the ceiling is hit.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you enforce a cost cap that almost never overshoots, even on a step that returns a huge tool output?
QWhy is verifying the model's final-answer signal safer than trusting it outright, and how would you verify it?
Don't say thisRed flags and common mistakes that signal junior thinking. Click to expand.
Red flags and common mistakes that signal junior thinking. Click to expand.
Trusting the model's final-answer signal as the only stop. A confused agent declares done prematurely or never, so hard backstops on steps and cost must run independently of what the model claims.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.
Same topic, related formats. Practice these next.