Skip to main content
Back to Blog
AI/MLProgramming LanguagesDatabases
5 April 20269 min readUpdated 5 April 2026

Enhancing AI Agents with LangMem SDK for Long-Term Memory

Introduction Large language model (LLM) powered AI agents often face a significant limitation: their memory is restricted. Standard LLMs can only retain information within the c...

Enhancing AI Agents with LangMem SDK for Long-Term Memory

Introduction

Large language model (LLM)-powered AI agents often face a significant limitation: their memory is restricted. Standard LLMs can only retain information within the context window, which is essentially the current conversation or chat history. Once this session ends or the token limit is reached, the information is lost, resulting in stateless interactions. The LangMem SDK addresses this challenge by implementing persistent long-term memory.

LangMem allows AI agents to accumulate knowledge over time, remembering past interactions, key details, preferences, and more across multiple sessions. This article explores what LangMem is, the benefits of long-term memory, LangMem’s functionality, and its application in your projects. Additionally, it compares LangMem’s performance with other solutions. By the conclusion, you'll understand how to leverage the LangMem SDK to develop advanced AI agents equipped with memory.

Key Takeaways

  • Stateful Agents: LangMem transforms traditional stateless LLM agents into systems capable of retaining information across sessions.
  • Diverse Memory Types: The SDK supports semantic (facts), episodic (past interactions), and procedural (behavior rules) memory through a unified API.
  • LLM-Driven Memory Management: A Memory Manager evaluates conversations to decide what to store, update, or delete, consolidating knowledge over time.
  • Backend-Agnostic Storage: LangMem integrates with various storage solutions like vector databases, key-value stores, and Postgres through a flexible interface.
  • Planning for Production: Effective use of LangMem requires consideration of namespacing, pruning, retrieval optimization, and cost management for scalable systems.

Illustration for: - Stateful Agents: LangMem tra...

What Is LangMem SDK?

The LangMem SDK is an open-source toolkit designed to provide long-term memory capabilities to AI agents. It functions as a memory store, accompanied by mechanisms to store, update, and retrieve information from this store during interactions with users. Compatible with any language model and agent framework, LangMem extracts relevant data from conversations and retrieves appropriate memories when needed. Operating as a lightweight Python library, it seamlessly integrates with any backend or memory store.

Illustration for: The LangMem SDK is an open-sou...

With LangMem, an agent can recall facts, preferences, past events, and even adapt its behavior based on feedback. For instance, if a virtual assistant learns your name or that you prefer "dark mode," it can store this information and use it in future interactions.

LangMem supports three types of memories:

  • Semantic Memory: Facts and data such as user information or knowledge triples.
  • Episodic Memory: Past experiences or events, typically stored as summaries.
  • Procedural Memory: Learned behaviors or instructions that influence agent actions.

Why Long-Term Memory Matters for AI Agents

By endowing agents with persistent memory, several enhancements are possible:

  • Continuous Context: Agents can maintain a conversation’s context over time, avoiding the need to reintroduce previous information. A customer support bot could remember a user's past issues.
  • Personalization: Agents can tailor responses based on user preferences and profile info. For example, an AI tutor could adapt its teaching style based on past student struggles.
  • Learning from Experience: Agents can learn and adapt from their actions, refining strategies based on past successes and failures.
  • Task Continuity: Memory allows an agent to recall task-related information, crucial for complex tasks that span multiple sessions.
  • Reduced Prompt Size: With long-term memory, agents only retrieve relevant information, optimizing the use of the context window and reducing costs.

Illustration for: - Continuous Context: Agents c...

Architecture and Technical Overview

LangMem's architecture consists of multiple layers that integrate with the core agent logic:

  1. Agent Framework Layer: This layer involves the agent interacting with the language model. The agent must be configured to utilize LangMem’s memory tools.

  2. Memory Manager Core (LLM-powered): This component analyzes conversation data to generate memory entries, deciding what to store, update, or delete.

  3. Memory Storage Layer: LangMem expects a backend memory store to manage memory entries. This layer functions as the long-term memory database.

  4. LangGraph Integration (Optional): When used with LangChain’s LangGraph, additional services like checkpointing and BaseStores are available.

