Short answer
Put two checks outside the model and run them before every tool call: a step limit for the whole run, and a repeat check that stops the run when the same tool is called with the same arguments more than about 3 times. Telling the model to stop repeating itself is not enough, because the model is the part that is stuck.
Nothing was attacked and nothing crashed. The agent just never decided it was finished. This is one of the most common ways agents go wrong in production. Here is why it happens and how to stop it.
Why agents loop
An agent works in a cycle: think, call a tool, read the result, think again. It stops when the model decides the task is done. A loop starts when that decision never arrives. Common causes:
- A tool returns nothing useful. A search comes back empty, the model tries the same search again, gets the same empty result, and repeats.
- An error the model cannot fix. It retries the same failing call because the error message gives it nothing new to change.
- Two steps undo each other. The agent writes a value, a later step reads it as wrong, rewrites it, and so on.
- No exit condition. The prompt says "keep going until the task is complete", and the model's idea of complete never matches yours.
Asking the model to "stop if you are repeating yourself" in the prompt helps a little, but the model is the thing that is stuck. The stop has to live outside it.
The fix: limits that sit outside the agent
You need two checks that run before each tool call, in plain code:
- A step limit. No run gets more than N tool calls.
- A repeat check. The same tool with the same arguments, more than a few times, stops the run.
Here is a small version you can paste into any Python agent loop:
import json
class AgentStopped(Exception):
pass
class RunLimits:
def __init__(self, max_steps=50, max_repeats=3):
self.max_steps = max_steps
self.max_repeats = max_repeats
self.steps = 0
self.seen = {}
def check(self, tool_name, arguments):
self.steps += 1
if self.steps > self.max_steps:
raise AgentStopped(f"step limit reached ({self.max_steps})")
key = (tool_name, json.dumps(arguments, sort_keys=True))
self.seen[key] = self.seen.get(key, 0) + 1
if self.seen[key] > self.max_repeats:
raise AgentStopped(
f"{tool_name} called {self.seen[key]} times with the same arguments"
)Call limits.check(tool_name, arguments) right before every tool call, and catch AgentStopped where you run the agent.
We ran this on 20 November 2026. An agent calling search_orders with the same customer ID every time is stopped on the fourth call. An agent fetching 20 different pages is not stopped, because the arguments differ each time.
That second result matters. Do not stop on the tool name alone. Looping over a list of 50 customers calls the same tool 50 times and is perfectly healthy. Checking the arguments as well keeps you from killing good runs.
What this simple version does not cover
- Reworded repeats. If the model changes one word in the query each time, the arguments differ and the repeat check never fires. The step limit is your backstop.
- Spend. A loop of different but expensive calls hits no repeat limit. You also want a cost limit per run.
- Several agents. Each agent has its own counters, so a loop between two agents is invisible to both.
- Where it runs. The check only works if every code path calls it. In a big codebase that is easy to miss.
Doing it with SteerPlane
If you would rather not maintain this yourself, SteerPlane puts a step limit, a cost limit and a repeat check around the run. Install it with pip install steerplane:
from steerplane import SteerPlane
from steerplane.exceptions import LoopDetectedError
sp = SteerPlane(agent_id="support_agent")
try:
with sp.run(max_cost_usd=5.0, max_steps=100) as run:
for _ in range(100):
run.log_step("search_orders", tokens=300, cost=0.001)
except LoopDetectedError as e:
print(f"Stopped: {e}")On version 1.1.3 this stops after 8 identical calls and ends the run with a loop error you can catch and alert on.
One thing to know. SteerPlane's repeat check looks at the tool name, not the arguments. We tested it on 20 November 2026: a healthy run that calls fetch_page on 20 different pages is also stopped. If your agent legitimately repeats one tool over a list, turn the repeat check off for that run and rely on the step and cost limits:
with sp.run(max_cost_usd=5.0, max_steps=100, detect_loops=False) as run:
for page in pages:
run.log_step("fetch_page", tokens=100, cost=0.001)We are working on making the check argument-aware. Until then, this is the honest trade-off.
The repository has a quickstart that simulates a runaway agent, so you can watch it stop in a few seconds without an API key. Setup steps are on the setup page, and the wider picture is on the problem page.
A checklist before you ship an agent
- Every run has a maximum number of steps.
- Every run has a maximum cost.
- Identical repeated calls stop the run.
- Hitting a limit raises an error you can alert on, not a silent end.
- You have tested the limits with an agent that is meant to fail.