Topic 1 of 6 · 5 min read
Detecting repetitive loops in production traces
Goal: Identify the loop signature in an agent trace and choose the detector (repeat count, state hash or budget) that catches it earliest.
A loop is a run where the agent keeps taking steps but the world stops changing, and you catch it by measuring change, not by counting steps. Every agent loop is "call the model, run a tool, feed the result back". Nothing in that cycle guarantees it ends.
Check yourself · 1 of 3
Which signal shows that an agent is oscillating between two states?
Why it matters
A looping agent is expensive and quiet. It does not crash. It makes 300 plausible calls, burns tokens and may hit a rate limit on a real API. In a production trace it looks like activity.
The three signatures
- Exact repeat. The same tool with the same arguments, again and again. Example:
read_file("config.yaml")seven times in a row because the agent forgot it already has the content. - Oscillation. Two or more states alternate. The agent edits a file, a test fails, it reverts the edit, the other test fails, it re-applies the edit. Each call differs from its neighbour but the pattern repeats every second step.
- Retry storm. A call fails, and the agent retries with tiny changes (a different spelling, an extra flag) without changing its approach. Each retry is new text, so a simple duplicate check misses it.
How to detect them
- Repeat count. Hash the tool name plus normalised arguments. Alert when one hash appears 3 times in the last 10 steps. It catches signature 1 early and cheaply.
- State hash. Hash what matters after each step: the file tree, the test output, the page URL. If the state hash returns to a value seen before, the agent is oscillating. This catches signature 2, which repeat counting cannot.
- Progress metric. For retry storms, track error class instead of text. Five different calls that all return
403 Forbiddenare one failure, not five attempts. - Budget. A hard cap on steps, tokens and wall-clock time per run. It is the last line: it catches everything, but late. Set it so that a normal run uses well under half of it.
Before and after
Before: the only guard is max_steps = 200. A stuck run reaches step 200 and has cost the full budget.
After: the harness hashes each call, stops at the third identical call in a window, hashes the workspace after each step, and ends the run with a clear reason such as loop:state-revisited. The same stuck run ends near step 12, and the trace says why.
What goes wrong
- Budget as the only detector. You pay for every wasted step and the failure reads as "ran out of steps".
- Raw argument matching. The agent changes one character and the check never fires. Normalise first.
- Silent stop. Ending a run without recording the loop reason leaves no data to fix the prompt or tool.
Examples
Oscillation that repeat counting misses
[
{"step": 14, "tool": "edit_file", "args": {"path": "api.ts", "patch": "timeout=5"}, "result": "ok"},
{"step": 15, "tool": "run_tests", "result": "1 failed: test_slow_upload"},
{"step": 16, "tool": "edit_file", "args": {"path": "api.ts", "patch": "timeout=30"}, "result": "ok"},
{"step": 17, "tool": "run_tests", "result": "1 failed: test_fast_fail"},
{"step": 18, "tool": "edit_file", "args": {"path": "api.ts", "patch": "timeout=5"}, "result": "ok"}
]
No single call repeats three times in a row. The file contents at step 18 equal the contents at step 14, so a state hash flags it. The fix is a different approach, such as making the timeout a parameter, which the agent needs to be told to try.
A retry storm by error class
Five calls to fetch_invoice use ids INV-77, inv-77, INV77, 77 and INV-0077. All return 404. Grouping by tool plus status shows one failure repeated five times. The useful stop reason is repeated-error:404, and the next step should be asking the user which invoice they mean.
Key points
- A loop is a run where steps continue but the state stops changing, so measure change rather than step count.
- Exact repeats, oscillation between states and retry storms need different detectors: repeat count, state hash and error-class tracking.
- A hard budget on steps, tokens and time is the backstop, not the primary detector.
- Normalise tool arguments before hashing, or a one-character change hides a repeat.
- Record a specific stop reason such as loop:state-revisited so the trace explains the end of the run.
Common mistakes
- Relying on a max-steps limit as the only protection against loops.
- Comparing raw argument strings, so a retry that changes one character looks like new work.
- Counting only identical neighbouring calls and missing patterns that repeat every second or third step.
- Ending a stuck run without logging why, so nobody fixes the cause.
Check yourself · 2 of 3
An agent retries a failing call five times, changing the spelling of an id each time. All five return 404. Which detector fires earliest?
Check yourself · 3 of 3
A harness stops runs with the reason "max_steps reached". What is the main weakness of this when used as the only loop guard?
Practice items, not counted towards your mastery.
Start here
- Why Do Multi-Agent LLM Systems Fail? (opens in a new tab)PaperFree
A taxonomy of failures labelled across traces from several multi-agent frameworks, including task derailment and wrong verification.
- Recent frontier models are reward hacking (METR) (opens in a new tab)ArticleFree
Concrete cases of models tampering with scorers and tests instead of solving the task.
- Specification gaming: the flip side of AI ingenuity (Google DeepMind) (opens in a new tab)ArticleFree
Explains why an agent satisfies the letter of an objective, with many examples.
More for this topic
- Building effective agents (Anthropic) (opens in a new tab)ArticleFree
Advice on stopping conditions, ground truth from the environment and careful tool design.