
Why Your Agent Loops Forever (And How to Fix It)
A junior engineer's customer-support agent got stuck in infinite loops on 3% of production conversations, made 11,000 tool calls overnight, and burned the entire monthly API budget before anyone noticed. The seven causes are no termination condition, unclear completion criteria, tool errors that look like "try again," oscillation between two states, dependency on a tool that never returns the expected result, plans that can't replan, and prompts that reward thoroughness over efficiency. The LoopGuard class implements both defenses: a max_total_steps=15 budget and an exact-repeat detector that hashes tool_name + sorted args and flags >=3 identical calls, plus a 4-step oscillation detector catching A→B→A→B patterns. The seven common pitfalls include no step budget, relying on the model's self-control (just trust the model = 11,000 tool calls), and setting alerts only on agent loop rather than system-level call-pair frequency. Every loop incident is diagnostic gold—it pinpoints which prompt or design assumption broke.

