Checkpoints¶
You can compile any LangGraph workflow with a CheckPointer to give your agent "memory" by persisting its state. This permits things like:
- Remembering things across multiple interactions
- Interrupting to wait for user input
- Resiliance for long-running, error-prone agents
- Time travel retry and branch from a previous checkpoint
Checkpoint¶
Bases: TypedDict
State snapshot at a given point in time.
Source code in langgraph/checkpoint/base.py
v: int
instance-attribute
¶
The version of the checkpoint format. Currently 1.
ts: str
instance-attribute
¶
The timestamp of the checkpoint in ISO 8601 format.
channel_values: dict[str, Any]
instance-attribute
¶
The values of the channels at the time of the checkpoint.
Mapping from channel name to channel snapshot value.
channel_versions: defaultdict[str, int]
instance-attribute
¶
The versions of the channels at the time of the checkpoint.
The keys are channel names and the values are the logical time step at which the channel was last updated.
versions_seen: defaultdict[str, defaultdict[str, int]]
instance-attribute
¶
Map from node ID to map from channel name to version seen.
This keeps track of the versions of the channels that each node has seen.
Used to determine which nodes to execute next.
BaseCheckpointSaver¶
Bases: ABC
Source code in langgraph/checkpoint/base.py
SerializerProtocol¶
Bases: Protocol
Protocol for serialization and deserialization of objects.
dumps: Serialize an object to bytes.loads: Deserialize an object from bytes.
Valid implementations include the pickle, json and orjson modules.
Source code in langgraph/serde/base.py
Implementations¶
LangGraph also natively provides the following checkpoint implementations.
MemorySaver¶
Bases: BaseCheckpointSaver
An in-memory checkpoint saver.
This checkpoint saver stores checkpoints in memory using a defaultdict.
Note
Since checkpoints are saved in memory, they will be lost when the program exits. Only use this saver for debugging or testing purposes.
Parameters:
-
serde(Optional[SerializerProtocol], default:None) –The serializer to use for serializing and deserializing checkpoints. Defaults to None.
Examples:
import asyncio
from langgraph.checkpoint.memory import MemorySaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
memory = MemorySaver()
graph = builder.compile(checkpointer=memory)
coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
asyncio.run(coro) # Output: 2
Source code in langgraph/checkpoint/memory.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 | |
get_tuple(config)
¶
Get a checkpoint tuple from the in-memory storage.
This method retrieves a checkpoint tuple from the in-memory storage based on the provided config. If the config contains a "thread_ts" key, the checkpoint with the matching thread ID and timestamp is retrieved. Otherwise, the latest checkpoint for the given thread ID is retrieved.
Parameters:
-
config(RunnableConfig) –The config to use for retrieving the checkpoint.
Returns:
-
Optional[CheckpointTuple]–Optional[CheckpointTuple]: The retrieved checkpoint tuple, or None if no matching checkpoint was found.
Source code in langgraph/checkpoint/memory.py
list(config, *, before=None, limit=None)
¶
List checkpoints from the in-memory storage.
This method retrieves a list of checkpoint tuples from the in-memory storage based on the provided config. The checkpoints are ordered by timestamp in descending order.
Parameters:
-
config(RunnableConfig) –The config to use for listing the checkpoints.
-
before(Optional[RunnableConfig], default:None) –If provided, only checkpoints before the specified timestamp are returned. Defaults to None.
-
limit(Optional[int], default:None) –The maximum number of checkpoints to return. Defaults to None.
Yields:
-
CheckpointTuple–Iterator[CheckpointTuple]: An iterator of checkpoint tuples.
Source code in langgraph/checkpoint/memory.py
put(config, checkpoint, metadata)
¶
Save a checkpoint to the in-memory storage.
This method saves a checkpoint to the in-memory storage. The checkpoint is associated with the provided config.
Parameters:
-
config(RunnableConfig) –The config to associate with the checkpoint.
-
checkpoint(Checkpoint) –The checkpoint to save.
Returns:
-
RunnableConfig(RunnableConfig) –The updated config containing the saved checkpoint's timestamp.
Source code in langgraph/checkpoint/memory.py
aget_tuple(config)
async
¶
Asynchronous version of get_tuple.
This method is an asynchronous wrapper around get_tuple that runs the synchronous method in a separate thread using asyncio.
Parameters:
-
config(RunnableConfig) –The config to use for retrieving the checkpoint.
Returns:
-
Optional[CheckpointTuple]–Optional[CheckpointTuple]: The retrieved checkpoint tuple, or None if no matching checkpoint was found.
Source code in langgraph/checkpoint/memory.py
alist(config)
async
¶
Asynchronous version of list.
This method is an asynchronous wrapper around list that runs the synchronous method in a separate thread using asyncio.
Parameters:
-
config(RunnableConfig) –The config to use for listing the checkpoints.
Yields:
-
AsyncIterator[CheckpointTuple]–AsyncIterator[CheckpointTuple]: An asynchronous iterator of checkpoint tuples.
Source code in langgraph/checkpoint/memory.py
AsyncSqliteSaver¶
Bases: BaseCheckpointSaver, AbstractAsyncContextManager
An asynchronous checkpoint saver that stores checkpoints in a SQLite database.
Tip
Requires the aiosqlite package.
Install it with pip install aiosqlite.
Note
While this class does support asynchronous checkpointing, it is not recommended for production workloads, due to limitations in SQLite's write performance. For production workloads, consider using a more robust database like PostgreSQL.
Parameters:
-
conn(Connection) –The asynchronous SQLite database connection.
-
serde(Optional[SerializerProtocol], default:None) –The serializer to use for serializing and deserializing checkpoints. Defaults to JsonPlusSerializerCompat.
Examples:
Usage within a StateGraph:
import asyncio
import aiosqlite
from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
memory = AsyncSqliteSaver.from_conn_string("checkpoints.sqlite")
graph = builder.compile(checkpointer=memory)
coro = graph.ainvoke(1, {"configurable": {"thread_id": "thread-1"}})
asyncio.run(coro) # Output: 2
Raw usage:
import asyncio
import aiosqlite
from langgraph.checkpoint.aiosqlite import AsyncSqliteSaver
async def main():
async with aiosqlite.connect("checkpoints.db") as conn:
saver = AsyncSqliteSaver(conn)
config = {"configurable": {"thread_id": "1"}}
checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
saved_config = await saver.aput(config, checkpoint)
print(
saved_config
) # Output: {"configurable": {"thread_id": "1", "thread_ts": "2023-05-03T10:00:00Z"}}
asyncio.run(main())
Source code in langgraph/checkpoint/aiosqlite.py
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 | |
from_conn_string(conn_string)
classmethod
¶
Create a new AsyncSqliteSaver instance from a connection string.
Parameters:
-
conn_string(str) –The SQLite connection string.
Returns:
-
AsyncSqliteSaver(AsyncSqliteSaver) –A new AsyncSqliteSaver instance.
Source code in langgraph/checkpoint/aiosqlite.py
setup()
async
¶
Set up the checkpoint database asynchronously.
This method creates the necessary tables in the SQLite database if they don't already exist. It is called automatically when needed and should not be called directly by the user.
Source code in langgraph/checkpoint/aiosqlite.py
aget_tuple(config)
async
¶
Get a checkpoint tuple from the database asynchronously.
This method retrieves a checkpoint tuple from the SQLite database based on the provided config. If the config contains a "thread_ts" key, the checkpoint with the matching thread ID and timestamp is retrieved. Otherwise, the latest checkpoint for the given thread ID is retrieved.
Parameters:
-
config(RunnableConfig) –The config to use for retrieving the checkpoint.
Returns:
-
Optional[CheckpointTuple]–Optional[CheckpointTuple]: The retrieved checkpoint tuple, or None if no matching checkpoint was found.
Source code in langgraph/checkpoint/aiosqlite.py
alist(config, *, before=None, limit=None)
async
¶
List checkpoints from the database asynchronously.
This method retrieves a list of checkpoint tuples from the SQLite database based on the provided config. The checkpoints are ordered by timestamp in descending order.
Parameters:
-
config(RunnableConfig) –The config to use for listing the checkpoints.
-
before(Optional[RunnableConfig], default:None) –If provided, only checkpoints before the specified timestamp are returned. Defaults to None.
-
limit(Optional[int], default:None) –The maximum number of checkpoints to return. Defaults to None.
Yields:
-
AsyncIterator[CheckpointTuple]–AsyncIterator[CheckpointTuple]: An asynchronous iterator of checkpoint tuples.
Source code in langgraph/checkpoint/aiosqlite.py
aput(config, checkpoint, metadata)
async
¶
Save a checkpoint to the database asynchronously.
This method saves a checkpoint to the SQLite database. The checkpoint is associated with the provided config and its parent config (if any).
Parameters:
-
config(RunnableConfig) –The config to associate with the checkpoint.
-
checkpoint(Checkpoint) –The checkpoint to save.
Returns:
-
RunnableConfig(RunnableConfig) –The updated config containing the saved checkpoint's timestamp.
Source code in langgraph/checkpoint/aiosqlite.py
SqliteSaver¶
Bases: BaseCheckpointSaver, AbstractContextManager
A checkpoint saver that stores checkpoints in a SQLite database.
Note
This class is meant for lightweight, synchronous use cases
(demos and small projects) and does not
scale to multiple threads.
For a similar sqlite saver with async support,
consider using AsyncSqliteSaver.
Parameters:
-
conn(Connection) –The SQLite database connection.
-
serde(Optional[SerializerProtocol], default:None) –The serializer to use for serializing and deserializing checkpoints. Defaults to JsonPlusSerializerCompat.
Examples:
import sqlite3
from langgraph.checkpoint.sqlite import SqliteSaver
from langgraph.graph import StateGraph
builder = StateGraph(int)
builder.add_node("add_one", lambda x: x + 1)
builder.set_entry_point("add_one")
builder.set_finish_point("add_one")
conn = sqlite3.connect("checkpoints.sqlite")
memory = SqliteSaver(conn)
graph = builder.compile(checkpointer=memory)
config = {"configurable": {"thread_id": "1"}}
# checkpoint = {"ts": "2023-05-03T10:00:00Z", "data": {"key": "value"}}
result = graph.invoke(3, config)
graph.get_state(config)
# Output: StateSnapshot(values=4, next=(), config={'configurable': {'thread_id': '1', 'thread_ts': '2024-05-04T06:32:42.235444+00:00'}}, parent_config=None)
Source code in langgraph/checkpoint/sqlite.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 | |
from_conn_string(conn_string)
classmethod
¶
Create a new SqliteSaver instance from a connection string.
Parameters:
-
conn_string(str) –The SQLite connection string.
Returns:
-
SqliteSaver(SqliteSaver) –A new SqliteSaver instance.
Examples:
In memory:
memory = SqliteSaver.from_conn_string(":memory:")
To disk:
memory = SqliteSaver.from_conn_string("checkpoints.sqlite")
Source code in langgraph/checkpoint/sqlite.py
setup()
¶
Set up the checkpoint database.
This method creates the necessary tables in the SQLite database if they don't already exist. It is called automatically when needed and should not be called directly by the user.
Source code in langgraph/checkpoint/sqlite.py
cursor(transaction=True)
¶
Get a cursor for the SQLite database.
This method returns a cursor for the SQLite database. It is used internally by the SqliteSaver and should not be called directly by the user.
Parameters:
-
transaction(bool, default:True) –Whether to commit the transaction when the cursor is closed. Defaults to True.
Yields:
-
Cursor–sqlite3.Cursor: A cursor for the SQLite database.
Source code in langgraph/checkpoint/sqlite.py
get_tuple(config)
¶
Get a checkpoint tuple from the database.
This method retrieves a checkpoint tuple from the SQLite database based on the provided config. If the config contains a "thread_ts" key, the checkpoint with the matching thread ID and timestamp is retrieved. Otherwise, the latest checkpoint for the given thread ID is retrieved.
Parameters:
-
config(RunnableConfig) –The config to use for retrieving the checkpoint.
Returns:
-
Optional[CheckpointTuple]–Optional[CheckpointTuple]: The retrieved checkpoint tuple, or None if no matching checkpoint was found.
Examples:
Basic:
config = {"configurable": {"thread_id": "1"}}
checkpoint_tuple = memory.get_tuple(config)
print(checkpoint_tuple) # Output: CheckpointTuple(...)
With timestamp:
config = {
"configurable": {
"thread_id": "1",
"thread_ts": "2024-05-04T06:32:42.235444+00:00",
}
}
checkpoint_tuple = memory.get_tuple(config)
print(checkpoint_tuple) # Output: CheckpointTuple(...)
Source code in langgraph/checkpoint/sqlite.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
list(config, *, before=None, limit=None)
¶
List checkpoints from the database.
This method retrieves a list of checkpoint tuples from the SQLite database based on the provided config. The checkpoints are ordered by timestamp in descending order.
Parameters:
-
config(RunnableConfig) –The config to use for listing the checkpoints.
-
before(Optional[RunnableConfig], default:None) –If provided, only checkpoints before the specified timestamp are returned. Defaults to None.
-
limit(Optional[int], default:None) –The maximum number of checkpoints to return. Defaults to None.
Yields:
-
CheckpointTuple–Iterator[CheckpointTuple]: An iterator of checkpoint tuples.
Examples:
config = {"configurable": {"thread_id": "1"}} checkpoints = list(memory.list(config, limit=2)) print(checkpoints) # Output: [CheckpointTuple(...), CheckpointTuple(...)]
config = {"configurable": {"thread_id": "1"}} before = {"configurable": {"thread_ts": "2024-05-04T06:32:42.235444+00:00"}} checkpoints = list(memory.list(config, before=before)) print(checkpoints) # Output: [CheckpointTuple(...), ...]
Source code in langgraph/checkpoint/sqlite.py
put(config, checkpoint, metadata)
¶
Save a checkpoint to the database.
This method saves a checkpoint to the SQLite database. The checkpoint is associated with the provided config and its parent config (if any).
Parameters:
-
config(RunnableConfig) –The config to associate with the checkpoint.
-
checkpoint(Checkpoint) –The checkpoint to save.
-
metadata(Optional[dict[str, Any]]) –Additional metadata to save with the checkpoint. Defaults to None.
Returns:
-
RunnableConfig(RunnableConfig) –The updated config containing the saved checkpoint's timestamp.
Examples:
config = {"configurable": {"thread_id": "1"}}
checkpoint = {"ts": "2024-05-04T06:32:42.235444+00:00", "data": {"key": "value"}}
saved_config = memory.put(config, checkpoint)
print(
saved_config
) # Output: {"configurable": {"thread_id": "1", "thread_ts": 2024-05-04T06:32:42.235444+00:00"}}