Agentic SRE Advanced By Samson Tanimawo, PhD Published Jul 2, 2026 5 min read

Loop Detection in Long-Running Agents

Agents repeat. Loops eat budget and produce nothing. The cheap detector that catches 90% of loops and the expensive one that catches the rest.

Cheap detection: state hashing

Hash the agent's recent state every step: last action, last tool call args, last hypothesis. If the same hash repeats, the agent is looping.

Repeats over the last 3-5 steps catch most production loops. The window is a tunable parameter.

False positives are rare because real progress moves the state. When they happen, the loop is benign (re-investigating with fresh data); the cost is one extra escalation.

Expensive detection: semantic similarity

Hash-based detection misses loops where the agent paraphrases the same action with slightly different args. Semantic similarity catches these.

Embed each step's state. Compute cosine similarity to recent steps. High similarity over multiple steps is a semantic loop.

The cost is one embedding call per step. For long-running agents (10+ steps), this is worth it. For short ones, hash detection is fine.

What to do when a loop is detected

First response: log the loop and break it. Force the agent to escalate to a human. Do not try to recover; the agent is stuck for a reason.

Second response: capture the loop state for offline analysis. The bug that caused the loop usually reveals itself in the captured state.

Third response: add an eval case that reproduces the loop. The next prompt change has to handle it.

Loop guards in budget terms

Treat loops as budget exhaustion. The agent has a per-run budget; loops eat budget without progress; the budget cap stops them.

Loops also have a per-loop budget: "if the same hash repeats 3 times within 10 steps, abort." This is faster than waiting for the run-level budget.

Tracking: per-run budget consumption with a per-step indicator. Loops show up as flat-line progress with growing token usage.

Eval cases for loop behaviour

Cases that intentionally try to make the agent loop: ambiguous goals, contradictory instructions, missing data. The agent should escalate, not loop.

Pass criterion: loop guard fires within N steps. The agent did not silently spin until the budget cap.

Add a case every time a real loop happens in production. The case is your defence against the same loop happening again.