GraphRecursionError in LangGraph

GraphRecursionError is a runtime exception in LangGraph that fires when a StateGraph exceeds its recursion_limit (default: 25). It typically means your graph has a cycle where nodes keep calling each other without reaching an end state. This is LangGraph's safety net against infinite loops.

HIGH Severity

LangGraph Cycle Without Termination

Vulnerable
python
from langgraph.graph import StateGraph, END

graph = StateGraph(AgentState)
graph.add_node("agent", call_agent)
graph.add_node("tools", call_tools)

# Cycle: agent -> tools -> agent (no END route!)
graph.add_edge("agent", "tools")
graph.add_edge("tools", "agent")  # Always loops back

graph.set_entry_point("agent")
app = graph.compile()
# Raises GraphRecursionError after 25 iterations
Secure
python
from langgraph.graph import StateGraph, END

graph = StateGraph(AgentState)
graph.add_node("agent", call_agent)
graph.add_node("tools", call_tools)

# Conditional edge routes to END when done
graph.add_conditional_edges("agent", should_continue, {
    "continue": "tools",
    "end": END  # Exit path exists
})
graph.add_edge("tools", "agent")

graph.set_entry_point("agent")
app = graph.compile()

Frequently Asked Questions

What causes GraphRecursionError in LangGraph?

GraphRecursionError occurs when your StateGraph exceeds its recursion_limit. Common causes: (1) Missing conditional edges that route to END, (2) Cycles between nodes that never resolve, (3) An agent node that always decides to call another tool instead of finishing, (4) Incorrect state updates that prevent termination conditions from being met.

How do I fix GraphRecursionError?

Three approaches: (1) Increase recursion_limit if your graph legitimately needs more steps: graph.invoke(input, {"recursion_limit": 50}), (2) Add a conditional edge that routes to END based on a counter or condition, (3) Ensure your agent's "should_continue" function actually returns END when the task is complete.

What is the default recursion_limit in LangGraph?

The default recursion_limit in LangGraph is 25. You can override it per invocation: graph.invoke(input, {"recursion_limit": 50}). However, simply increasing the limit without fixing the underlying cycle is not recommended.

How can I detect GraphRecursionError before it happens in production?

Static analysis tools like Inkog analyze your StateGraph definition and identify cycles without proper termination conditions. This catches the issue at development time rather than in production where it causes errors and costs.

How Inkog Detects This

Inkog builds the control flow graph from your StateGraph definition. It detects cycles where no conditional edge routes to END, missing termination conditions, and nodes that always transition to the same next node without a path to completion.

bash
npx -y @inkog-io/cli scan .

Detect Graph Cycles

Scan your AI agents for vulnerabilities. Free for developers.

Start Free Scan