LangGraph provides two primary approaches for managing conversation state: InMemorySaver and SqliteSaver. These checkpointers serve different purposes and offer distinct advantages depending on your use case.
Core Concepts
- Checkpoints - Snapshots of graph state saved at specific points during execution
- Contain configuration, metadata, values, and tasks
- Enable features like time travel and fault tolerance
- Thread IDs - Unique identifiers for each conversation or workflow
- Required for managing multiple simultaneous conversations
- Prevent state mixing between different conversations
flowchart LR subgraph "Conversation Flow" direction TB M1[Message 1] --> M2[Message 2] --> M3[Message 3] end subgraph "In-Memory Storage" direction TB IM["InMemorySaver • RAM-based • Process-lifetime only • Fast access"] end subgraph "SQLite Storage" direction TB DB[(Database File)] Cache["In-Memory Cache • Faster access • Temporary storage"] end subgraph "Thread Management" direction TB TID["Thread ID • Conversation identifier • Groups related states"] end M1 & M2 & M3 --> TID TID --> IM TID --> DB DB <--> Cache style IM fill:#f9f,stroke:#333,color:#000 style DB fill:#9cf,stroke:#333,color:#000 style Cache fill:#fcf,stroke:#333,color:#000
The diagram above illustrates several key concepts:
- Messages flow into thread IDs, which group related conversations together
- SQLite storage uses a dual-layer approach with both disk storage and an in-memory cache for better performance
- The bidirectional arrows between the database and cache represent read/write operations, showing how data flows between persistent storage and temporary memory
Comparison of Saver Types
| Feature | InMemorySaver | SqliteSaver |
|---|---|---|
| Storage Location | RAM only | Disk file or :memory: |
| Persistence | Process lifetime only | Survives process restarts |
| Performance | Fastest access times | Slower due to disk I/O |
| Thread Safety | Built-in support | Requires configuration (check_same_thread=False) |
| Memory Usage | Increases with usage | Managed by SQLite |
| Best For | Development/testing | Production deployments |
Implementation Examples
InMemorySaver Implementation
from langgraph.checkpoint.memory import InMemorySaver
memory = InMemorySaver()
config = {"configurable": {"thread_id": "1"}}
result = app.invoke(input_data, config=config)- Simplest implementation
- Fastest performance
- No external dependencies
- Automatic thread safety
- State lost after process restart
- Memory usage increases over time
- Not suitable for production
InMemorySaver is included with LangGraph core and requires minimal setup. It’s ideal for development and testing scenarios where persistence across sessions isn’t required. SqliteSaver Implementation
import sqlite3
from langgraph.checkpoint.sqlite import SqliteSaver
sqlite_conn = sqlite3.connect('checkpoint.db', check_same_thread=False)
memory = SqliteSaver(sqlite_conn)
config = {"configurable": {"thread_id": "1"}}
result = app.invoke(input_data, config=config)- Persistent storage
- Survives process restarts
- Suitable for production
- Disk space managed by SQLite
- Requires explicit thread configuration
- Slower than in-memory storage
- Needs file system access
SqliteSaver provides persistent storage and is ideal for production environments . The check_same_thread=False parameter is required for multi-threaded applications.
Advanced Features
- Time Travel - Both savers support replaying previous states
- Enable debugging and state inspection
- Allow exploring alternative conversation paths
- Fault Tolerance - Save pending writes from completed nodes
- Resume execution from last checkpoint on failure
- Prevent redundant computations
- State Management
# Get latest state
config = {"configurable": {"thread_id": "1"}}
graph.get_state(config)
# Get state history
history = graph.get_state_history(config)Best Practices
- Development Workflow - Start with InMemorySaver for rapid prototyping
- Test thread management thoroughly
- Verify checkpoint behavior early
- Production Deployment - Migrate to SqliteSaver for persistence
- Implement proper error handling
- Monitor memory usage
- Consider upgrading to Postgres for large-scale deployments
- Performance Optimization - Use appropriate thread IDs for concurrent conversations
- Implement cleanup strategies for old checkpoints
- Monitor storage growth in production environments
Choose InMemorySaver for development and testing, while SqliteSaver is better suited for production environments requiring persistent state across sessions. Both options support advanced features like time travel and fault tolerance, making them powerful tools for building robust conversational applications.