GraphRecursionError: Recursion limit of … reached without hitting a stop condition
Cause: Your LangGraph agent ran the maximum number of steps without reaching an end state, usually because it keeps calling the same tool or bouncing between nodes.
Copy-paste fix
Catch the error, and add a guard that stops when the agent repeats the same tool call with the same arguments.
import json
from collections import Counter
from langgraph.errors import GraphRecursionError
calls = Counter()
def guard_tool_call(name, args, max_repeats=2):
key = (name, json.dumps(args, sort_keys=True, default=str))
calls[key] += 1
if calls[key] > max_repeats:
raise RuntimeError(f"Tool loop: {name} called {calls[key]} times "
"with the same arguments")
try:
result = graph.invoke(inputs, {"recursion_limit": 40})
except GraphRecursionError:
# log the last steps and return a clear failure instead of retrying
raise
See what each extra agent step costs on TokenCenter
What the error means
LangGraph raises GraphRecursionError with the message “Recursion limit of N reached without hitting a stop condition. You can increase the limit by setting the recursion_limit config key.”
LangGraph's docs say this is often an infinite loop, though complex graphs can reach the limit naturally. Raising the limit hides a loop and multiplies the tokens it burns, so look for the loop first.
Checklist
- Make sure every path through the graph can reach END.
- Give tools clear success and failure results, so the model doesn't retry a tool that already answered.
- Stop on repeated identical tool calls.
- Cap total steps and total tokens per run, not just recursion depth.
- Log each step's node and tool name so a loop is easy to see.
- Raise recursion_limit only for graphs you know are long and loop-free.
Related
- APITimeoutError: Request timed out.
- Failure mode: Stale tool results
- Failure mode: Retry and cost multipliers
- ModelRaters list: Best for coding