Data Flow:

  • During a conversation, the agent processes user input and may invoke the manage_memory tool to decide what to store.
  • Later, the agent can use the search_memory tool to retrieve relevant stored memories for context or queries.

Integration Guide

1) Python Packages

Install the necessary packages for using LangMem with Python:

pip install -U langmem langchain langgraph langchain-openai openai

For persistent memory with Postgres, install:

pip install -U "psycopg[binary,pool]"

2) Provider Credentials

LangMem requires an external LLM, which must be configured separately. For example, with OpenAI:

import os, getpass
os.environ["OPENAI_API_KEY"] = getpass.getpass("Paste OPENAI_API_KEY: ").strip()

Step 1 — Import the Updated Components

Use the following imports to create an agent and memory store:

from langchain.agents import create_agent
from langgraph.store.memory import InMemoryStore
from langmem import create_manage_memory_tool, create_search_memory_tool

Step 2 — Create a Memory Store (Demo Mode)

For development, use an in-memory vector index:

store = InMemoryStore(
   index={
       "dims": 1536,
       "embed": "openai:text-embedding-3-small",
   }
)

Step 3 — Define Memory Tools With User-Scoped Namespaces

Namespaces help prevent memory leakage across users:

Memory write tool (manage memory)

manage_memory = create_manage_memory_tool(
   namespace=("memories", "{user_id}"),
   instructions=(
       "Store stable user facts and preferences (name, role, long-running projects, UI preferences). "
       "Avoid storing sensitive data unless the user explicitly requests it."
   ),
)

Memory read tool (search memory)

search_memory = create_search_memory_tool(
   namespace=("memories", "{user_id}"),
   instructions=(
       "When questions depend on prior info (preferences, identity, previous tasks), search memory first "
       "and use the results in the response."
   ),
)

Step 4 — Create the Agent

Integrate everything into an agent:

agent = create_agent(
   model="gpt-4o-mini",                 # choose your model
   tools=[manage_memory, search_memory],
   store=store,
)

Production Upgrade — Persistent Memory With Postgres

For persistent storage, use a Postgres database:

from langgraph.store.postgres import PostgresStore
store = PostgresStore.from_conn_string("postgresql://user:password@host:5432/dbname")
store.setup()  # run once

Performance & Scaling Considerations

While enhancing agents with long-term memory offers substantial benefits, it introduces new performance and scaling challenges:

  • Memory Growth and Pruning: Accumulating many memory entries can slow retrieval. Use pruning and compression policies to manage this.
  • Retrieval Efficiency: Large memory stores can increase latency. Use indexed databases and monitor retrieval times.
  • Context Window Usage: Retrieve only essential information to manage token usage effectively.
  • Memory Scope and Privacy: Use namespaces to prevent data leaks across users.
  • Scaling the LLM for Memory Operations: Balance model choice with cost and quality of memory extraction.

Comparison With Alternatives

Here’s a comparison of various methods for integrating long-term memory into AI agents:

  1. Custom Memory Solutions: Offers maximum control but requires significant effort and maintenance.

  2. Other Memory SDKs/Tools: May align well with existing stacks but vary in feature set and maturity.

  3. LangMem SDK: Provides a fast integration path with structured memory management, ideal for those using LangChain.

FAQs

  • Does LangMem include its own language model? No, it requires an external LLM provider.
  • Can LangMem work without LangChain? Yes, it can integrate with custom-built systems.
  • Is memory automatically persistent? Persistence depends on the chosen storage backend.
  • How does LangMem prevent memory leakage between users? It uses namespaces to separate user data.
  • Does long-term memory increase costs? Yes, due to additional storage and retrieval processes.

Conclusion

LangMem allows AI agents to transition from being stateless to stateful, retaining user information and task history across interactions. It provides a memory manager driven by LLMs and a flexible storage layer. This SDK simplifies the process of creating agents that learn and improve over time, ensuring scalability and manageability.