1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
51 small wins to finish your pathNext question →
How does an agent know when to stop?
30-second answerSay your answer out loud first, then reveal.
Layers of stopping
- Natural termination: the model returns a response with no tool call, or calls a dedicated
final_answer/submittool. This is the cleanest pattern because the final output is structured. - Hard limits (always in code, never just in the prompt):
–max_iterations(e.g. 15–25 for most tasks)
– token / cost budget per run
– wall-clock timeout - Loop detection: the same tool with the same arguments N times, or no new information over K steps → break out or inject a hint.
- Success verification: for some tasks the environment can confirm success, e.g. tests pass, the record exists in the DB. Prefer this over the model's own claim that it's done.
- Graceful failure: when a limit is hit, return a useful partial result and explanation, not an exception.
Prompt-side support: tell the agent what "done" means and that it's acceptable to stop and say "I couldn't find X; here's what I tried." Many agents loop because they think giving up is forbidden.
Common mistakes
- Relying only on the model to stop.
- Raising an error at max iterations and losing all the work done so far.
Follow-ups to expect
- Scenario: the agent keeps calling the same tool. (See Q18.)
Related
You understood something today that you didn't yesterday.