01
Recognize the five patterns
A loop often means the runtime did not record a meaningful state change.
The same visible symptom—an agent ‘thinking forever’—can have different causes. Track normalized tool name and arguments, recent observations, task state, and remaining budgets so operators can tell repetition from legitimate progress.
- Repeated call: the same tool and arguments are issued again without new evidence.
- Ambiguous error: a tool returns an unclear failure and the model retries the same action.
- Lost completion: completed work was only described in prose, so the planner recreates it.
- Context overflow: verbose tool output displaces the user goal and earlier evidence.
- Runaway retry: transient retries have no deadline, backoff, or aggregate cost cap.
Name and instrument the failure before changing the prompt; many loops are state, retry, or tool-contract bugs.
02
Detect no-progress behavior in the runtime
Do not make the model the only component responsible for deciding when to stop.
Define progress in domain terms: a record was found, an approval arrived, a state transition was committed, or a required output was produced. Compare recent normalized actions and observations; repeated errors or a run with no progress should trigger a deterministic branch. Apply independent budgets to steps, wall-clock time, tokens, and money, because a single turn limit does not bound a slow or expensive tool.
- Emit a LOOP_DETECTED or NO_PROGRESS event with the trace ID and last safe state.
- Use exponential backoff only for known transient errors; do not retry validation or permission failures.
- Summarize large observations while keeping a link to the full, access-controlled trace.
A stop condition is part of the agent’s runtime contract, not a failure of autonomy.
03
Recover visibly and turn incidents into tests
A safe recovery preserves what happened and gives the user an honest next step.
When a loop is detected, stop new side effects, retain the trajectory, and classify whether the cause was model selection, tool behavior, missing state, or a budget. Then ask a clarifying question, move to a deterministic fallback, request a human review, or return a partial result with the blocked step identified. Once resolved, minimize sensitive fields and add the case to a regression set.
- Keep the last successful action and the reason the run stopped.
- Never restart a write workflow from scratch without checking idempotency and persisted state.
- Re-run regression cases when prompts, tools, models, or framework versions change.
Mature systems do not claim zero failures; they make failures bounded, diagnosable, and less likely to recur.
Primary sources
First-party repositories and documentation used for this chapter. Check the upstream README and release notes before implementation.
- OpenAI Agents SDK for Python — Run tracing and agent workflow examples
- Promptfoo — Regression evaluation and red-team testing
- Microsoft Agent Framework — Workflow, checkpointing, and human-in-the-loop samples