Guides · ← All guides
How to Stop an AI Agent From Looping Forever (and Billing You for It)
By the Ingenious AI team · Published: November 14, 2026 · Reviewed: November 14, 2026 · 7 min read
The short answer
Stop loops with four independent brakes, because any one can be walked around: a hard iteration ceiling per run, a progress check that compares state between steps and halts when nothing changes, a fail-streak circuit breaker that trips after a set number of consecutive errors, and a spend cap that ends the run financially even if every other brake is misconfigured. Loops are the most common way agents hurt their owners — no attacker required — and they are the reason Rogue-Agent Defense ships fail-streak, velocity and cost-spike guards as first-class classes: the loop's signature is visible in rate and repetition long before the invoice makes it undeniable. The money side is in spend caps; this guide covers the mechanics.
Generate a free canary token → No signup, nothing stored: plant a tripwire where your agent works while you read — most controls in this guide start working the moment a canary, a policy check, or a spend cap is in place.
What is an AI agent loop? a run in which the agent repeatedly selects the same or equivalent actions without progress — retrying a failing step, re-reading its own output, or oscillating between two tools — consuming calls, time and budget until something external stops it.
Why agents loop: decisions, not bugs
A traditional program loops because its code says repeat. An agent loops because it keeps deciding to: the step failed, so try again with more context; the answer looks incomplete, so search once more; two tools each suggest the other as the next step, so oscillate. Each decision is locally reasonable — that is what makes loops robust against prompt-level fixes like try not to repeat yourself, and why the brakes below are mechanical, living outside the model's judgement.
Name the three common shapes. Retry loops: a failing step retried with escalating effort. Reread loops: the agent consuming its own growing output as new input, payload and cost swelling together. Ping-pong loops: two tools or two agents handing a task back and forth, each convinced the next step is the other's — the multi-agent variant that contagion patterns make faster.
Brake one and two: ceilings and progress checks
The iteration ceiling is the crudest and most necessary: a maximum step count per run, sized from the job's real distribution (the 98th percentile of step counts, not a round number from a tutorial). Framework defaults are starting points, not engineering; measure your runs. Hitting the ceiling should end the run in a held state with its work preserved — a loop stopped mid-task that loses the partial results teaches operators to raise ceilings until they stop meaning anything.
The progress check is the smarter brake: hash or summarise the run's state at intervals — documents gathered, records touched, the current best answer — and halt when consecutive checks show no change. Progress checks catch the loops ceilings miss (slow loops under the step cap) and spare the long legitimate jobs ceilings hurt (steady progress at step 400 is work; identical state at step 40 is a loop wearing a low number).
Brake three: the fail-streak breaker
Consecutive failures are the loop's loudest early signal and the cheapest to act on: after a set count — five is a sane default, tuned per job — the breaker trips, the tool or the run pauses, and a human or a backoff policy decides what happens next. The breaker matters because failure is where retry loops live, and because a failing external system plus an eager agent is a denial-of-wallet attack you perform on yourself.
Give different failures different counts: a flaky read API earns retries with backoff; a permission denial earns zero — the agent retrying a denied action is either confused or steered, and both readings argue for stopping immediately and logging the attempt pattern for the trail. A BLOCK verdict from policy should be the most terminal failure of all: re-proposing a blocked action in new words is loop behaviour with an adversarial accent.
Brake four: money and time walls
The final brakes do not care about the loop's shape at all: per-run spend caps and wall-clock limits, enforced at the boundary per the spend guide, end any run that outlives its budget of money or minutes. Their independence is the point — a loop that evades semantic detection by varying its actions still spends, and still takes time. Set the walls from the same distributions as the ceilings, and let a wall-trip page the owner with the run's story attached: steps, spend, last distinct actions, the state hash sequence that shows where progress stopped.
Walls also protect the fleet from itself: one tenant's loop consuming the shared API quota is every other tenant's outage. Per-agent and per-tenant rate budgets, from velocity practice, keep loops local — the agent equivalent of a circuit breaker per flat in a building.
After the loop: find the attractor
Every loop has an attractor — the condition the agent kept trying to satisfy: an unreachable success criterion, a tool whose success response the agent cannot recognise, a task definition with two contradictory completion tests. The post-loop review reads the trail for the attractor, because brakes without attractor fixes produce the same loop at a slower cadence next month.
Record loops in the incident system with the same neutrality as security events — signal, shape, brake that tripped, attractor, fix — per postmortem practice. Teams that treat loops as embarrassments hide them; teams that treat them as telemetry accumulate a map of their agents' failure geometry, which is one of the most valuable documents in a maturing fleet.
Five actions to take today
- Measure step distributions. Per job, real runs — then set iteration ceilings at the 98th percentile, not at a tutorial's round number.
- Add a progress check. State compared at intervals; no change across consecutive checks halts the run, work preserved.
- Wire the fail-streak breaker. Five consecutive failures trips a pause; policy denials trip it at one.
- Set independent money and time walls. Boundary-enforced per run, paging the owner with the run's story on a trip.
- Log every loop's attractor. Signal, shape, brake, cause, fix — filed as telemetry, reviewed monthly.
A worked example: the two-agent ping-pong at Alto Research
The following is a fictional example built from real threat classes and controls. The company and numbers are invented; the mechanics are not.
Alto Research, a 19-person consultancy, runs a pair of agents: a gatherer that collects sources and a drafter that writes briefs and may return them for more gathering. On a Friday evening a brief's completion criterion — include a primary source for the claim — meets a claim no primary source supports. The drafter returns the brief; the gatherer searches, finds nothing new, and returns the same sources; the drafter, unable to verify, returns it again. Each agent behaves impeccably. Together they loop 214 times over the weekend at low individual rates no per-agent ceiling was set to catch.
What catches it is the shared wall: the pair's combined per-brief spend budget trips at $212 on Sunday morning — against a normal brief cost of $9 — and the page carries the story: two agents, one brief, a state-hash sequence showing the same five sources circulating unchanged since Friday. The progress check that would have caught it at iteration 6 did not exist for cross-agent runs; it does now, hashing the shared brief state regardless of which agent holds it.
Alto's attractor fix is a sentence in the task contract: unverifiable claims are flagged in the brief, not blocking. The loop's total cost — $212 and a Monday review — is the cheap version; the same geometry against uncapped premium models was modelled in the review at roughly $6,800 for the weekend.
Frequently asked questions
Why do AI agents get stuck in loops?
Because looping is a decision pattern, not a code bug: the agent keeps choosing the locally reasonable next step — retry the failure with more effort, search once more, hand the task back — without a global view of progress. Prompt instructions not to repeat are weak against it; mechanical brakes outside the model's judgement are the fix.
What are the main types of agent loops?
Retry loops (a failing step retried with escalating effort), reread loops (the agent consuming its own growing output, swelling payload and cost), and ping-pong loops (two tools or two agents returning a task to each other). Each has a signature in rate, repetition or shared-state stagnation that mechanical guards can detect.
What is the best way to stop an agent loop?
Four independent brakes: a hard iteration ceiling sized from real run distributions, a progress check that halts when run state stops changing, a fail-streak circuit breaker (with policy denials tripping it immediately), and boundary-enforced spend and time walls that end any run regardless of shape. Independence matters — a loop that evades one brake meets another.
How do you detect a loop between two agents?
Meter the shared artefact, not just each agent: hash or summarise the joint state (the brief, the ticket, the plan) at intervals and halt when it stops changing across handoffs. Per-agent ceilings miss ping-pong loops because each agent's own rate looks modest; the shared progress check sees the stagnation directly.
Should a stopped loop lose its work?
No — the run should end in a held state with partial results preserved and the trail intact. Loops stopped destructively teach operators to raise ceilings until brakes mean nothing. The preserved run is also the diagnostic record that reveals the attractor: the unreachable criterion or unrecognised success the agent kept chasing.
Related guides
- AI Agent Spend Caps — the money wall, in full.
- AI Agent Velocity Anomalies — rate signatures of loops in the fleet view.
- When One Agent Infects the Rest — ping-pong's faster, hostile cousin.
- AI Agent Audit Trails: What to Log — the state sequences that prove stagnation.
- AI Agent Incident Postmortem Template — loops filed as telemetry, not embarrassment.
- How to Secure a LangChain Agent in Production — executor ceilings and their limits.
Generate a free canary token → No signup, nothing stored: plant a tripwire where your agent works, and let the next rogue action announce itself.