Loop Detection: When AI Agents Go in Circles

The first time I watched an AI agent enter an infinite loop, it cost me forty dollars in API calls in under three minutes. The agent was supposed to research a topic, synthesize findings, and produce a summary. Instead, it got stuck in a cycle: search, read the same result, decide it needed more information, search again with the same query. Over and over.
This is the runaway agent problem, and it is more common than most people realize.
Why Agents Loop
Language models do not have persistent memory of what they just did in the way humans do. Each step in an agent loop involves sending context to the model and receiving a decision. If the context does not clearly show that a previous attempt failed or that the current approach is not working, the model will happily try the same thing again.
Common loop patterns include:
Tool repetition: calling the same tool with identical or near-identical arguments. The agent searches for "best practices for X," gets results, decides they are not good enough, and searches for "best practices for X" again.
Circular reasoning: the agent alternates between two states. It writes code, tests it, finds an error, rewrites the same code with the same bug, tests again, and repeats.
Escalation spirals: the agent tries increasingly aggressive approaches to solve a problem, eventually doing something destructive, then trying to undo it, then re-doing it.
How Loop Detection Works
theGuard's loop detector uses a sliding-window algorithm. It maintains a buffer of recent actions, including tool calls, their arguments, and their results. At each step, it computes a similarity score between the current action and recent history.
The algorithm tracks three signals:
Exact repetition: the same tool called with byte-identical arguments. This is the simplest case and triggers fastest, typically after three to five repeats.
Fuzzy repetition: the same tool called with arguments that differ only in minor ways (whitespace, ordering, trivial word changes). This catches agents that slightly rephrase queries without changing intent.
Pattern cycles: sequences of actions that repeat as a group. The agent might alternate between tools A and B in a stable cycle. The detector looks for repeating subsequences in the action history.
When any signal exceeds a configurable threshold, the detector terminates the agent run and returns a structured error explaining what pattern it found.
Adaptive Thresholds
Fixed thresholds do not work well in practice. Some workflows legitimately involve repetition. A data processing agent might call the same transformation tool hundreds of times on different inputs. A strict "no repeats" policy would break it.
theGuard uses adaptive thresholds that consider the diversity of arguments. If the tool is the same but the inputs vary meaningfully, the counter resets. If the tool and inputs are both repetitive, the counter increments. The window size and sensitivity are configurable per-tool, so you can set tight limits on expensive API calls and loose limits on cheap local operations.
Practical Advice
If you are building agent systems, loop detection is not optional. It is the difference between a useful tool and an expensive mistake. Start with conservative thresholds and loosen them as you learn your agent's normal behavior patterns. Log everything. And always set a hard ceiling on total tool calls per run, even if your loop detection is good. Defense in depth matters.