Agent Loop Prevention
Preventing agents from infinite loops when tools fail — retry state counters in Pydantic state models, circuit breakers, Pydantic self-healing for malformed tool outputs, and exponential backoff strategies for agentic workflows.
Overview
Why agents loop
An agent loops when its LLM control logic keeps repeating the same action without making progress. The LLM expects that calling a tool again will produce a different result, but the tool is failing for a reason that will not change without outside help. The LLM has no built-in concept of futility.
Three distinct loop types
- Retry loop — Tool errors (API timeout, validation failure, malformed response); the LLM re-calls it expecting the error to clear — Failing — No retry counter in agent state, so it runs until a framework timeout
- Semantic loop — Tool succeeds, but the agent keeps re-calling it with slightly different parameters — Working — The result is correct but not what the agent expected
- Dependency loop — In a multi-agent system, A waits on B while B waits on A — Not called — Neither can progress without outside intervention
Three prevention layers
- 1. State-level counters — tool_retries dict in the TypedDict state — Retry loops, per tool
- 2. Framework limit — LangGraph recursion_limit config — Any runaway loop that gets past Layer 1
- 3. LLM-level instruction — "Call escalate_to_human if stuck" — Semantic loops before counters fire
> [!IMPORTANT] Calibrate recursion_limit from production traces of successful runs: complex multi-step research may legitimately visit 50+ nodes, so a limit of 25 would kill valid runs. Measure the 99th percentile step count and set the limit at 2-3x that value.
Pydantic self-healing
When a tool returns malformed JSON or a schema mismatch, a naive retry sends the same prompt and gets the same bad output. Self-healing sends a correction prompt that includes the specific ValidationError, so the LLM can fix the structure instead of repeating the mistake.
> [!TIP] Self-healing retries count against the per-tool retry limit, so self-healing cannot itself become an infinite loop.
Interview questions
- How do you prevent an agent from getting stuck in an infinite loop when an API tool fails?
- How would you distinguish between a genuine retry (transient failure) and a semantic loop (agent is stuck thinking)?
Sign up free to read the full topic
More MLOps & LLMOps topics
Start free on Cariara · Coding problems · System design studio