
LangChain Memory Explained: Short-Term vs Long-Term, LangGraph Persistence, and LangMem

LangChain memory is the set of mechanisms that let a LangChain chain or agent remember earlier interactions. Since LangChain 1.0, the official framing splits it into short-term memory (the message history of one conversation thread, persisted by a checkpointer) and long-term memory (facts, preferences and instructions shared across threads, persisted in a Store). The old ConversationBufferMemory family still exists, but only in the langchain-classic compatibility package. This guide covers how each layer works, how LangGraph persistence underpins both, what LangMem adds, where the built-in options stop, and how to add a persistent memory layer to a LangChain or LangGraph agent.
What is LangChain memory?
LangChain memory is any state that survives between model calls. In the original LangChain, that meant a memory object attached to a chain: ConversationBufferMemory kept every message, ConversationBufferWindowMemory kept the last k, ConversationSummaryMemory compressed older turns into a running summary, and ConversationEntityMemory tracked facts about named entities. All of them lived in the Python process, so a restart wiped the conversation, and none of them had a concept of users, threads or cross-conversation knowledge.
Those classes were deprecated in LangChain v0.3.1. The deprecation notice in the source now reads since="0.3.1", removal="2.0.0", points to langchain.agents.create_agent as the alternative, and adds: "For agents that need to remember prior interactions, use create_agent with checkpointing or the Store API." With LangChain 1.0 (released October 17, 2025) the legacy memory module left the langchain package and moved to langchain-classic, which LangChain says it will maintain for security vulnerabilities until December 2026. An earlier version of the deprecation notice targeted removal at 1.0.0; the classes were kept in langchain-classic instead, with the removal target moved to 2.0.0.
What replaced them is LangGraph persistence. create_agent, the LangChain 1.0 agent harness, is built on LangGraph, so every agent gets the same two primitives:
- A checkpointer that saves the graph state of one thread after every step. This is short-term memory: LangChain conversation memory for a single session, keyed by
thread_id. - A Store that saves arbitrary JSON documents under a namespace, readable from any thread. This is LangChain long-term memory: user profiles, preferences, extracted facts, and agent instructions.
Everything else, LangMem, Mem0, Zep, Cognee, sits on top of or beside these two primitives rather than replacing them. The LangChain docs describe the split in the memory overview: short-term memory is "thread-scoped memory" that "tracks the ongoing conversation by maintaining message history within a session", while long-term memory "stores user-specific or application-level data across sessions and is shared across conversational threads."
Short-term memory vs long-term memory in LangChain
The two layers differ in scope, storage, and what you put in them. The table below condenses the comparison from the LangGraph persistence docs and adds the practical consequences.
| Short-term memory | Long-term memory | |
|---|---|---|
| Also called | Thread-scoped memory, conversation memory | Cross-thread memory |
| Persisted by | Checkpointer (InMemorySaver, SqliteSaver, PostgresSaver) | Store (InMemoryStore, PostgresStore) |
| Unit of data | Graph state snapshot (messages plus any other state keys) | JSON document under (namespace, key) |
| Scope | One thread_id | Any thread; namespaces usually include a user or org ID |
| Written | Automatically after every graph step | Explicitly, from a node, a tool, or a background job |
| Read | Automatically at the start of each step | store.get, store.search (filter and optional semantic search) |
| Typical content | Chat history, uploaded files, retrieved documents, tool results | Preferences, facts about the user, few-shot examples, system prompt updates |
| Growth problem | Message list grows every turn; needs trimming or summarization | Needs extraction, deduplication, and conflict handling |
| Legacy equivalent | ConversationBufferMemory, ConversationSummaryMemory | ConversationEntityMemory (loosely); no real equivalent |
A useful rule: if the information is only relevant to the conversation currently in progress, it belongs in short-term memory. If a different conversation next week should know it, it belongs in long-term memory. Most production agents use both, compiled into the same graph.
How does short-term memory work in LangChain?
Short-term memory in LangChain is the agent's state, persisted by a checkpointer and keyed by thread_id. LangGraph writes a checkpoint after every super-step, so a thread can be resumed, inspected, rewound (time travel), or paused for human approval. The short-term memory guide shows the minimum: pass a checkpointer to create_agent and a thread_id in the config.
Which checkpointer should you use?
InMemorySaver (formerly MemorySaver) keeps checkpoints in RAM and loses them on restart; it is for development and tests. For anything that should survive a process restart, use a database-backed checkpointer:
SqliteSaverfromlanggraph-checkpoint-sqlite(from langgraph.checkpoint.sqlite import SqliteSaver) for local, single-machine persistence.PostgresSaverfromlanggraph-checkpoint-postgres(from langgraph.checkpoint.postgres import PostgresSaver) for production, withAsyncPostgresSaverfor async apps.- Community packages for MongoDB and Redis, listed on the checkpointers page.
Two operational notes from the persistence docs: PostgresSaver stores thread_id in a column of limited length, so keep IDs under 255 characters (a UUID is fine), and checkpoints accumulate over long conversations, so plan a retention policy or a periodic prune.
How do you stop the message list from growing forever?
A checkpointer persists history; it does not bound it. MessagesState with the add_messages reducer appends every turn, which produces the same token-cost curve the legacy buffer memory had. The docs list four ways to manage it:
- Trim before each model call with
trim_messages, keeping the last N tokens. - Delete messages from state permanently with
RemoveMessage. - Summarize older turns with
SummarizationMiddleware, which replaces them with a model-written summary once a token threshold is crossed. - Manage checkpoints directly:
graph.get_state(config)reads the latest snapshot,graph.get_state_history(config)lists earlier ones.
Summarization is the usual production choice because trimming silently drops facts:
If you need trimming without middleware, trim_messages from langchain_core.messages.utils takes strategy="last", max_tokens, a token_counter (for example count_tokens_approximately), and start_on/end_on so the trimmed window begins on a human message and never ends on a dangling tool call.
How does long-term memory work in LangChain?
Long-term memory in LangChain is a Store: a key-value document database with hierarchical namespaces and optional vector search. The long-term memory guide describes items as JSON documents organized under a custom namespace (like a folder) and a distinct key (like a file name). Namespaces are tuples, typically (user_id, "memories") or ("org", org_id, "preferences"), so one Store can isolate many users and purposes.
How do agents read and write the Store?
When you compile a graph or build an agent with store=..., LangGraph injects it into nodes and tools. Inside a tool, the recommended access path is ToolRuntime:
Writing works the same way with runtime.store.put(...). The docs recommend uuid7() from langchain_core.utils.uuid as the key for append-style memories so they sort by time.
How does semantic search over memories work?
By default store.search filters on content. Pass an index configuration with an embedding model and the Store also ranks items by vector similarity to a natural-language query:
Which Store should you use in production?
InMemoryStore is lost on restart. The production option in the docs is PostgresStore (from langgraph.store.postgres import PostgresStore, installed with langgraph-checkpoint-postgres), with AsyncPostgresStore for async graphs. It supports the same index option for semantic search backed by pgvector. The stores page lists the available implementations.
What kinds of long-term memory should you store?
The LangChain memory overview borrows three categories from cognitive science (via the CoALA paper) and they are a good checklist for what to extract:
- Semantic memory stores facts, either as one continuously updated profile document per user or as a growing collection of small memory documents. Profiles are compact but error-prone to update; collections have higher recall but need deduplication and search.
- Episodic memory stores experiences, usually as few-shot examples of past successful task completions that are retrieved into the prompt.
- Procedural memory stores instructions, most commonly the agent's own system prompt, which a reflection step rewrites based on feedback and saves back to the Store.
The docs also distinguish writing memories in the hot path (the agent calls a save-memory tool during the turn, which is transparent but adds latency) from writing in the background (a separate job extracts memories after the conversation, which keeps the main loop fast but needs a trigger schedule).
LangGraph memory: persistence, checkpointers and the Store
LangGraph memory is the implementation underneath everything above. LangChain's create_agent compiles to a LangGraph graph, so the memory semantics are LangGraph's, and anything written here applies equally to hand-built StateGraph workflows. The LangGraph persistence docs summarize it in one line: "LangGraph's persistence layer gives agents short-term memory through checkpointers and long-term memory through stores."
In a raw graph you attach both at compile time:
A few LangGraph-specific points matter for memory design:
- Checkpoints are per thread and per step.
graph.get_state(config)returns aStateSnapshotwith the values, the next node, and acheckpoint_id. Passing a specificcheckpoint_idin the config replays from that point, which is how time travel and human-in-the-loop resumption work. - Subgraphs have their own checkpoint namespace. A parent graph does not automatically see a subgraph's state updates; the docs recommend the Store for data that must cross graph boundaries.
- The Store is not graph state. It is read and written imperatively from nodes or tools and is never part of the checkpoint, so it does not grow the context window unless you put retrieved items into the prompt.
- Agent Server handles both automatically. When deploying on LangChain's hosted Agent Server, checkpointer and Store are provisioned for you; LangMem's docs note its Store integration is "available by default in all LangGraph Platform deployments."
The reference implementation for all of this is the langchain-ai/langgraph repository, which also hosts the checkpointer and Store base classes.
What is LangMem?
LangMem is LangChain's long-term memory SDK, released on February 18, 2025 (the first public PyPI release, 0.0.10, is dated that day). It adds extraction and management logic on top of the Store rather than a new storage layer. The langchain-ai/langmem repository describes three parts:
- A core memory API of functional primitives that work with any storage system.
- Memory tools for the hot path:
create_manage_memory_toollets the agent create, update or delete memories, andcreate_search_memory_toollets it search them. - A background memory manager that extracts, consolidates, and updates memories from completed conversations.
Wiring the tools into an agent takes a few lines. Namespaces can include a {user_id} template that LangMem fills from the runtime config:
LangMem is MIT-licensed. As of October 2026 the latest PyPI release is 0.0.30 (uploaded October 27, 2025), so it is still pre-1.0 and its own documentation examples use create_react_agent rather than create_agent. Its memory types follow the semantic, episodic and procedural split from the memory overview, and its prompt-optimization utilities implement procedural memory by rewriting system prompts from feedback. On latency, Mem0's LOCOMO evaluation (arXiv 2504.19413) measured LangMem search at p50 17.99 s and p95 59.82 s, which the paper's authors describe as impractical for interactive use; treat that as a vendor-run benchmark, but it is the only published number.
What are the limits of built-in LangChain memory?
Checkpointer plus Store plus LangMem covers a lot, and for many agents it is enough. The limits show up in specific situations.
Durable history is not extracted knowledge. Replacing ConversationBufferMemory with PostgresSaver gives you transcripts that survive restarts. It does not give you a user profile, a list of decisions, or a fact that was true last month but changed. Extraction is a separate step, and the built-in path leaves its quality to the LLM prompt you write or to LangMem's defaults.
The Store is flat. Items are JSON documents in namespaces. There is content filtering and vector similarity, but no relationships between items. "Which of this customer's projects did the engineer who left in March work on?" is a graph question, and answering it from a flat Store means retrieving many documents and letting the model join them.
No provenance. A Store item knows its namespace, key and value. It does not know which conversation, document or chunk produced it, so when an agent recalls something wrong there is no pointer back to the evidence.
No temporal validity. A memory is current or deleted. Keeping "role was X until June, Y after" requires modelling it yourself in the document schema and writing update logic that preserves the old value.
Conflicts are overwritten silently. If two conversations assert different facts, the later put wins, or two contradictory documents coexist and retrieval returns whichever is closer to the query. Nothing flags the contradiction.
Hot-path extraction costs latency; background extraction costs freshness. The docs are candid about this trade-off. Teams that want both low latency and fresh memory end up building a queue and a consolidation job, which is a second system to operate.
Memory is scoped to LangChain. A LangGraph Store is readable by LangGraph code. If the same user also talks to the team's Claude Code setup, a Cursor agent, or a non-LangChain service, those tools cannot see the memories without a custom API in front of the Store.
None of these are bugs; they are scope decisions. They are the reason a category of external memory layers exists.
How to add persistent memory to LangChain and LangGraph agents
The steps below work for either create_agent or a hand-built StateGraph, and they are vendor-neutral until the last one.
1. Find and retire legacy memory classes. Grep for ConversationBufferMemory, ConversationSummaryMemory, ConversationBufferWindowMemory and ConversationEntityMemory. On LangChain 1.x they import only from langchain_classic.memory, emit deprecation warnings, and will be removed in 2.0. Each becomes a checkpointer, a summarization middleware, or a Store namespace.
2. Add a database-backed checkpointer. Start with InMemorySaver in tests, then switch to SqliteSaver or PostgresSaver. Call checkpointer.setup() once against a fresh database. Pass a stable thread_id per conversation (one per chat session, support ticket, or workflow run) and keep it under 255 characters.
3. Bound the active context. Add SummarizationMiddleware with a token trigger that fits your model's window and cost target, or trim with trim_messages in a before_model hook. Measure tokens per turn before and after; this step is what prevents the legacy buffer-memory bill from coming back.
4. Add a Store and decide the namespaces. Compile with store=PostgresStore.... Choose a namespace convention up front, for example (org_id, user_id, "profile") for a single profile document and (org_id, user_id, "memories") for a collection. Enable the index option if you need semantic search.
5. Choose how memories get written. Options, roughly in order of effort: let the model call a save tool (LangMem's create_manage_memory_tool or your own @tool writing runtime.store.put); run a background extraction job after each conversation; or hand the conversation to an external memory engine that does extraction, structuring and storage for you.
6. Retrieve before the model call. In a before_model middleware or in the node itself, search the Store with the latest user message, format the top results into the system prompt, and keep the count small (the docs' examples use limit=3). Retrieval into the prompt is where long-term memory re-enters the context window, so budget for it in step 3.
7. Plug in an external memory layer if the built-in limits bite. If you need graph relationships, provenance, temporal facts, or memory shared with non-LangChain tools, add a memory engine as a set of tools alongside the checkpointer and Store rather than instead of them. The next two sections cover how to choose one and what the Cognee integration looks like.
8. Validate. Write a small eval: a fixed set of conversations, then questions whose answers depend on earlier threads. Check recall accuracy, tokens per turn, retrieval latency, and whether recalled facts can be traced to a source. Re-run it when you change the extraction prompt, the embedding model, or the memory layer.
How to choose a long-term memory layer for LangChain
Four options cover most teams. The right one depends on which built-in limit you actually hit.
| Option | What it is | Strengths | Trade-offs |
|---|---|---|---|
| Store + LangMem | LangChain's own Store with LangMem extraction tools | Zero new infrastructure if you already run Postgres; MIT; same mental model as the rest of LangGraph | Flat documents, no provenance or temporal model; pre-1.0; high search latency in the one published benchmark |
| Mem0 | Memory layer with a hosted platform and an open-source core | Simple add/search client, broad framework support, fast retrieval in its own benchmark | Hosted platform is not air-gappable; graph features are an add-on; see Mem0 alternatives |
| Zep | Memory service built on a temporal knowledge graph | Temporal facts, hosted operations handled for you | Primarily a cloud service; see Zep alternatives |
| Cognee | Open-source memory engine building a knowledge graph plus vector index | Graph structure, provenance to source chunks, temporal mode, self-host on Postgres or Neo4j, MCP server | You run the extraction pipeline, which costs LLM tokens on ingest |
Criteria that separate them in practice:
- Deployment model. If memory must stay inside your VPC or an air-gapped network, you need something you can self-host end to end: Store + LangMem on your own Postgres, or an open-source engine. Hosted tiers of any vendor, including LangChain's own Agent Server, are out.
- Shape of recall. Flat key-value with vector search is fine for preferences and facts about one user. Cross-entity questions, "who worked with whom on what", favour a graph.
- Provenance and audit. Regulated teams usually need every recalled fact to point at its source. Only engines that keep document and chunk references can do that.
- Time. If facts change and you need to answer "what was true in March", pick a layer with explicit validity windows.
- Latency budget. Hot-path recall should return in well under a second. Look for p95 numbers, not p50, and measure on your own data.
- Reach beyond LangChain. If the same memory should serve an IDE agent or an MCP client, the layer needs an API or MCP server independent of LangGraph.
- Cost model. Self-hosted options cost infrastructure and ingest tokens; hosted ones charge per token or per memory operation. Price the ingest side, since extraction is where tokens go.
The broader comparison of memory architectures is in LLM memory explained, and the agent-specific view is in agent memory.
How Cognee adds long-term memory to LangChain and LangGraph
Cognee is an open-source memory engine (Apache 2.0, topoteretes/cognee, about 32.5k GitHub stars) that turns raw text, documents and code into a knowledge graph plus a vector index. For LangChain and LangGraph it ships as the cognee-integration-langgraph package, which exposes Cognee as tools that sit next to the checkpointer and Store rather than replacing them. The package requires Python 3.10+ and its tools are async, so agents must be run with await agent.ainvoke().
get_sessionized_cognee_tools(session_id) returns an add_tool and a search_tool bound to that session. add_tool ingests text, documents or code; Cognee's pipeline chunks it, extracts entities and relationships with an LLM, summarizes, embeds, and commits the result as graph nodes and edges plus vectors. search_tool answers natural-language queries against that graph and returns the context the answer was built from. Each session ID gets its own node set, so two users' memories never mix, and passing include_persist_tool=True adds a third tool that promotes a session's memory into the permanent shared graph.
Mapped to the limits above:
- Graph, not flat documents. Entities and relationships are first-class, so multi-hop questions resolve by traversal rather than by retrieving many documents and hoping the model joins them.
- Provenance. Every entity and relation carries a pointer to the document and chunk it came from, and recall returns those references with the facts.
- Temporal mode. Superseded facts are kept with validity windows instead of being deleted, so "What was John's role in 2025?" stays answerable after his role changes.
- Contradictions are recorded, not overwritten. With
CONTRADICTION_DETECTION=trueset during ingestion, conflicting facts get acontradictsedge carrying both statements, a reason, and a confidence score. - Self-hosted or managed. Cognee runs on a single Postgres (with pgvector), or on Neo4j, Kuzu, FalkorDB or LanceDB, with any LLM provider including local models through Ollama. The 1.0 API is four verbs:
remember,recall,improve,forget. - Shared beyond LangChain. The same engine exposes an MCP server (
cognee/cognee-mcpimage; stdio, Streamable HTTP and SSE), so memory a LangGraph agent writes is readable from Claude Code, Cursor or Codex. AMem0Sourceadapter imports existing Mem0 memories.
Pricing, as of October 2026: self-hosting is free; Cognee Cloud has a free tier with 1M tokens and one workspace, and a Standard tier at $1.00 per 1M tokens processed plus $5 per additional workspace, with Enterprise BYOC on request.
Next steps
Start with what LangChain gives you: a PostgresSaver for short-term memory, SummarizationMiddleware to bound it, and a PostgresStore with sensible namespaces for long-term memory. Add LangMem's tools if you want the agent to manage its own memories inside that Store. When you need relationships, provenance, temporal facts, or memory that other tools can read, add Cognee's add_tool and search_tool beside them with pip install cognee-integration-langgraph, or point a Cognee Cloud workspace at the same agent.
Related guides
- LLM Memory Explained: Types, Architectures, and How to Add It
- Agent Memory: How Agents Remember Across Sessions
- MCP Memory: What It Is and How to Give Any MCP Client Persistent Memory
- Best Mem0 Alternatives
- Best Zep Alternatives
- The Token Cost of Persistent AI Memory
FAQ
Is ConversationBufferMemory deprecated?
Yes. ConversationBufferMemory and the other legacy memory classes were deprecated in LangChain v0.3.1. With LangChain 1.0 (October 2025) they moved out of the langchain package into langchain-classic, where they import from langchain_classic.memory and emit a deprecation warning pointing to create_agent with checkpointing or the Store API. The current removal target in the source is 2.0.0, and LangChain says langchain-classic is maintained for security fixes until December 2026. New code should use a checkpointer for conversation memory and a Store for long-term memory.
What is the difference between short-term and long-term memory in LangChain?
Short-term memory is thread-scoped: the message history and other state of one conversation, saved automatically by a checkpointer and keyed by thread_id. Long-term memory is cross-thread: JSON documents saved in a Store under a namespace such as (user_id, "memories"), written explicitly by nodes, tools or background jobs, and readable from any conversation. Short-term memory needs trimming or summarization to stay inside the context window; long-term memory needs extraction, deduplication and conflict handling.
How does LangGraph store memory?
LangGraph stores memory through two persistence systems. A checkpointer writes a snapshot of the graph state after every step, indexed by thread_id and checkpoint_id; implementations include InMemorySaver, SqliteSaver and PostgresSaver. A Store writes application-defined JSON items under (namespace, key) outside the graph state, with content filtering and optional vector search; implementations include InMemoryStore and PostgresStore. Both are attached at compile time with builder.compile(checkpointer=..., store=...) or passed to create_agent.
What is LangMem?
LangMem is LangChain's open-source (MIT) SDK for long-term agent memory, released February 18, 2025. It provides create_manage_memory_tool and create_search_memory_tool, which let an agent save and search memories in a LangGraph Store during a conversation, plus a background memory manager that extracts and consolidates memories after the fact, and prompt-optimization utilities for procedural memory. It is still pre-1.0 (0.0.30 as of October 2026) and relies on the Store for persistence rather than providing its own database.
How do I add memory to a LangGraph agent?
For conversation memory, pass a checkpointer when compiling (builder.compile(checkpointer=PostgresSaver(...)) or create_agent(..., checkpointer=...)) and include {"configurable": {"thread_id": "..."}} in every invoke. For memory across conversations, also pass store=PostgresStore(...) and read or write it from nodes via runtime.store or from tools via ToolRuntime. Add SummarizationMiddleware so the thread does not outgrow the context window. For graph-structured, cited memory, add an external engine's tools, for example Cognee's get_sessionized_cognee_tools(session_id), to the agent's tool list.
Does LangChain memory persist across sessions?
Only if you use a database-backed backend. InMemorySaver and InMemoryStore live in process memory and are lost on restart. With SqliteSaver or PostgresSaver, a thread resumes across restarts whenever the same thread_id is supplied. With PostgresStore, items in a namespace are readable from any new thread, which is what makes memory persist across sessions for a user. External memory engines such as Mem0, Zep or Cognee persist in their own storage and are read through tools, so they also survive restarts and can be shared with non-LangChain clients.
Related reading





