WRAITHERIX
Home / Blog
Test · 2026-12-05

How to Stop an AI Agent Stuck in a Loop Before It Burns Your Budget

WRAITHERIX team
A robot arm on a circuit board endlessly pulling the handle of a locked door, a ring of identical footprints around it and a growing stack of translucent coins beside it

An agent loop is a run where the model keeps making the same moves and never gets anywhere, while every round is billed. We built the worst case on purpose: a task that cannot succeed. Then we measured how long the agent kept trying and which guards stopped it.

The setup: a ~100-line Python agent with three tools, read_file, write_file and run_tests. The task is "the test in test_config.py fails, make it pass". The trap: every write returns error: permission denied (read-only file system), and the tests always fail with assert 10 == 30. The right behavior is to say "I can't do this". Requests went to https://api.wraitherix.com on a test key, model deepseek-v4-flash. Costs come from the account usage log.

Short version

Why loops are expensive

An agent asks for a tool call, your code runs it, the result goes back, the model thinks again. Two things make a stuck loop costly:

  1. History grows. Each step sends everything that came before. Step 20 costs much more than step 1.
  2. The agent does not know it is circling. An error arrives as plain text, and the model simply tries again.

The three setups

The repeat guard is a few lines:

key = name + json.dumps(args, sort_keys=True)
seen[key] = seen.get(key, 0) + 1
if seen[key] >= 3:
    stop = True

Results

No guard, four runs:

Run Steps How it ended Same call, times Cost
1 20 hit the cap 4 $0.0064
2 11 model wrote that it cannot do it 4 $0.0034
3 20 hit the cap 6 $0.0074
4 19 model stopped with no useful answer 6 $0.0069

In run 1 the model made 29 different calls over 20 steps while the outcome stayed the same. One call repeated up to 4 times, but the other calls differed in some way, for example in arguments. That is why a repeat guard alone does not catch everything.

Prompt tokens add up because history is resent: 26,179, 10,578, 30,033 and 24,889 in total across the four unguarded runs, against 2,915 to 4,256 with the repeat stop.

Repeat stop, three runs:

Run Steps Cost
1 6 $0.0012
2 5 $0.0006
3 5 $0.0005

Average: about $0.0060 with no guard, about $0.0008 with repeat stop. Roughly 8 times less.

Nudge, three runs: one was ended by the guard at the fifth repeat (7 steps, $0.0010); in the other two the model stopped on its own at steps 10 and 12 ($0.0018 and $0.0036). Three runs are not enough to say the hint made the model quit earlier: in two runs the agent still repeated a call 4 times and finished with no useful answer.

What did not work

We tried the same on gpt-5.4-mini. Its first reply was text that looked like a terminal command instead of a tool call, so the loop ended after one step (that request cost $0.0165). With no tool call there is nothing to compare, so we do not use this model in the results.

What to do in your own agent

  1. Set a step cap. Any number, but always. In our test, 20 steps of a three-tool task came to about 30,000 prompt tokens.
  2. Count repeated calls. Key = tool name plus arguments. End the loop on the third identical call.
  3. Do not rely on repeats alone. Models change arguments and keep circling. Also watch progress: if tool results have not changed in five steps (same errors), stop.
  4. Cap the budget per task. A step cap and a token cap together: if one breaks, the other still works.
  5. Show the stop to a human. When a guard fires, make the agent say where it is stuck and what it tried, instead of going silent.
  6. Test on a task that cannot succeed. That is exactly what we did. If your agent does not give up in a sensible number of steps there, it has no real guard.

What it cost us

The whole experiment, 11 runs and 116 API requests, cost $0.0495. The most expensive single run, on gpt-5.4-mini, was $0.0165. See the catalog for model prices; we do not copy a table because it changes.

The sums are small because the model is cheap and the task is short. On an expensive model with a large context the numbers will differ, and we did not measure that. What matters here is the ratio: the guard cut the bill several times over.

How we tested

2026-12-05, macOS 27.2.1, Python 3. Requests to /v1/chat/completions with tool definitions on api.wraitherix.com. The tools are simulated in the program: reads return fixed content, writes always fail, tests always fail. Four runs with no guard, three with repeat stop, three with the nudge. The sample is small: this illustrates the effect and is not a statistical study; your numbers will differ. Costs come from the account usage log, tokens from the request log.

One key for 400+ models

Pay as you go, below list price.

Get a keyCatalog