By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
Every call inside a node can fail, and the right response depends on why. A timeout or a connection reset is transient: the same call may succeed on the next attempt. A validation error, a missing order, or a malformed model argument is permanent: retrying only repeats the bug, more slowly and more expensively. A refund that succeeded but whose response was lost is partial: repeating it without an idempotency key charges the customer twice. Fault tolerance is the discipline of telling those three apart before writing the retry.
LangGraph handles the first case with a per-node RetryPolicy and leaves the other two to your code. Attach the policy to the node that calls the flaky dependency; the runtime retries that node in place, so the rest of the graph does not re-run and the state is merged only when the node finally succeeds.
A RetryPolicy is attached with add_node(..., retry_policy=...). It applies to that node only, which is the point: the model node should not retry because a database lookup timed out, and the write node should not retry because the model hit a rate limit.
| Setting | What it controls | A sensible starting point |
|---|---|---|
max_attempts | Total attempts, including the first | 3 for an external API |
initial_interval | Seconds before the first retry | 0.2-1.0 s for a network call |
backoff_factor | Multiplier between waits | 2.0, so waits grow |
jitter | Random spread on the wait | Keep it on; clients should not retry in lockstep |
retry_on | Which exceptions count as transient | An explicit tuple, never a bare Exception |
from typing import TypedDict
from langgraph.graph import END, START, StateGraph
from langgraph.types import RetryPolicy
ATTEMPTS = {"count": 0}
class OrderState(TypedDict):
order_id: str
status: str
def fetch_status(state: OrderState) -> dict:
"""Pretend to call an upstream service that is briefly unavailable."""
ATTEMPTS["count"] += 1
if ATTEMPTS["count"] < 3:
raise ConnectionError("upstream timeout")
return {"status": "delivered"}
builder = StateGraph(OrderState)
builder.add_node(
"fetch_status",
fetch_status,
retry_policy=RetryPolicy(
max_attempts=3,
initial_interval=0.01,
backoff_factor=2.0,
jitter=False,
),
)
builder.add_edge(START, "fetch_status")
builder.add_edge("fetch_status", END)
graph = builder.compile()
result = graph.invoke({"order_id": "A17", "status": "unknown"})
print(result["status"])
print(ATTEMPTS["count"])Two properties keep a retry safe. First, the whole node body re-runs, so anything before the failing call happens again — keep retried nodes small and free of side effects. Second, retries are bounded: when max_attempts is exhausted the runtime raises the last exception, so a caller that should not see a 500 still needs a fallback.
A fallback edge is not a language feature; it is a design. The node catches the failure it expects, writes an error field into state, and the router sends the run to a fallback node that answers, escalates or queues the work. The failure then has a trace of its own instead of ending the process with a stack trace.
Catch narrowly. except Exception inside a node hides programming errors as if they were upstream outages; catch the exceptions your dependency actually raises, and let everything else fail loudly.
error string is enough for routing and for the trace.fallback when error is set, and the normal path otherwise.from typing import Literal, TypedDict
from langgraph.graph import END, START, StateGraph
class LookupState(TypedDict):
order_id: str
status: str | None
error: str | None
def lookup_status(state: LookupState) -> dict:
try:
# A real implementation calls the orders service here.
status = {"A17": "delivered"}[state["order_id"]]
except (KeyError, ConnectionError) as exc:
return {"error": str(exc), "status": None}
return {"status": status, "error": None}
def route_after_lookup(state: LookupState) -> Literal["fallback", "done"]:
return "fallback" if state["error"] else "done"
def fallback(state: LookupState) -> dict:
return {"status": "unavailable - a human will follow up"}
builder = StateGraph(LookupState)
builder.add_node("lookup_status", lookup_status)
builder.add_node("fallback", fallback)
builder.add_edge(START, "lookup_status")
builder.add_conditional_edges(
"lookup_status",
route_after_lookup,
{"fallback": "fallback", "done": END},
)
builder.add_edge("fallback", END)
graph = builder.compile()A retry re-runs a node. An interrupt() resume re-runs the node that called it. A time-travel replay re-runs every node after a checkpoint. All three are the same hazard: the work may happen more than once. Reads are naturally safe to repeat; writes are not. Idempotency is the property that repeating a write has the same effect as performing it once, and it is what makes the rest of the fault-tolerance story safe.
f"{thread_id}:refund:{order_id}" names the intent, not the attempt.interrupt() runs again on resume, so the write belongs after the decision, once.Key takeaways
Classify failures: transient errors belong in a RetryPolicy, permanent errors belong in state, partial writes need idempotency.
Attach RetryPolicy per node with bounded max_attempts and an explicit retry_on; the whole node body re-runs, so keep it side-effect free.
A fallback edge is a design: catch expected errors, write them into state, and route to a node that handles the degraded case.
Retries, interrupt resumes and time-travel replays all re-run work, so every write needs an idempotency key derived from the intent.
Log attempts, error classes and fallback rates; a retry storm is an incident, not a metric to admire.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.