AI Engineering Judgment/AI Debugging & Troubleshooting
Intermediate10 min

Why Your Agent Loops

Infinite loops are the most common failure mode in AI agents. Learn the four root causes — ambiguous tools, context pollution, missing stop conditions, and reasoning spirals — with concrete detection and prevention strategies.

Quick Reference

  • Agent loops happen when the model calls the same tools repeatedly without making progress
  • Root cause 1: ambiguous tool descriptions — the model cannot distinguish which tool to use
  • Root cause 2: context pollution — accumulated tool results confuse the model's reasoning
  • Root cause 3: missing or unclear stopping conditions in the system prompt
  • Root cause 4: reasoning spirals — the model talks itself into circles trying to be thorough
  • Always add circuit breakers: max iterations, max tokens, max tool calls, and timeout limits

Recognizing Agent Loops

An agent loop looks like this: the user asks a question, the agent calls a search tool, gets results, decides the results are not good enough, calls the search tool again with a slightly different query, gets similar results, and repeats 15 times before hitting a token limit or timeout. The user sees a spinning indicator for 60 seconds and gets a mediocre response. This is the most common agent failure in production.

Loop vs Legitimate Iteration

Not all repeated tool calls are loops. An agent that searches, reads a document, searches again based on what it learned, and then answers is doing legitimate iterative research. The key difference: in a productive iteration, the context changes meaningfully between calls. In a loop, the agent is repeating the same actions with trivially different inputs.

Loop detection — track tool call patterns and alert on repetition