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 LangChainRunnable 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
ReturnCommand 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:Retry Policies
Automatically retry failed nodes:Multiple Retry Policies
Caching
Cache expensive node results:- 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:- Cleanup operations
- Final logging/metrics
- Post-processing steps
Best Practices
Node Design
Node Design
- 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
Control Flow
Control Flow
- Prefer explicit edges for simple flows
- Use conditional edges for dynamic routing
- Use
Commandfor complex multi-step routing - Document routing logic clearly
- Test all routing paths
Performance
Performance
- 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