AI agent loops: how to find and stop them
· 3 min read
An agent loop is an agent that keeps calling the model without making progress: it repeats the same tool call, gets the same result, and tries again. It does not crash and it does not raise an error. It just spends money until it hits a step limit, a timeout, or your budget.
Loops are one of the most expensive kinds of waste in agent applications, because each repeat resends the whole conversation so far. The tenth repeat costs more than the first.
Why agents loop
- A tool returns an error the model cannot fix. The model retries the same call with the same arguments, hoping for a different answer.
- A tool returns nothing useful. An empty search result, a "not found", a truncated page. The model searches again.
- The goal is unclear. The model cannot tell that it is done, so it keeps checking.
- History was trimmed. The agent drops old steps to save tokens, forgets it already tried something, and tries it again.
- Two tools bounce off each other. Tool A suggests calling tool B, which suggests calling tool A.
The three signs of a loop
You can detect a loop from call data alone, without reading prompts:
- The same request is sent again within one run.
- The same tool result comes back again: the newest input to the model is identical to an earlier one.
- The same tool calls are made again: identical tool names and arguments.
One repeat can be fine; a model sometimes needs to check twice. Four or more of the same thing in one run is a loop.
Guards that stop loops
Put these in every agent, whatever framework you use:
MAX_STEPS = 15
seen_calls: set[str] = set()
for step in range(MAX_STEPS):
reply = call_model(history)
if not reply.tool_calls:
break
key = repr([(c.name, c.arguments) for c in reply.tool_calls])
if key in seen_calls:
history.append(note("You already made this exact call. Try something else or answer."))
seen_calls.add(key)
history += run_tools(reply.tool_calls)
else:
log.warning("agent hit the step limit", extra={"run_id": run_id})- A step limit. Every agent needs one. Pick it from real runs: the longest normal run plus a margin.
- Duplicate-call detection. When the same tool call repeats, tell the model, or stop.
- Useful tool errors. "Not found: no order with id 123" lets the model change course; an empty string does not.
- A cost limit per run. Stop a run that has spent more than a set amount.
- One run id per task. Log it with every model call, so a whole run can be seen at once.
Measure before and after
A step limit stops the worst runs, but it does not tell you how much you were losing or whether the fix worked. Measure the share of spend in repeated steps per feature, fix the biggest one, and check that the share drops.
How Zexl finds loops
Zexl groups calls into runs automatically when each request continues the previous answer, or from zexl.run() when you mark them yourself. Within each run it compares salted hashes of the request, the newest input and the tool calls. Four or more identical ones make a loop, and every repeat after the first is counted as waste, measured, not estimated. The run detail page shows the run step by step with the repeats marked. See Features, runs and customers.
Create a free account, or read How to reduce your LLM API costs.
See where your own AI spend goes
One import in a Python app, or five environment variables for Claude Code. Prompts never leave your servers.