Skip to content
mentoor

Topic 4 of 5 · 4 min read

Runaway and stuck loop mechanics

Goal: Spot a repeating-call pattern in a run log and add a guard to the loop that detects and breaks it.

Stuck loops follow a few mechanical patterns, and each can be detected by comparing the last few tool calls, so the guard belongs in your loop code, not in a prompt. Telling the model "do not repeat yourself" helps a little. A counter helps every time.

Check yourself · 1 of 3

In an agent run, edit_file and run_tests alternate for six turns, undoing and redoing the same change. What is this pattern called?

Choose one answer

Why it matters

A model that sees the same observation tends to produce the same next action. Nothing in the loop forces it to change. Without a guard, a stuck run keeps spending until the turn or cost cap, and it can take repeated side effects with it: ten emails, ten tickets, ten file rewrites.

The three patterns

  1. Repeating the same call. search("refund policy") five times with identical arguments and similar results.
  2. Ping-pong. Two states alternate: edit the file, run the test, undo the edit, run the test, redo the edit. Each step looks reasonable. The cycle is only visible across turns.
  3. Retrying an error forever. The same failure, perhaps with tiny variations in the arguments, never leading to a new approach.

The guards

  • Fingerprint calls. Hash the tool name plus normalised arguments. Keep a count in a map or the last N turns.
  • Threshold, then intervene. At 3 identical calls, inject a message such as "You already called this with the same result. Try a different approach or report that you are blocked." At 5, stop with stuck.
  • Detect cycles. Compare the last 6 fingerprints. If the sequence A, B, A, B repeats, treat it as stuck.
  • Cap per-tool failures. Count consecutive errors per tool and stop or disable that tool.
const seen = new Map<string, number>();
function fingerprint(call: ToolCall) {
  return call.name + ":" + JSON.stringify(sortKeys(call.input));
}
for (const call of reply.toolCalls) {
  const key = fingerprint(call);
  const n = (seen.get(key) ?? 0) + 1;
  seen.set(key, n);
  if (n === 3) messages.push(nudge("Same call 3 times with the same result. Change approach."));
  if (n >= 5) return stop("stuck", { repeated: key });
}

Before and after

Weak log: read_file(a.ts) appears six times in ten turns, and nothing in the loop notices. Fixed: the third repeat injects a nudge, the model tries grep instead, and the run moves on. If it does not, the fifth repeat stops it with a reason.

What goes wrong

  • Exact match only. The agent changes one argument by a space or a timestamp and the fingerprint misses it. Normalise arguments and drop volatile fields.
  • Guards that block legitimate repeats. Polling a job status three times is fine. Count identical call plus identical result.
  • Nudges ignored. After the nudge, the loop still needs a hard stop.

Examples

Spotting ping-pong in a log

1 edit_file(parse.ts, add null check)
2 run_tests -> FAIL 2
3 edit_file(parse.ts, remove null check)
4 run_tests -> FAIL 3
5 edit_file(parse.ts, add null check)
6 run_tests -> FAIL 2

No call repeats back to back, so a simple repeat counter misses it. The pair of edits alternates, and the test result returns to the same failure count. A window comparison over the last six calls catches it.

A polling call that is allowed

get_job_status(job_42) -> running
get_job_status(job_42) -> running
get_job_status(job_42) -> done

The arguments are identical but the results change, so this is progress. Key the counter on call plus result, or allow a higher limit for tools marked as polling.

Key points

  • Stuck loops come in three shapes: identical repeated calls, two-state ping-pong and endless error retries.
  • Detect them in code by fingerprinting each tool call (name plus normalised arguments) and counting.
  • Intervene in stages: a nudge message at a low threshold, a hard stop at a higher one.
  • Count identical call plus identical result, so legitimate polling is not blocked.
  • Normalise arguments and drop volatile fields such as timestamps, or the fingerprint will miss near-repeats.

Common mistakes

  • Relying on a line in the prompt, such as 'do not repeat actions', instead of a counter in the loop.
  • Fingerprinting the raw argument string, so a changed timestamp hides a repeat.
  • Blocking all repeated calls, which breaks legitimate polling.
  • Adding a nudge but no hard stop after it.

Check yourself · 2 of 3

What does a call fingerprint usually consist of?

Choose one answer

Check yourself · 3 of 3

A guard counts identical calls and stops the run at 3. A job-status tool is polled with the same arguments until it returns done, and runs are being cut off. What is the best fix?

Choose one answer

Practice items, not counted towards your mastery.

Start here