Memory โ LangMem & the Persistence Stack
Lesson 9: Memory โ LangMem & the Persistence Stack
An agent without memory is a goldfish with a system prompt. The Lang ecosystem layers memory at three levels: conversation context (the current run), short-term memory (past runs in a thread), and long-term memory (facts learned across all interactions). Knowing which layer you need is the difference between "remembers this conversation" and "knows the user."
The three memory layers
| Layer | Scope | Mechanism | Example |
|---|---|---|---|
| Context | One run | Messages in state | "Summarize this document" |
| Short-term (thread) | One conversation | Checkpointer keyed by thread_id | Multi-turn chat, multi-step task |
| Long-term (user/agent) | Across all conversations | LangMem semantic/episodic memory in a Store | "User prefers terse replies" or "I learned the refund workflow" |
Short-term memory with the checkpointer
You've seen the mechanism in the LangGraph lesson: compile with a checkpointer, pass thread_id, and every run's state is saved and reloaded. For durable storage across restarts, swap MemorySaver for SQLite or Postgres โ the API is identical.
from langgraph.checkpoint.sqlite import SqliteSaver
with SqliteSaver.from_conn_string("checkpoints.db") as saver:
app = graph.compile(checkpointer=saver)
# Same invoke call โ now state survives process restarts
Long-term memory with LangMem
LangMem (langmem package) gives agents the ability to learn over time. Instead of you hardcoding user preferences, the agent extracts them during conversations and stores them:
from langmem import create_memory_manager
memory_manager = create_memory_manager(
model=model,
namespace=("users", "{user_id}"),
instructions="Extract durable preferences and facts about this user.",
)
# Run periodically in your graph to consolidate what the agent learned:
memory_manager.invoke({"messages": conversation_messages})
Semantic memory (facts/preferences) lives in a vector store for retrieval; episodic memory records what happened (events, outcomes); procedural memory captures how to do something (workflows the agent can replay). The graph queries these stores as needed, so a support agent can say "last time you had this issue, the fix that worked was X."
๐ง Knowledge Check
1. What does the checkpointer keyed by thread_id give you?
2. What does LangMem add beyond the checkpointer?
3. Which is NOT one of LangMem's memory types?