Deploying LangGraph Agent Workflows in Production: A Distributed Execution Guide
This article walks through moving LangGraph from development to production by configuring persistent checkpoints with PostgresSaver or RedisSaver, using stream_mode for flexible streaming outputs, deploying the LangGraph Server via CLI or Docker Compose, handling retries, monitoring state, and visualizing the overall architecture.
Production checkpoint
Development uses InMemorySaver. Production requires a persistent checkpoint. Two options are shown:
# PostgresSaver (recommended for production)
from langgraph.checkpoint.postgres import PostgresSaver
saver = PostgresSaver.from_conn_string(
"postgresql://user:[email protected]:5432/langgraph"
)
saver.setup() # initialize tables
graph = builder.compile(checkpointer=saver)
# RedisSaver (suitable for high‑concurrency scenarios)
from langgraph.checkpoint.redis import RedisSaver
saver = RedisSaver.from_url("redis://redis.example.com:6379/0")
graph = builder.compile(checkpointer=saver)Streaming output (stream_mode)
The framework provides three streaming modes. Each mode yields a chunk that can be printed directly.
# values mode – outputs each step's state
for chunk in graph.stream({"messages": [{"role": "user", "content": "你好"}]}, stream_mode="values"):
print(chunk)
# messages mode – outputs incremental messages
for chunk in graph.stream({"messages": [{"role": "user", "content": "你好"}]}, stream_mode="messages"):
print(chunk)
# custom mode – user‑defined output format
for chunk in graph.stream({"messages": [{"role": "user", "content": "你好"}]}, stream_mode="custom"):
print(chunk)LangGraph Server: quick deployment
Install the CLI, scaffold a project from the React‑agent template, and run the server in development or production mode.
# Install (the "inmem" extra provides the in‑memory backend for development)
pip install "langgraph-cli[inmem]"
# Create a project from the template
langgraph new my-agent --template react-agent
# Start the development server (hot‑reload)
langgraph dev
# Production mode (Docker Compose launches the full backend)
langgraph up --port 8123REST API
Streaming responses
Checkpoint persistence
Concurrency management
API call examples
Two request patterns are demonstrated: a synchronous POST and a streaming POST.
import requests
# Synchronous call
response = requests.post(
"http://localhost:54321/invoke",
json={
"input": {"messages": [{"role": "user", "content": "你好"}]},
"config": {"configurable": {"thread_id": "test"}}
}
)
print(response.json())
# Streaming call
with requests.post(
"http://localhost:54321/stream",
json={"input": {"messages": [{"role": "user", "content": "你好"}]}} ,
stream=True
) as r:
for line in r.iter_lines():
print(line)Error recovery and retry
Retry behavior is configured per node via retry_policy passed to add_node. The example uses exponential back‑off.
from langgraph.graph import StateGraph, START
from langgraph.types import RetryPolicy
builder = StateGraph(State)
builder.add_node(
"task",
task_node,
retry_policy=RetryPolicy(
initial_interval=1.0, # first wait in seconds
backoff_factor=2.0, # exponential factor
max_interval=128.0, # max wait per attempt
max_attempts=3 # maximum retries
),
)
builder.add_edge(START, "task")
graph = builder.compile(checkpointer=memory)Monitoring and debugging
Current state can be inspected with graph.get_state. Checkpoint history is listed via the checkpoint store.
# Get current state
snapshot = graph.get_state(config)
print(f"Current node: {snapshot.next}")
print(f"State: {snapshot.values}")
# List checkpoint history
for ckpt in memory.list(config):
print(f"Checkpoint: {ckpt.id}")Architecture overview
┌─────────────────────────────────────────────────────────────┐
│ LangGraph application layer │
│ │
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ │
│ │ State │ │ Node │ │ Edge │ │ Check │ │
│ │ Graph │ │ (process)│ │ (connect)│ │ pointer │ │
│ └────┬────┘ └────┬────┘ └────┬────┘ └────┬────┘ │
│ │ │ │ │ │
│ └────────────┴────────────┴────────────┘ │
│ │ │
│ ┌──────────┴───────────┐ │
│ │ LangGraph Server │ │
│ └───────────────────────┘ │
└─────────────────────────────────────────────────────────────┘Concept recap
PostgresSaver : production‑grade checkpoint stored in PostgreSQL.
RedisSaver : checkpoint implementation optimized for high‑concurrency scenarios.
stream_mode : three output formats – values, messages, custom.
LangGraph Server : one‑click deployment exposing a REST API with streaming, persistence, and concurrency handling.
RetryPolicy : node‑level retry configuration supplied to add_node.
Related links
https://langchain-ai.github.io/langgraph/
https://langchain-ai.github.io/langgraph/deployment/
https://langchain-ai.github.io/langgraph/cli/Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Tech Ocean
Focused on AI programming, sharing ready-to-use development efficiency solutions.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
