All Agent Engineering

LangGraph node retry: exception scope and attempt limits

Set RetryPolicy for a read-only teaching node and distinguish default filtering, an explicit policy and terminal failure.

AINavWritten / Updated
On this page

At a glance

LangGraph can retry nodes that encounter transient API, database or model failures. This task limits retries to ConnectionError in a teaching node, then checks the successful state. It makes no payment or write and does not treat retries as a substitute for business idempotency.

Runtime and applicability

Article type
Engineering practice
Verified
Applies to
  • LangGraph Use the cited documentation and version scope. Not tested.

Execution locations and responsibilities

  • Node registration and invocation in a LangGraph Python graph Choose retryable exceptions and attempt limits, then inspect node failure and graph termination.
Engineering workflow order
Engineering workflow order1. Configure one node → 2. Run a graph without external side effects → 3. Check success and exhausted retries. Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.1. Configure one node2. Run a graph without external side effects3. Check success and exhausted retries

Arrows show the order of checks in the guide. Follow each step’s prerequisites, branches and stop conditions; unexecuted steps are not completed work.

When to use

  • An existing read-only node has identifiable transient failures that need bounded retries.

When not to use

  • Do not apply this directly to permanent input errors or external writes without an idempotency policy.

Identify retryable exceptions first

The documented default retry_on uses default_retry_on and excludes several exception classes, including ValueError, TypeError and OSError. Errors from common HTTP libraries are retried by default only for 5xx responses. Python ConnectionError belongs to the OSError hierarchy, so this example explicitly supplies retry_on=ConnectionError rather than expecting a default policy to cover every network failure.

1. Configure one node

Pass RetryPolicy through retry_policy when adding the node. max_attempts=3 is the example’s chosen limit, not a recommendation for every node. Separate transient exceptions from permanent invalid input before choosing how many attempts the application can tolerate.

2. Run a graph without external side effects

Use an existing LangGraph Python environment, save the code as node_retry.py and run python node_retry.py. The first call increments a local counter and raises the teaching error; the second returns sample-ready. The global counter belongs only to this single-process demonstration. It is not persisted state or a reliable multi-user attempt ledger.

Example: bounded retries after one transient failure
python
from typing_extensions import TypedDict
from langgraph.graph import START, END, StateGraph
from langgraph.types import RetryPolicy


class State(TypedDict):
    value: str


attempts = 0


def read_sample(state: State):
    global attempts
    attempts += 1
    if attempts == 1:
        raise ConnectionError("Demonstration transient failure")
    return {"value": "sample-ready"}


builder = StateGraph(State)
builder.add_node(
    "read_sample",
    read_sample,
    retry_policy=RetryPolicy(max_attempts=3, retry_on=ConnectionError),
)
builder.add_edge(START, "read_sample")
builder.add_edge("read_sample", END)
graph = builder.compile()
print(graph.invoke({"value": "pending"}))
print("attempts:", attempts)

3. Check success and exhausted retries

Expected behavior and failure exits
CaseHandling
First call fails and second succeedsState value is sample-ready; local attempts is 2
Every call raises ConnectionErrorFailure remains after the configured attempt limit; do not report completion
A different exception occursHandle the actual error rather than treating this policy as universal

These are code-derived expectations, not observed LangGraph logs. When changing the function to fail continuously, retain the actual error and stop downstream work. An error_handler can route recovery under the separate documented interface, which currently requires LangGraph>=1.2. This example does not use it or extend that version requirement to every older retry API.

Retry can repeat an external action

The teaching constant has no external effect. In a real node, a retry calls the node again; an external action already sent is not automatically undone. Determine whether the operation is read-only, can use an idempotency key and has responses that leave its status unknown. Do not copy this example into a paid or write operation and claim duplicates are impossible. Async timeout is separate and currently requires async nodes and LangGraph>=1.2; this example does not configure it.

Completion and scope

  • The explicit exception type matches the target operation and the attempt limit has a defined stop reason.
  • Record successful state, attempts and persistent-failure behavior separately; failure does not continue delivery.
  • The teaching counter is process-local, while real side effects receive their own idempotency and unknown-status policy.

The official node configuration page was read to check default exclusions, explicit retry policy and version boundaries. LangGraph was not installed or run; no database, model or external API was called. The code and counts are original teaching material and expectations. Verify them under the actual installed version before integration.

Sources

All links are official pages; verification dates are listed per source, and links point to the version checked.

  1. LangChain · LangGraph: Configure nodes

Found an error? Tell us →