The anatomy of a runaway AI agent loop
A single unbounded while-loop cost an engineering team $47,000 overnight. The agent was performing API lookups in a research pipeline. One call failed with a transient HTTP 503. The agent retried. And retried. And retried — 30+ iterations, each one making a fresh, costly inference call — for eight hours while the engineer slept.
This pattern — the runaway loop — is the single most common failure mode in our 34-incident database. It's not a model quality problem. It's a control-surface problem.
How it happens, mechanically
A runaway loop requires three conditions: (1) the agent has access to a tool that costs money per call, (2) the tool can fail transiently (rate limits, network errors, 5xx), and (3) the agent's response to failure is "retry." Most agent frameworks retry by default. Most operators never configure a retry ceiling. The result: exponential cost as the loop compounds.
The DN42 scanning-agent incident ($6,500 AWS bill in 24 hours) is the hardware-provisioning variant: an agent that could spin up cloud resources did so, autonomously and repeatedly, each new resource incurring its own hourly charge. The operator hadn't set an IAM spend limit. The agent hadn't been told to stop.
Why the prompt can't fix it
Adding "don't loop forever" to the system prompt doesn't help. The model doesn't know it's looping — each retry is a fresh context that looks like progress. The cost is invisible to the agent. There's no weight in the forward pass for "and don't spend more than $X." The prompt is a suggestion; the firewall is a constraint.
The fix: velocity limits
A velocity rule is "allow at most N spend-calls per M minutes." Set it at 10 calls/minute for a typical agent. When the loop kicks in, the 11th call gets a BLOCKED decision with a reason. The agent reads the reason in the tool result and stops retrying. Overnight loss: zero. This is the one rule that would have prevented every overnight incident in our database — from the $4,200 Pinecone burn to the $47,000 token nightmare. Generate a ruleset with velocity limits →