How to Build an Agent with Memory Using LangMem

LangMem is LangChain’s dedicated memory library for AI agents, released in early 2025. It provides a structured system for managing the four types of agent memory — in-context working memory, episodic memory of past interactions, semantic memory of facts about the user and domain, and procedural memory of learned workflows — with explicit extraction, storage, and retrieval mechanisms. Rather than relying on the model to summarise its own context or implementing custom memory pipelines, LangMem provides a set of primitives that handle the memory lifecycle automatically. This guide covers the architecture, the key abstractions, and when LangMem adds meaningful value over simpler approaches.

Why a Dedicated Memory Library

Most agent frameworks treat memory as an afterthought — the conversation history is passed back to the model, and eventually you truncate it when it gets too long. This approach works for short sessions but breaks down for applications that need to remember things across days or weeks of interaction. LangMem is designed to solve this specifically: it provides tools for extracting durable facts from conversations, storing them in a structured external store, and retrieving the most relevant facts at the start of each new session. The key insight behind LangMem is that not everything in a conversation is worth remembering long-term — identifying what is worth storing (explicit preferences, important facts, successful strategies) and what is ephemeral (greetings, clarifications, intermediate reasoning) is a task that benefits from a dedicated system rather than ad-hoc logic.

The Memory Store

LangMem’s central abstraction is the InMemoryStore (for development) or the PostgresStore (for production), which provides a key-value store with namespace support and vector search capability. Memories are stored as documents in namespaces — typically scoped by user ID or session ID — and can be retrieved by exact key, by namespace listing, or by semantic similarity search. The store interface is deliberately simple: put, get, search, delete. The complexity is in the higher-level abstractions built on top of it — the extractors and managers that populate the store automatically from conversation content. Switching between the in-memory and Postgres backends requires no code changes, making development-to-production migration straightforward.

Memory Managers: Automatic Extraction

LangMem provides memory managers that observe conversations and automatically extract durable information for storage. The default manager uses a language model to identify facts worth storing — user preferences, important entities, stated goals — and writes them to the memory store with appropriate metadata. This extraction runs as a background process (after each conversation turn or at session end), so it does not add latency to the agent’s response. The extraction quality depends on the prompt given to the extractor model — LangMem provides sensible defaults but allows customisation for domain-specific extraction patterns. For example, a customer support agent might extract “the customer is on the Pro plan” and “the customer prefers email communication” from a support conversation, storing these as semantic memories retrievable in future sessions.

Figure 1 — LangMem memory lifecycle: extraction to retrieval

Conversation turns + tool calls Extractor (LLM identifies facts) Memory Store (namespaced KV + vectors) Retriever (injects into context) Session N Session N+1

Retrieval at Session Start

At the start of a new session, LangMem’s retrieval mechanism queries the memory store for facts relevant to the current context and injects them into the agent’s system prompt or the beginning of the conversation. The retrieval can use exact lookup (retrieve all memories in a user’s namespace), semantic search (find memories most similar to the current query), or a combination. The result is that the agent begins each new session with access to relevant facts from past interactions — user preferences, previously established context, prior task outcomes — without those facts needing to be re-stated by the user. For applications where users return repeatedly and expect the agent to remember them, this retrieval mechanism is what makes the agent feel persistent rather than amnesiac.

Procedural Memory and Prompt Optimisation

LangMem’s procedural memory component stores learned workflows and prompt refinements. When an agent consistently succeeds on a type of task using a specific approach, that approach can be extracted and stored as a procedural memory — retrieved and applied when similar tasks are encountered in future. More ambitiously, LangMem provides a Trustcall-based system for iterative prompt optimisation: the agent’s instructions are treated as mutable state, and successful task completions provide feedback signals that refine the instructions over time. This is an early implementation of “self-improving agents” — agents whose prompts become more effective with use. The feature is experimental but represents a meaningful direction: rather than requiring a human to manually iterate on prompts, the agent’s instructions evolve based on what actually works.

LangGraph Integration

LangMem is designed to integrate tightly with LangGraph, LangChain’s graph-based agent framework. Memory read (retrieval) and write (extraction) are nodes in the graph — placed at the start and end of the agent loop respectively. The memory store is accessible from any node via LangGraph’s state management. This integration means adding LangMem memory to an existing LangGraph agent is primarily a structural change — adding retrieval at the start and extraction at the end — rather than a rewrite. For agents already built in LangGraph, LangMem is the recommended path to adding persistent cross-session memory. For agents in other frameworks, LangMem can still be used but requires more manual integration work since the automatic graph hooks are not available.

When LangMem Adds Real Value

LangMem is worth the integration effort when personalisation and cross-session continuity are core to the application’s value proposition. A coding assistant that remembers your preferred patterns and project context across sessions is qualitatively more useful than one that starts fresh every time. A research assistant that builds an understanding of your interests over weeks of use provides compounding value that a stateless assistant cannot. A customer support agent that recalls previous interactions and doesn’t make users repeat themselves creates a noticeably better experience. For single-session or stateless applications — one-shot analysis, document processing, tool-use tasks with no ongoing relationship — LangMem’s overhead is not justified. The value is proportional to how much cross-session context improves the agent’s usefulness for the specific application.

Alternatives and When to Build Custom

LangMem is not the only way to add persistent memory to agents. Mem0 is an alternative memory library with a simpler API and less LangChain coupling — worth evaluating if you are not already using LangGraph. Zep provides managed agent memory as a service, handling storage and retrieval with minimal infrastructure ownership. For simple cases — storing a few user preferences and retrieving them at session start — a standard relational database with a lookup function is often simpler than adopting a memory library. LangMem’s value is specifically in the automatic extraction pipeline and the procedural memory features; if you only need basic fact storage and retrieval, those features may not justify the dependency. Start with the simplest approach that meets your persistence requirements, and migrate to LangMem when the extraction and self-improvement capabilities become relevant.

LangMem addresses a real gap in agent development: the machinery for extracting durable facts from conversations, storing them reliably, and retrieving them relevantly is non-trivial to build well. For LangGraph users building agents with meaningful personalisation requirements, LangMem provides that machinery with minimal additional code. The extraction quality, retrieval precision, and procedural memory features are genuinely useful capabilities that would each require significant custom engineering to replicate. Whether that engineering cost is worth paying depends entirely on how central cross-session memory is to your agent’s value proposition.

Memory Consolidation and Deduplication

One practical challenge with automatic memory extraction is accumulation of redundant or conflicting memories over time. If a user mentions their preference for concise responses in ten different sessions, a naive extractor stores ten copies of the same fact. LangMem addresses this with memory consolidation: before writing a new memory, the manager checks whether a similar memory already exists and updates it rather than creating a duplicate. Conflict resolution — handling cases where a new extraction contradicts an existing memory — uses recency as the default tiebreaker, with older memories updated or marked superseded when new information contradicts them. For long-running agents that accumulate months of interaction history, periodic consolidation runs (triggered manually or on a schedule) can compress the memory store by merging related facts and removing entries that have been superseded, keeping retrieval quality high as the store grows.

Leave a Comment