If you are building multi-agent systems with LangGraph, you have probably seen this exact error:
GraphRecursionError: Recursion limit of 25 reached without hitting a stop condition.
Your first instinct is to check your retry logic. You assume the framework caught the failure, stopped the loop, and your API budget is safe.
Here is the brutal truth: By the time you see GraphRecursionError, the damage is already done.
Last year, I woke up to a $3,000 OpenAI bill. My infrastructure looked perfectly healthy—100% uptime, no 500 errors in the logs. But behind the scenes, Agent A and Agent B were stuck in a polite ping-pong loop. Hitting LangGraph's default recursion limit of 25 meant my LLM made 25 full API calls before it crashed. Depending on your context window, that one crash can easily cost you hundreds of dollars.
Here is why standard max_retries fails to stop it, and how to build a deterministic runtime circuit breaker that actually protects your budget.
The Technical Diagnosis: Why Retries Don't Work
If you are using frameworks like LangGraph, CrewAI, or AutoGen, you probably have max_retries configured. I did too.
But a retry limit only triggers when an agent throws an error (like a 500, a timeout, or a malformed JSON response).
An agent loop isn't an error. The LLM is successfully generating tokens. It is returning valid, perfectly formatted Pydantic output. LangGraph sees a successful handoff. The HTTP status is 200 OK.
Standard retry logic is completely blind to this. There are two patterns that will silently drain your budget:
The Repeated Loop:
A -> A -> A -> A. The model gets stuck on a single thought and repeats it indefinitely.The Cyclic Loop:
A -> B -> A -> B. Two agents get stuck validating each other's work forever.
If you are only monitoring HTTP codes or infrastructure uptime, you will never catch this. The agents are healthy; they are just burning your runway.
The Naive Fix (And Why It Fails)
When I realized what was happening, my first instinct was to write a manual loop counter inside the agent node.
# The Naive Counter
execution_count = 0
def guarded_node(state):
global execution_count
execution_count += 1
if execution_count > 3:
raise Exception("Loop detected!")
return agent.run(state)
This worked for a single-agent setup. But it fell apart the moment I scaled to a multi-agent swarm:
It doesn't catch cyclic loops. If Agent A calls Agent B, the counter keeps going up, but it's not catching the
A -> B -> Apattern, just the total number of executions.State resets break it. If an agent crashes and the graph resumes from a
PostgresSavercheckpoint, the Python counter resets to zero, and the loop starts over.It's a nightmare to maintain. If you have 10 nodes, you have to manually pass and manage this counter across every single edge in your graph.
The Robust Fix: A Runtime Circuit Breaker
To actually stop runaway spend, you need a deterministic check that happens at the handoff layer, before the next API token is generated.
You need to hash the payload.
Every time an agent prepares to pass a message to another agent, you intercept that payload, serialize it, and run it through a SHA-256 hash.
If you keep a sliding window of the last 3 hashes, you can instantly detect loops:
If
hash_1 == hash_2 == hash_3, you have a Repeated Loop.If
hash_1 == hash_3 and hash_2is different, you have a Cyclic Loop.
Instead of letting the agents keep talking, you throw a 422 Unprocessable Entity error back to the orchestrator, forcibly halting the session before it burns more tokens.
import hashlib
import json
def generate_payload_hash(payload: dict) -> str:
# Canonicalize the JSON to ensure consistent hashing
serialized = json.dumps(payload, sort_keys=True)
return hashlib.sha256(serialized.encode()).hexdigest()
By wrapping your LangGraph handoffs in a middleware that calls this hash function, you can compare the current hash against the previous two hashes stored in your session state. If a repeat is detected, you raise an exception to break the graph execution loop.
The Lesson: Monitor State, Not Uptime
In deterministic software, a 200 OK means everything is working. In probabilistic AI systems, a 200 OK just means the model successfully generated text. It doesn't mean the text was useful, and it certainly doesn't mean you aren't stuck in a loop.
Stop relying on HTTP status codes or GraphRecursionError crash limits to monitor your agent health. Build a deterministic state check at the handoff layer. It is the only way to guarantee that your agents aren't silently draining your API budget.
Top comments (0)