Session 8· 04· 15 min
Short-Term Memory (Checkpointer)
What you'll learn
- ▸Add InMemorySaver to create a multi-turn chatbot
- ▸Use thread_id to maintain separate conversations
- ▸See that different thread_ids have independent memory
The problem: agents forget between invoke() calls
Without a checkpointer, each invoke() starts fresh — the agent has no memory of previous turns. A checkpointer persists the message state between calls, scoped by thread_id.
Without checkpointer
- •Each invoke() starts fresh
- •No memory
- •Fine for single-shot tasks
With checkpointer
- •State persists between calls
- •Same thread_id = same conversation
- •Different thread_id = separate memory
04_memory.py
from langgraph.checkpoint.memory import InMemorySaver ①
agent = create_agent(
model=f"openai:{model_name}",
tools=[get_user_info],
checkpointer=InMemorySaver(), ②
)
config = {"configurable": {"thread_id": "support-thread-1"}} ③
# Turn 1
agent.invoke(
{"messages": [{"role": "user", "content": "My name is Alice."}]},
config=config, ④
)
# Turn 2 — agent remembers Alice
agent.invoke(
{"messages": [{"role": "user", "content": "What's my name?"}]},
config=config,
)①InMemorySaver stores state in Python memory (use PostgresSaver for production).
②Pass the checkpointer to create_agent.
③thread_id scopes the conversation — same id = same memory.
④Pass config on every invoke() call.
$ python 04_memory.py
Code Check
What happens if you change thread_id between turns?
Recap — what you just learned
- ✓InMemorySaver (dev) or PostgresSaver (prod) gives agents memory
- ✓thread_id scopes conversations — different ids = separate memory
- ✓Pass config={"configurable": {"thread_id": "..."}} on every invoke()