Stopping, and the loop that will not
Stopping is the control logic that makes an agent loop terminate instead of repeatedly calling the model and tools forever. It defines explicit exits for success, exhausted budget, and repeated states, so the surrounding program, not the model’s judgement alone, decides when the task is done, unsafe, unaffordable, or stuck.
Agent loops exist because a single model call often cannot finish a real task. The agent may need to inspect files, call an API, read an error, revise a plan, and try again. The risk is that the same machinery also creates an unbounded retry loop. If progress is ambiguous, tools return partial failures, or the prompt keeps encouraging effort, the agent can continue long after a useful outcome is possible.
A bounded loop is usually structured like a production retry loop. Before or after each iteration, the program checks termination conditions. Success might be a passing test, a found record, a completed change, or explicit user approval. Budget checks count things such as steps, elapsed time, tokens, money, tool invocations, or dangerous operations. Repetition checks compare current state with prior states to catch repeated requests, identical errors, or no new information.
The trade-off is that stopping rules can end work too early as well as too late. Tight budgets reduce cost and blast radius, but may prevent recovery from transient failures. Loose budgets improve persistence, but make runaway behaviour more likely. Repetition detection is also approximate: two attempts can look similar while still being meaningfully different. The honest answer is usually policy-dependent, based on cost, risk, latency, and how reversible the agent’s actions are.
Engineers meet this in agent runners, orchestration frameworks, chat tool loops, background automation, and production incident controls. Practical implementations return explicit terminal states such as success, budget_exceeded, needs_human, or stuck_on_repetition, rather than just another model message. A common misunderstanding is that a better model will naturally stop at the right time. It may help, but termination is a property of the system around the model.
Common questions
- Why can an agent loop run forever?
- Because the model is repeatedly asked for the next plausible action, not for a proof that no further action is useful. If the environment keeps producing ambiguous observations, and the program has no hard exit conditions, each iteration can appear locally reasonable while the overall process makes no progress.
- What should count as success?
- Success should be an observable condition outside the model’s confidence alone. Examples include a passing test, a file that exists with expected contents, a tool returning the requested record, or a human approving the result. Natural-language self-assessment can be useful, but it is weakest when used as the only done condition.
- How do you detect repetition in practice?
- Store a compact record of each iteration: goal, chosen tool, inputs, important outputs, errors, and state changes. Then compare new iterations with recent ones. If the agent repeats the same tool call, receives the same failure, or changes nothing material, stop or ask for help instead of letting the loop continue.