1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson →
Retry policies: recovering from a failing node
A retry policy tells LangGraph to run a node again when it raises, so a step that fails on a flaky call can recover instead of ending the whole run.
Last updated: 27 Sep, 2026 · LangGraph 1.2
A node that calls a network or a database can fail for a moment and work on the next try. A RetryPolicy on that node re-runs it a few times before giving up.
A node that fails, then succeeds
This node raises the first two times and succeeds on the third, standing in for a call that is briefly unavailable.
attempts = {"n": 0}
def flaky(state):
attempts["n"] += 1
if attempts["n"] < 3:
raise ValueError("temporary failure")
return {"ok": f"succeeded on attempt {attempts['n']}"}Attaching a retry policy
Pass retry_policy to add_node. Name the errors to retry with retry_on; the default retries connection-style errors, not every exception.
b.add_node("flaky", flaky,
retry_policy=RetryPolicy(max_attempts=3, retry_on=ValueError))The node recovering on the third try
The whole program in one file.
from typing_extensions import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import RetryPolicy
class State(TypedDict):
ok: str
attempts = {"n": 0}
def flaky(state):
attempts["n"] += 1
if attempts["n"] < 3:
raise ValueError("temporary failure") # fails the first two times
return {"ok": f"succeeded on attempt {attempts['n']}"}
b = StateGraph(State)
b.add_node("flaky", flaky, retry_policy=RetryPolicy(max_attempts=3, retry_on=ValueError))
b.add_edge(START, "flaky")
b.add_edge("flaky", END)
graph = b.compile()
print(graph.invoke({"ok": ""})["ok"])Why the run survived
- The node raised
ValueErroron the first two runs. retry_on=ValueErrormatched, so LangGraph ran the node again, up tomax_attempts.- The third run returned normally, so the graph carried on.
No retry vs a retry policy
| No policy | RetryPolicy | |
|---|---|---|
| A raised error | Ends the run | Re-runs the node |
| Which errors | N/A | The ones in retry_on |
| Attempts | One | Up to max_attempts, with backoff |
When to add a retry
- A node that calls a model, an API or a database that can fail for a moment.
- A rate-limited call that works once the limit resets.
- Any step where a second attempt is likely to succeed.
Watch out. Retries help only transient errors. Retrying a bug or a bad input repeats the failure and slows the run; set
retry_on to the errors that are worth another try.Related
Try it yourself
- Lower
max_attemptsto 2 and watch the run fail instead. - Change
retry_onto a different error and see the ValueError no longer retried. - Add a second flaky node and give it its own policy.
Every expert started right here.