Skip to main content

Overview

Nodes are the computational units in LangGraph, while edges define the execution order. Together they form a directed graph that can contain cycles for iterative workflows.

Nodes

A node is any callable that accepts state and returns a partial state update.

Function Nodes

The simplest node is a Python function:

Runnable Nodes

Any LangChain Runnable can be a node:

Class-Based Nodes

Callable classes work as nodes:

Node Signatures

Nodes can access runtime context:

Async Nodes

Nodes can be async for concurrent I/O:

Node Metadata

Attach metadata for observability:

Edges

Edges define the execution order between nodes.

Unconditional Edges

Fixed transitions from one node to another:

Parallel Edges

Multiple nodes execute in parallel:

Conditional Edges

Dynamic routing based on state:

Multi-Destination Routing

Return multiple destinations to run in parallel:

Type-Safe Routing with Literals

The Send API

Dynamically invoke nodes with custom input (map-reduce pattern):
Send allows nodes to execute with different input than the main graph state, perfect for map-reduce workflows.

Command API

Return Command objects from nodes for advanced control flow:

Command Fields

Sequences

Quickly add a chain of nodes:

Cycles and Loops

LangGraph supports cycles for iterative workflows:
Cycles can run indefinitely. Set a recursion_limit to prevent infinite loops:

Retry Policies

Automatically retry failed nodes:

Multiple Retry Policies

Caching

Cache expensive node results:
Cached nodes:
  • Skip execution on cache hit
  • Use input state as cache key (customizable)
  • Respect TTL for expiration
  • Store results across invocations

Deferred Nodes

Delay node execution until graph end:
Use cases:
  • Cleanup operations
  • Final logging/metrics
  • Post-processing steps

Best Practices

  • Keep nodes pure and focused on single tasks
  • Use type hints for state and return values
  • Handle errors gracefully within nodes
  • Return partial state updates, not full state
  • Make nodes testable in isolation
  • Prefer explicit edges for simple flows
  • Use conditional edges for dynamic routing
  • Use Command for complex multi-step routing
  • Document routing logic clearly
  • Test all routing paths
  • Parallelize independent nodes
  • Use async nodes for I/O operations
  • Cache expensive operations
  • Set reasonable retry limits
  • Monitor node execution times

Next Steps

Checkpointing

Persist state between node executions

Streaming

Stream node outputs in real-time

Human-in-the-Loop

Add human review points with interrupts