Skip to content

Chapter 5 · Checkpoints, threads, and cross-thread memory ​

Store conversation progress separately from long-term preferences. Three calls demonstrate the different scopes of a checkpointer and a store.

Mechanics and design choices ​

A checkpointer saves graph state under thread_id. A store holds application-defined namespaced key-value data across threads. A thread ID locates a conversation; it does not authenticate a user. Derive identities and namespaces on a trusted server. In-memory implementations do not survive process termination.

Example ​

python
from typing import TypedDict
from langgraph.graph import StateGraph, START, END
from langgraph.checkpoint.memory import InMemorySaver
from langgraph.store.memory import InMemoryStore

class State(TypedDict):
    visits: int
    greeting: str

store = InMemoryStore()
store.put(("demo-user", "preferences"), "language", {"value": "English"})

def greet(state: State):
    preference = store.get(("demo-user", "preferences"), "language")
    return {"visits": state.get("visits", 0) + 1, "greeting": preference.value["value"]}

builder = StateGraph(State)
builder.add_node("greet", greet)
builder.add_edge(START, "greet")
builder.add_edge("greet", END)
graph = builder.compile(checkpointer=InMemorySaver(), store=store)
a = {"configurable": {"thread_id": "conversation-a"}}
b = {"configurable": {"thread_id": "conversation-b"}}
assert graph.invoke({"visits": 0}, a)["visits"] == 1
assert graph.invoke({}, a)["visits"] == 2
assert graph.invoke({"visits": 0}, b)["visits"] == 1
assert graph.get_state(a).values["greeting"] == "English"
print(graph.get_state(a).values)

Run and verify ​

No model is required. The second call submits an empty update rather than resetting visits, so it reaches two. Another thread starts at one but shares the preference. The closure holds the store for simplicity; use runtime dependency injection in services.

Pitfalls ​

Do not call in-memory examples production persistence. Database adoption also needs backup recovery, tenant isolation, retention, and sensitive-data deletion tests. Keep tokens out of serialized graph state.

🧪 随堂测验

点击你认为正确的选项。答错时会展示正确答案与原因解析。

1. What saves thread state?

2. Where should cross-thread preferences live?

3. Does in-memory storage survive restart?

4. Does thread_id prove identity?

Hands-on exercises ​

  1. Add another user namespace and assert isolation.
  2. Restart and explain which data disappears and why.
  3. Inspect get_state values and next, distinguishing data from pending nodes.

Official references ​

Verified on: 2026-09-17.