LangGraphLangGraph 1.2 · Python 3.10+
0%
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.

python
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.

python
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.

Example
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 ValueError on the first two runs.
  • retry_on=ValueError matched, so LangGraph ran the node again, up to max_attempts.
  • The third run returned normally, so the graph carried on.

No retry vs a retry policy

No policyRetryPolicy
A raised errorEnds the runRe-runs the node
Which errorsN/AThe ones in retry_on
AttemptsOneUp 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.
Try it yourself
  • Lower max_attempts to 2 and watch the run fail instead.
  • Change retry_on to 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.