AI Hints
Home›LangGraph Tutorial›Reliability
LangGraph · Lesson 79 of 120

Retry Policies

Trying again.

⏱ 2 min read▶ Video at 1:24:52🎓 Free
Watch this lesson in the LangGraph courseWatch on YouTube ↗Full LangGraph course

Network calls fail now and then. A model provider is busy, or a stock service times out. Often, simply trying again a moment later works. A retry policy does that for one node, automatically.

Retry Policies
From the video: Retry Policies
  • Calls fail sometimes
  • Trying again often works
  • Automatic retries

A node that retries

The flaky stock node fails twice, with a connection error, then succeeds. We add it with a retry policy, allowing three attempts, with a short wait between them. Run it. The first two attempts fail quietly, the third succeeds, and the graph finishes normally.

retry.pypython
from typing import TypedDict
from langgraph.graph import StateGraph, START
from langgraph.types import RetryPolicy

class State(TypedDict):
    stock: str

attempts = {"n": 0}
def check_stock(s):
    attempts["n"] += 1
    print("attempt", attempts["n"])
    if attempts["n"] < 3:
        raise ConnectionError("stock service busy")
    return {"stock": "Dune: 3 copies"}

b = StateGraph(State)
b.add_node("check_stock", check_stock, retry_policy=RetryPolicy(max_attempts=3, initial_interval=0.1))
b.add_edge(START, "check_stock")
print(b.compile().invoke({}))
Output
attempt 1
attempt 2
attempt 3
{'stock': 'Dune: 3 copies'}

Retry settings

Max attempts caps the tries. Initial interval is the first wait, and backoff factor makes each wait longer. Retry on chooses which errors to retry. By default, LangGraph retries likely temporary errors, like connection problems, and not bugs in your code.

keyvalue
max_attemptshow many tries
initial_intervalfirst wait
backoff_factorwaits grow
retry_onwhich errors

Summary

A retry policy on a node retries temporary failures automatically. Set the attempts and the waits. Next, handling errors that retries can't fix.

Key takeaways
  • RetryPolicy
  • max_attempts
  • Temporary errors only
Next up: Errors in Nodes — failing gracefully