Skip to content

Chapter 10 · Fault tolerance, retries, and idempotency ​

Simulate one transient failure and verify a node retry without looping forever on all errors. No real service is contacted.

Mechanics and design choices ​

RetryPolicy specifies attempt limits and retryable exceptions. Authentication and validation errors should not normally be retried as transient failures. A checkpoint restores graph state, but an external write may have succeeded while its response was lost; retry with the same idempotency key. Current docs also describe 1.2 node timeouts and error handlers; check installed versions before adopting them.

Example ​

python
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.types import RetryPolicy

class TemporaryFailure(Exception):
    pass

class State(TypedDict):
    result: str

attempts = {"count": 0}

def fetch_fixture(state: State):
    attempts["count"] += 1
    if attempts["count"] == 1:
        raise TemporaryFailure("Simulated transient failure")
    return {"result": "fixture loaded"}

builder = StateGraph(State)
builder.add_node("fetch", fetch_fixture, retry_policy=RetryPolicy(
    max_attempts=2, initial_interval=0.01,
    retry_on=TemporaryFailure,
))
builder.add_edge(START, "fetch")
builder.add_edge("fetch", END)
result = builder.compile().invoke({"result": ""})
assert attempts["count"] == 2
assert result["result"] == "fixture loaded"
print(result)

Run and verify ​

Expect success on the second attempt. attempts is a local test counter, not recoverable business state. Change the error to ValueError and expect immediate failure, verifying the retry policy is narrow.

Pitfalls ​

Avoid multiplicative retries across model SDK, tool client, and graph node. Set one overall budget and record attempts at each layer together with the final exception.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. Does max_attempts include the initial attempt?

2. Should authentication errors retry indefinitely?

3. Can checkpoints prevent every duplicate payment?

4. What is a risk of layered retries?

Hands-on exercises ​

  1. Always raise TemporaryFailure and assert only two attempts.
  2. Add a non-retryable error case and verify one attempt.
  3. Design a request_id-based mock write that records repeated requests once.

Official references ​

Verified on: 2026-09-17.