# LangMem

> Long-term memory tools for LangGraph agents that learn from every conversation

- Category: Agents & Automation / Agent Memory
- Language: Python
- Repository: https://github.com/langchain-ai/langmem
- Website: https://langchain-ai.github.io/langmem/
- Canonical page: https://ai-tldr.dev/tools/langmem/

## In short

LangMem is a memory SDK from the LangChain team that helps agents remember information across conversations. It gives you tools to pull important details out of a chat, store them, and search them later, so an agent can personalize its replies and stay consistent between sessions. It sits in the Agent Memory category of the Agents & Automation tools and is open-source, written in Python.

## Overview

LangMem is a memory SDK from the LangChain team that helps agents remember information across conversations. It gives you tools to pull important details out of a chat, store them, and search them later, so an agent can personalize its replies and stay consistent between sessions.

It is aimed at developers building agents who need long-term memory rather than a fresh start on every request. You can use its core memory API with any storage system, or rely on its built-in integration with LangGraph's long-term memory store.

Within the agent-memory space, LangMem sits as a memory framework. It supports both "hot path" memory (tools the agent calls during a conversation) and background memory management that extracts and consolidates knowledge automatically.

## What it does

- Core memory API that works with any storage backend, not just LangGraph
- Memory management tools (create_manage_memory_tool, create_search_memory_tool) agents call during a conversation
- Background memory manager that extracts, consolidates, and updates knowledge automatically
- Native integration with LangGraph's long-term memory store, available in LangGraph Platform deployments
- Prompt-refinement tooling to optimize agent behavior over time

## Getting started

Install LangMem with pip, set an LLM provider API key, then wire its memory tools into a LangGraph agent.

1. Install LangMem

   Install the package from PyPI.

```bash
pip install -U langmem
```

2. Set your LLM API key

   Configure an API key for a supported LLM provider in your environment.

```bash
export ANTHROPIC_API_KEY="sk-..."  # Or another supported LLM provider
```

3. Create an agent with memory

   Add the manage- and search-memory tools to a LangGraph react agent backed by a store.

```python
from langgraph.prebuilt import create_react_agent
from langgraph.store.memory import InMemoryStore
from langmem import create_manage_memory_tool, create_search_memory_tool

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

agent = create_react_agent(
    "anthropic:claude-3-5-sonnet-latest",
    tools=[
        create_manage_memory_tool(namespace=("memories",)),
        create_search_memory_tool(namespace=("memories",)),
    ],
    store=store,
)
```

4. Store and recall a memory

   Chat normally; the agent decides what to store and searches its memory when asked.

```python
agent.invoke(
    {"messages": [{"role": "user", "content": "Remember that I prefer dark mode."}]}
)

response = agent.invoke(
    {"messages": [{"role": "user", "content": "What are my lighting preferences?"}]}
)
print(response["messages"][-1].content)
```

## When to use it

- Give a chat agent persistent memory of a user's preferences across separate sessions
- Let an agent record and search facts in the hot path during an ongoing conversation
- Run a background process that consolidates and updates an agent's knowledge over time
- Add long-term memory to an existing LangGraph application using its storage layer
