LangGraph workflows can revisit nodes while processing a request, but each run has a finite execution budget. A GraphRecursionError (GRAPH_RECURSION_LIMIT) means that the graph exhausted that budget before stopping; fixing it requires a reachable exit or enough steps for work that already has a known end.
The recursion_limit setting counts graph super-steps rather than Python function-call depth. Sequential nodes consume separate super-steps, while parallel nodes can share one. Changing Python's sys.setrecursionlimit() does not change this LangGraph limit.
A repeated node is not necessarily faulty if its state moves toward completion. The distinction is whether the routing can actually stop when that condition is met. An active Python environment with LangGraph installed is sufficient to inspect this behavior with a counter, without a model, API key, or external tool.
Steps to fix recursion limit errors in LangGraph:
- Create a file named recursion_demo.py with the counter state and node using your text editor.
- recursion_demo.py
from typing import TypedDict from langgraph.errors import GraphRecursionError from langgraph.graph import END, START, StateGraph class State(TypedDict): count: int target: int def count(state: State): return {"count": state["count"] + 1}
The node returns a state update. The intended completion condition is count >= target; the counter starts at zero.
- Append the cyclic graph definition below the count function in recursion_demo.py.
builder = StateGraph(State) builder.add_node("count", count) builder.add_edge(START, "count") builder.add_edge("count", "count") graph = builder.compile()
The unconditional self-edge deliberately omits an exit so the failure can be reproduced without running an application that has side effects.
- Append a bounded diagnostic run at the end of recursion_demo.py.
initial = {"count": 0, "target": 3} config = {"recursion_limit": 6} try: for update in graph.stream( initial, config=config, stream_mode="updates" ): print(update) except GraphRecursionError as error: print(f"GraphRecursionError: {str(error).splitlines()[0]}")
The limit of six sits far below the default on purpose, so the loop fails within a few steps instead of spending thousands of iterations. Tested with LangGraph 1.2.20, where the default is 10,007 super-steps; older releases use lower defaults, and the number in your own error shows the limit that applied.
Replaying an application graph can repeat tool calls, writes, or billable requests. A disposable input and disabled external actions are appropriate for diagnosis; the counter graph only changes in-memory state.
- Save recursion_demo.py.
- Run recursion_demo.py to identify the repeating node and its last updates.
$ python recursion_demo.py {'count': {'count': 1}} {'count': {'count': 2}} {'count': {'count': 3}} {'count': {'count': 4}} {'count': {'count': 5}} {'count': {'count': 6}} GraphRecursionError: Recursion limit of 6 reached without hitting a stop condition. You can increase the limit by setting the `recursion_limit` config key.For an application graph, the relevant updates are those used by its routing condition; a value that never changes can also prevent an exit. Streaming state may reveal private input, so only non-sensitive fields belong in shared logs.
- Compare the printed count values with the target of three to decide whether the run is bounded.
The count passes three and the same node keeps running, so this graph has no working exit and a larger limit would only fail later. A run is bounded only when its routing field moves toward the stop value on every pass and a route takes the exit at that value; otherwise the exit needs fixing.
When a run is already bounded, count the super-steps in one pass, multiply them by the maximum number of passes, add one, and pass the result at the top level of config. A three-node pass repeated 4,000 times needs {"recursion_limit": 12001}, which raises the 10,007 default; nested under configurable, the key is ignored. A result at or below the limit in your own error means the run used more super-steps than you counted, so recheck the exit before raising anything.
- Open recursion_demo.py in your text editor.
- Insert a routing function immediately before builder = StateGraph(State) in recursion_demo.py.
def route(state: State): return END if state["count"] >= state["target"] else "count"
The router sees the updated counter after the node runs. In an application, the exit condition must represent completed work, such as no remaining items, rather than merely suppressing the exception.
- Replace the unconditional self-edge in recursion_demo.py with the conditional route.
Old edge:
builder.add_edge("count", "count")
Replacement edge:
builder.add_conditional_edges("count", route)
An unconditional outgoing edge remains active even when another route returns END. Replacing the self-edge avoids scheduling another iteration alongside the exit. The existing builder.compile() call builds the changed topology when the script runs again.
- Replace the diagnostic try/except block in recursion_demo.py with an invocation that returns the final state.
Old block:
try: for update in graph.stream( initial, config=config, stream_mode="updates" ): print(update) except GraphRecursionError as error: print(f"GraphRecursionError: {str(error).splitlines()[0]}")
Replacement block:
result = graph.invoke(initial, config=config) print(result)
The existing initial and config assignments stay above this block, and the GraphRecursionError import can stay unused. With target set to three, the limit of six already allows the repaired graph to finish, because three passes of one super-step need a limit of four.
- Save recursion_demo.py.
- Rerun recursion_demo.py with the original input to confirm that the counter reaches its target and returns.
$ python recursion_demo.py {'count': 3, 'target': 3}A returned state with count equal to three shows that routing stopped at the intended boundary within the unchanged limit. In an application, the corresponding result must include the completed work expected for the original failing input; the absence of an exception alone is insufficient.