Development-time thread memory
Keep short-term conversation or graph state available across invocations that use the same thread during one process lifetime.
AI Agents · Class
MemorySaver is LangGraph.js's in-memory checkpointer for saving graph checkpoints by thread. It is useful for development and testing, but its state is lost when the process restarts.
MemorySaver is an in-memory implementation of LangGraph's checkpoint saver. Attach it during graph compilation so checkpoints are associated with thread IDs and can support state continuity during the process lifetime.
new MemorySaver(serde?: SerializerProtocol): MemorySaver| Name | Type | Description |
|---|---|---|
serde | SerializerProtocol | Optional serializer used to encode and decode checkpoint data. |
A MemorySaver instance that implements the checkpoint-saver interface.
import { MemorySaver } from "@langchain/langgraph";
import { StateGraph, StateSchema, START, END } from "@langchain/langgraph";
const State = new StateSchema({ value: { reducer: (a: number, b: number) => b, default: () => 0 } });
const graph = new StateGraph(State)
.addNode("increment", (state) => ({ value: state.value + 1 }))
.addEdge(START, "increment")
.addEdge("increment", END)
.compile({ checkpointer: new MemorySaver() });
const config = { configurable: { thread_id: "demo" } };
console.log(await graph.invoke({ value: 0 }, config));Creates an in-memory checkpointer, attaches it when compiling a graph, and invokes that graph with a thread identifier.
Keep short-term conversation or graph state available across invocations that use the same thread during one process lifetime.
Run graph tests that need checkpoint-backed state without provisioning a database.
Pause, resume, inspect, or replay graph execution using in-memory checkpoints.
MemorySaver is a checkpointer that stores graph checkpoints in memory. Pass an instance to the graph's compile configuration, then provide a thread_id when invoking the graph so state is associated with that thread.
Follow-up: How would you preserve state across deployments? Replace MemorySaver with a durable checkpointer backed by persistent storage, because in-memory checkpoints disappear when the process exits.
A checkpointer records graph state at supersteps and enables thread-scoped capabilities such as conversational continuity and human-in-the-loop workflows. MemorySaver provides that behavior without external persistence, while the process remains alive.
Follow-up: What identifier is required for checkpoint lookup? Supply configurable.thread_id; checkpoint_id can additionally select a particular checkpoint when needed.
Compile the graph with a MemorySaver instance and invoke it using a consistent thread_id. Reusing that identifier lets separate invocations refer to the same in-memory thread state.
Follow-up: Can separate application replicas share this state? No. Process-local memory is not a shared or durable persistence layer, so use an external checkpointer for that requirement.
BaseCheckpointSaverSerializerProtocolCheckpointStateGraphCompiledGraphthread_idcheckpoint_id