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."

Key idea: LangGraph's checkpointer is the short-term memory backbone (thread-scoped), and LangMem extends it to long-term memory: distilled facts, preferences, and learned procedures stored in a vector store, written by the agent itself as it works.

The three memory layers

LayerScopeMechanismExample
ContextOne runMessages in state"Summarize this document"
Short-term (thread)One conversationCheckpointer keyed by thread_idMulti-turn chat, multi-step task
Long-term (user/agent)Across all conversationsLangMem 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."

MEMORY LAYERS Context messages in state one run dies with the run Short-term checkpointer + thread_id one conversation survives restarts Long-term (LangMem) semantic / episodic / procedural across all conversations vector store + Store API
Pitfall: Long-term memory is a sharp knife. An agent that writes everything it "learns" will accumulate garbage, drift, and privacy violations. Scope namespaces tightly (per user), curate with the memory manager, and give users a way to view/delete what the agent remembers.
Rule of thumb: Start with the checkpointer (short-term) โ€” it's one argument and covers 80% of "memory" needs. Add LangMem only when the agent must carry knowledge across sessions or between users.

๐Ÿง  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?

Further Reading