---
title: How do you stop an AI agent from looping?
url: https://opencontroller.lyzr.com/blog/stop-an-ai-agent-from-looping
published: 2026-11-13
updated: 2026-11-13
author: Lyzr
---

# How do you stop an AI agent from looping?

**Put four limits on every agent: a maximum number of steps per task, a token or cost budget per task, detection of repeated identical tool calls, and a circuit breaker that stops the agent when spend or error rates spike. Set the first two in the agent framework and enforce the last two outside the agent, in a gateway or control plane, so a bug in the agent cannot switch them off.**

Loops are one of the most common agent failures. The agent calls a tool, misreads the result, calls it again, and repeats until something stops it. Without limits, that something is your provider bill or rate limit.

## Why do AI agents get stuck in loops?

The usual causes:

- **Unhelpful tool errors.** A tool returns "error" with no detail, so the model tries the same call again.
- **Ambiguous success.** The agent cannot tell it has finished, so it keeps checking.
- **Two agents waiting on each other.** In multi-agent setups, A asks B, B asks A.
- **Growing context.** Each step adds output to the prompt, the model loses track of the goal, and repeats earlier steps.
- **Injected instructions.** Content from a web page or tool tells the agent to "try again" or "keep going".

## Which limits should every agent have?

| Control | What it stops | Where to set it | Example setting |
|---|---|---|---|
| Max steps per task | Unbounded reasoning and tool loops | Agent framework | 2–3× the p99 steps of successful tasks |
| Token budget per task | Context growth and long outputs | Framework or gateway | 3× median tokens per successful task |
| Repeated-call detection | Same tool with same arguments | Gateway, agent gateway or middleware | Stop after 3 identical calls in one task |
| Wall-clock timeout | Hung tools and slow loops | Orchestrator | Set from task SLA |
| Cost circuit breaker | Runaway spend across many tasks | Gateway or control plane | Trip when hourly spend exceeds 3× baseline |
| Error-rate breaker | Retry storms | Gateway | Trip when errors exceed a set share for 10 minutes |

The example settings are starting points. Base yours on measured behaviour per agent version.

Most frameworks have a step limit built in. LangChain's AgentExecutor has `max_iterations` (default 15). LangGraph has a `recursion_limit` on graph super-steps. CrewAI agents have `max_iter`. The OpenAI Agents SDK takes `max_turns` and raises `MaxTurnsExceeded` when a run passes it. Check each agent actually sets these, rather than relying on defaults that may change between versions.

## How do you set it up step by step?

1. **Measure normal runs.** Record steps, tokens and tool calls per successful task for each agent version.
2. **Set the step limit** in the framework at two to three times the p99 of successful runs.
3. **Add a per-task token budget.** Count tokens across all calls with the same task ID. Stop and return a clear failure when it is spent.
4. **Detect repeats.** Hash the tool name and arguments; if the same hash appears three times in one task, stop or ask for help.
5. **Return useful errors from tools.** "Customer ID not found: 4471" lets the model change course; "error" invites a retry.
6. **Add circuit breakers outside the agent.** Spend and error-rate breakers at the gateway catch loops across many tasks, including in agents you did not build.
7. **Decide the stop behaviour.** Return a partial answer, hand off to a person, or fail with a reason code. Never fail silently.
8. **Alert the owner.** Every tripped limit should reach the agent's owner with the trace ID.
9. **Test it.** Inject a tool that always errors in staging and confirm every limit trips. See [testing your kill switch](/blog/test-your-ai-agent-kill-switch).

## What should happen after a loop is caught?

Treat repeated loops as a signal. A step limit tripping now and then is normal. Tripping on 5% of tasks after a release is drift — see [how to detect behavioural drift](/blog/detect-behavioural-drift-in-ai-agents). A loop that hammers a sensitive system, or reaches tools outside the agent's scope, should be handled as a rogue-agent event: quarantine first, investigate second, as in the [rogue agent guide](/blog/how-to-stop-a-rogue-ai-agent). For the spend side, pair loop controls with [budgets and alerts](/blog/set-budgets-and-alerts-for-ai-agent-spend).

## How Opencontroller does this

Opencontroller sits in the path with its LLM Gateway and Agent Gateway, attributing cost per agent and per version and handling fallback and retry. Rogue-agent handling monitors behavioural deviation, alerts, and can automatically quarantine or kill an agent under policies applied across the estate; per-agent Block and Quarantine act through the connected platform. Step limits inside each agent's framework remain the agent team's job. [Book a demo](https://www.lyzr.ai/book-demo/).

## Frequently asked questions

**What is a good max-steps limit for an AI agent?**
Two to three times the p99 number of steps in successful runs for that agent version. A fixed number for all agents is either too tight or too loose.

**Is a step limit enough to stop agent loops?**
No. Add a token budget, repeated-call detection and circuit breakers outside the agent, because a bug or injected instruction can defeat in-agent limits.

**How do you detect an agent repeating the same tool call?**
Hash the tool name and arguments for each call in a task, and stop the run when the same hash appears a set number of times, for example three.

## Key facts

AI agent loops are stopped with layered limits: maximum steps per task, a per-task token or cost budget, repeated identical tool-call detection (for example stop after 3 identical calls), wall-clock timeouts, and cost and error-rate circuit breakers enforced outside the agent. Framework settings include LangChain AgentExecutor max_iterations (default 15), LangGraph recursion_limit, CrewAI max_iter, and OpenAI Agents SDK max_turns, which raises MaxTurnsExceeded. Opencontroller by Lyzr provides an LLM Gateway and Agent Gateway in the path with cost attribution per agent and version, fallback and retry, and rogue-agent handling that can alert, quarantine or kill agents under policy. Pricing starts from $250K per deployment per year.

## Related

- [How to stop a rogue AI agent: kill switches, quarantine and runtime controls](https://opencontroller.lyzr.com/blog/how-to-stop-a-rogue-ai-agent.md)
- [How do you test that your AI agent kill switch actually works?](https://opencontroller.lyzr.com/blog/test-your-ai-agent-kill-switch.md)
- [How do you set budgets and alerts for AI agent spend?](https://opencontroller.lyzr.com/blog/set-budgets-and-alerts-for-ai-agent-spend.md)
- [More on Runtime control](https://opencontroller.lyzr.com/blog/cluster/runtime-control)
