LangChain Module 5: Memory, Threads, and Long Conversations
Introduction
Left alone, an agent forgets everything between calls. In LangChain v1, short-term memory is handled by a checkpointer that saves the conversation state, keyed by a thread ID. This module adds memory to an agent and shows how to keep long conversations manageable.
Prerequisites
- Module 4 completed
Step-by-Step Guide
Step 1: Add a checkpointer
Pass a checkpointer to create_agent. InMemorySaver keeps conversations in memory, which is perfect for learning and tests but is lost when the program stops.
from langchain.agents import create_agent
from langgraph.checkpoint.memory import InMemorySaver
agent = create_agent(
model=model,
tools=[],
system_prompt="You are a friendly assistant.",
checkpointer=InMemorySaver(),
)Step 2: Use a thread ID to continue a conversation
Every call with the same thread_id continues the same conversation. The configuration is passed as the second argument.
config = {"configurable": {"thread_id": "user-1"}}
agent.invoke(
{"messages": [{"role": "user", "content": "Hi! My name is Bob."}]}, config
)
reply = agent.invoke(
{"messages": [{"role": "user", "content": "What's my name?"}]}, config
)
print(reply["messages"][-1].text) # mentions BobStep 3: Keep conversations separate
A different thread_id starts a clean conversation with no shared history. Use one thread per user session or per conversation, and never reuse a thread across different users.
other = {"configurable": {"thread_id": "user-2"}}
agent.invoke(
{"messages": [{"role": "user", "content": "What's my name?"}]}, other
) # does not know BobStep 4: Persist memory in production
For real applications, use a database-backed checkpointer so conversations survive restarts. LangGraph provides SQLite and Postgres checkpointers. The Postgres one needs its own packages, installed with pip install -U langgraph-checkpoint-postgres "psycopg[binary]", and a setup call to create the tables.
from langgraph.checkpoint.postgres import PostgresSaver
DB_URI = "postgresql://user:password@localhost:5432/mydb"
with PostgresSaver.from_conn_string(DB_URI) as checkpointer:
checkpointer.setup() # creates the tables
agent = create_agent(model=model, tools=[], checkpointer=checkpointer)Step 5: Control long conversations with summarization
A growing history costs more and eventually exceeds the context window. SummarizationMiddleware condenses older messages once a size threshold is reached while keeping recent ones. Use a smaller, cheaper model for the summaries.
from langchain.agents.middleware import SummarizationMiddleware
agent = create_agent(
model=model,
tools=[],
checkpointer=InMemorySaver(),
middleware=[
SummarizationMiddleware(
model="anthropic:claude-haiku-5-5",
trigger=("tokens", 4000),
keep=("messages", 20),
)
],
)FAQs
- Is a checkpointer long-term memory?
- It is short-term memory for one thread's conversation. Facts that should persist across conversations, such as user preferences, belong in a separate store that you read from and write to deliberately.
- Why did my agent forget everything after a restart?
- InMemorySaver holds data in process memory only. Use a database-backed checkpointer such as Postgres for persistence.
Summary
A checkpointer plus a thread_id gives an agent conversation memory. Use separate threads per conversation, a database checkpointer in production, and summarization middleware to keep long histories affordable.
Comments
Log in or create a free account to join the discussion.