What conditions produce an infinite agent loop and what runtime safeguards prevent one?
Describe two distinct conditions that cause an agent loop to run indefinitely. Then name three runtime safeguards, explaining why each one alone is insufficient as the only protection.
Loops come from same-tool repetition and no-progress oscillation. Step caps, call dedup, and progress checks each miss a case, so production stacks all three plus a cost and time budget.
Imagine someone lost in a hedge maze. One way they get stuck is walking into the same dead end over and over, never learning it is a dead end. Another way is pacing back and forth between two spots, looking busy but going nowhere. A simple fix is a kitchen timer: when it rings, leave the maze. But the timer is dumb, it might ring while you are about to find the exit, or let you wander for an hour first. So you add more checks. One watches for the same wrong turn twice. One watches whether you are actually getting closer to the exit. No single check catches everything, because each one is blind to a different kind of stuck. Only stacking them all makes the maze safe to enter.
Detailed answer & concept explanation~7 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.
Open by separating the two loop conditions, same-tool repetition and multi-state oscillation. Then take each safeguard in turn, state what it catches and the exact case it misses, and converge on layered defense with cost and wall-clock budgets plus tracing as the production answer.
Real products, models, and research that use this idea.
- LangGraph exposes a recursion_limit on its StateGraph that hard-stops a cyclic agent after a fixed number of super-steps, the canonical step-budget backstop in production.
- Cursor and Cline coding agents detect when the same file edit or test command repeats with no new diff and inject a message forcing a different strategy before the budget runs out.
- Claude Opus 4.7 agent harnesses pair a max-turns cap with token and wall-clock budgets, since a slow browser tool can stay under the step cap while blowing the latency SLA.
- OpenAI's Agents SDK enforces a max_turns guardrail and surfaces a trace per run in its dashboard so a stuck trajectory can be replayed and diagnosed.
What an interviewer would ask next. Try answering before peeking at the approach.
QHow would you build a progress detector that is not fooled by observations that change but do not advance the goal?
QHow do you set a step budget that adapts to task difficulty instead of a fixed limit for every run?
QWhat changes about loop detection when one agent hands off to others in a multi-agent system?
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.
Naming only a step cap and stopping there. A step cap bounds the worst case but cannot tell a productive long run from a stuck short one, so it either fires too early or wastes a full budget on a hung agent.
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.