langgraph-checkpoint-aws: A LangChain checkpointer implementation that uses Bedrock Session Management Service and ElastiCache Valkey to enable stateful and resumable LangGraph agents. · Pythie
A LangChain checkpointer implementation that uses Bedrock Session Management Service and ElastiCache Valkey to enable stateful and resumable LangGraph agents.
Actively maintained.Last release 11 days ago, commits in 26 of the last 26 weeks. A safe choice.
A custom AWS-based persistence solution for LangGraph agents that provides multiple storage backends including Bedrock AgentCore Memory, DynamoDB with S3 offloading, and high-performance Valkey (Redis-compatible) storage.
Overview
This package provides multiple persistence solutions for LangGraph agents:
AWS Bedrock AgentCore Memory Service
Stateful conversations and interactions
Resumable agent sessions
Efficient state persistence and retrieval
Seamless integration with AWS Bedrock
DynamoDB Storage
Checkpoint storage with DynamoDB and automatic S3 offloading
Document storage with semantic vector search via Amazon DynamoDB vector indexes
Unified table design with TTL support
Intelligent compression for optimal storage
Valkey Storage Solutions
Checkpoint storage with Valkey (Redis-compatible)
Intelligent caching for LLM responses and computation results
Document storage with vector search capabilities
Installation
You can install the package using pip:
# Base package (includes Bedrock AgentCore Memory components)
pip install langgraph-checkpoint-aws
# Optional Valkey support
pip install 'langgraph-checkpoint-aws[valkey]'
DynamoDBSaver - DynamoDB-based checkpoint storage with S3 offloading
DynamoDBStore - DynamoDB document store with optional semantic vector search
ValkeySaver - Valkey checkpoint storage
AgentCoreMemoryStore - AWS Bedrock-based document store
ValkeyStore - Valkey document store
ValkeyCache - Valkey LLM response cache
Usage
1. Bedrock Session Management
The agent example also requires LangChain and its AWS model integration:
pip install -U langchain langchain-aws
# Import LangGraph and LangChain components
from langchain.agents import create_agent
from langchain.chat_models import init_chat_model
# Import the AgentCoreMemory integrations
from langgraph_checkpoint_aws import AgentCoreMemorySaver
REGION = "us-west-2"
MEMORY_ID = "YOUR_MEMORY_ID"
MODEL_ID = "us.anthropic.claude-sonnet-5"
# Initialize checkpointer for state persistence. No additional setup required.
# Sessions will be saved and persisted for actor_id/session_id combinations
checkpointer = AgentCoreMemorySaver(MEMORY_ID, region_name=REGION)
# Initialize chat model
model = init_chat_model(MODEL_ID, model_provider="bedrock_converse", region_name=REGION)
# Create a LangChain agent with checkpoint persistence
agent = create_agent(
model=model,
tools=[], # Add tools here if needed
checkpointer=checkpointer, # AgentCoreMemorySaver we created above
)
# Specify config at runtime for ACTOR and SESSION
config = {
"configurable": {
"thread_id": "session-1", # REQUIRED: This maps to Bedrock AgentCore session_id under the hood
"actor_id": "react-agent-1", # REQUIRED: This maps to Bedrock AgentCore actor_id under the hood
}
}
# Invoke the agent
response = agent.invoke(
{
"messages": [
{
"role": "user",
"content": "I like sushi with tuna. In general seafood is great.",
}
]
},
config=config,
)
2. Bedrock Memory Store
# Import LangGraph and LangChain components
from langchain.chat_models import init_chat_model
from langgraph.prebuilt import create_react_agent
from langgraph_checkpoint_aws import AgentCoreMemoryStore
REGION = "us-west-2"
MEMORY_ID = "YOUR_MEMORY_ID"
MODEL_ID = "us.anthropic.claude-sonnet-4-20250514-v1:0"
# Initialize store for saving and searching over long term memories
# such as preferences and facts across sessions
store = AgentCoreMemoryStore(MEMORY_ID, region_name=REGION)
# Pre-model hook runs and saves messages of your choosing to AgentCore Memory
# for async processing and extraction
def pre_model_hook(state, config: RunnableConfig, *, store: BaseStore):
"""Hook that runs pre-model invocation to save the latest human message"""
actor_id = config["configurable"]["actor_id"]
thread_id = config["configurable"]["thread_id"]
# Saving the message to the actor and session combination that we get at runtime
namespace = (actor_id, thread_id)
messages = state.get("messages", [])
# Save the last human message we see before model invocation
for msg in reversed(messages):
if isinstance(msg, HumanMessage):
store.put(namespace, str(uuid.uuid4()), {"message": msg})
break
# OPTIONAL: Retrieve user preferences based on the last message and append to state
# user_preferences_namespace = ("preferences", actor_id)
# preferences = store.search(user_preferences_namespace, query=msg.content, limit=5)
# # Add to input messages as needed
return {"model_input_messages": messages}
# Initialize chat model
model = init_chat_model(MODEL_ID, model_provider="bedrock_converse", region_name=REGION)
# Create a pre-built langgraph agent (configurations work for custom agents too)
graph = create_react_agent(
model=model,
tools=[],
pre_model_hook=pre_model_hook,
)
# Specify config at runtime for ACTOR and SESSION
config = {
"configurable": {
"thread_id": "session-1", # REQUIRED: This maps to Bedrock AgentCore session_id under the hood
"actor_id": "react-agent-1", # REQUIRED: This maps to Bedrock AgentCore actor_id under the hood
}
}
# Invoke the agent
response = graph.invoke(
{"messages": [("human", "I like sushi with tuna. In general seafood is great.")]},
config=config,
)
3. Valkey Cache - LLM Response caching
Intelligent caching to improve performance and reduce costs:
from langgraph_checkpoint_aws import ValkeyCache
from langchain_aws import ChatBedrockConverse
# Initialize cache
with ValkeyCache.from_conn_string(
"valkey://localhost:6379",
ttl_seconds=3600, # 1 hour TTL
pool_size=10,
) as cache:
# Use cache with your LLM calls
model = ChatBedrockConverse(model="us.anthropic.claude-sonnet-4-5-20250929-v1:0")
# Cache expensive prompts/computations
cache_key = "expensive_computation_key"
result = cache.get([cache_key])
if not result:
# Compute and cache result
prompt: str = "Your expensive prompt"
response = model.invoke([HumanMessage(content=prompt)])
cache.set({cache_key: (response.content, 3600)}) # Cache for 1 hour
4. DynamoDB Checkpoint Storage
from langgraph.graph import StateGraph
from langgraph_checkpoint_aws import DynamoDBSaver
# Basic usage with DynamoDB only
checkpointer = DynamoDBSaver(table_name="my-checkpoints", region_name="us-west-2")
# With S3 offloading for large checkpoints (>350KB)
checkpointer = DynamoDBSaver(
table_name="my-checkpoints",
region_name="us-west-2",
s3_offload_config={"bucket_name": "my-checkpoint-bucket"},
)
# Production configuration with TTL and compression
checkpointer = DynamoDBSaver(
table_name="my-checkpoints",
region_name="us-west-2",
ttl_seconds=86400 * 7, # 7 days
enable_checkpoint_compression=True,
s3_offload_config={"bucket_name": "my-checkpoint-bucket"},
)
# Create your graph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
graph = builder.compile(checkpointer=checkpointer)
config = {"configurable": {"thread_id": "session-1"}}
result = graph.invoke(1, config)
5. Valkey Checkpoint Storage
from langgraph.graph import StateGraph
from langgraph_checkpoint_aws import ValkeySaver
# Using connection string
with ValkeySaver.from_conn_string(
"valkey://localhost:6379",
ttl_seconds=3600, # 1 hour TTL
pool_size=10,
) as checkpointer:
# Create your graph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
graph = builder.compile(checkpointer=checkpointer)
config = {"configurable": {"thread_id": "session-1"}}
result = graph.invoke(1, config)
6. AgentCore Valkey Storage
For AWS Bedrock AgentCore-compatible applications that want to use Valkey instead of managed Memory:
from langgraph.prebuilt import create_react_agent
from langgraph_checkpoint_aws import AgentCoreValkeySaver
# Using connection string
with AgentCoreValkeySaver.from_conn_string(
"valkey://localhost:6379",
ttl_seconds=3600, # 1 hour TTL
pool_size=10,
) as checkpointer:
# Create your agent
graph = create_react_agent(model=model, tools=tools, checkpointer=checkpointer)
# AgentCore-style configuration (requires actor_id)
config = {
"configurable": {
"thread_id": "session-123",
"actor_id": "agent-456", # Required for AgentCore compatibility
"checkpoint_ns": "production",
}
}
result = graph.invoke({"messages": [...]}, config)
Key Differences from ValkeySaver:
✅ Requires actor_id in configuration (AgentCore requirement)
git clone <repository-url>
cd libs/aws/langgraph-checkpoint-aws
Install development dependencies:
make install_all
Or install specific components:
make install_dev # Basic development tools
make install_test # Testing tools
make install_lint # Linting tools
make install_typing # Type checking tools
make install_codespell # Spell checking tools
Running Tests
make tests # Run all tests
make test_watch # Run tests in watch mode
Code Quality
make lint # Run linter
make format # Format code
make spell_check # Check spelling
Clean Up
make clean # Remove all generated files
Infrastructure Setup
AWS Configuration (for Bedrock components)
Ensure you have AWS credentials configured using one of these methods:
Environment variables
AWS credentials file (~/.aws/credentials)
IAM roles
Direct credential injection via constructor parameters
Required AWS permissions for Bedrock Session Management:
# Connect to AWS ElastiCache from host running inside VPC with access to cache
from langgraph_checkpoint_aws.checkpoint.valkey import ValkeySaver
with ValkeySaver.from_conn_string(
"valkeys://your-elasticache-cluster.amazonaws.com:6379", pool_size=20
) as checkpointer:
pass
If you want to connect to cache from a host outside of VPC, use ElastiCache console to setup a jump host so you could create SSH tunnel to access cache locally.
Using Docker
# Start Valkey with required modules
docker run --name valkey-bundle -p 6379:6379 -d valkey/valkey-bundle:latest
# Or with custom configuration
docker run --name valkey-custom \
-p 6379:6379 \
-v $(pwd)/valkey.conf:/etc/valkey/valkey.conf \
-d valkey/valkey-bundle:latest
Performance and Best Practices
Valkey Performance Optimization
Connection Pooling
# Use connection pools for better performance
from valkey.connection import ConnectionPool
pool = ConnectionPool.from_url(
"valkey://localhost:6379", max_connections=20, retry_on_timeout=True
)
with ValkeySaver.from_pool(pool) as checkpointer:
# Reuse connections across operations
pass
TTL Strategy
# Configure appropriate TTL values
with ValkeySaver.from_conn_string(
"valkey://localhost:6379",
ttl_seconds=3600, # 1 hour for active sessions
) as checkpointer:
pass