Skip to main content

LangMem

LangMem helps agents learn and adapt from their interactions over time.

It provides tooling to extract important information from conversations, optimize agent behavior through prompt refinement, and maintain long-term memory.

It offers both functional primitives you can use with any storage system and native integration with LangGraph's storage layer.

This lets your agents continuously improve, personalize their responses, and maintain consistent behavior across sessions.

Key features

  • 🧩 Core memory API that works with any storage system
  • 🧠 Memory management tools that agents can use to record and search information during active conversations "in the hot path"
  • ⚙️ Background memory manager that automatically extracts, consolidates, and updates agent knowledge
  • ⚡ Native integration with LangGraph's Long-term Memory Store, available by default in all LangGraph Platform deployments

Installation

pip install -U langmem

Configure your environment with an API key for your favorite LLM provider:

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

Creating an Agent

Here's how to create an agent that actively manages its own long-term memory in just a few lines:

# Import core components (1)
from langgraph.prebuilt import create_react_agent
from langgraph.store.memory import InMemoryStore
from langmem import create_manage_memory_tool, create_search_memory_tool

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

# Create an agent with memory capabilities (3)
agent = create_react_agent(
    "anthropic:claude-3-5-sonnet-latest",
    tools=[
        # Memory tools use LangGraph's BaseStore for persistence (4)
        create_manage_memory_tool(namespace=("memories",)),
        create_search_memory_tool(namespace=("memories",)),
    ],
    store=store,
)
  1. The memory tools work in any LangGraph app. Here we use create_react_agent to run an LLM with tools, but you can add these tools to your existing agents or build custom memory systems without agents.

  2. InMemoryStore keeps memories in process memory—they'll be lost on restart. For production, use the AsyncPostgresStore or a similar DB-backed store to persist memories across server restarts.

  3. The memory tools (create_manage_memory_tool and create_search_memory_tool) let you control what gets stored. The agent extracts key information from conversations, maintains memory consistency, and knows when to search past interactions. See Memory Tools for configuration options.

Then use the agent:

# Store a new memory (1)
agent.invoke(
    {"messages": [{"role": "user", "content": "Remember that I prefer dark mode."}]}
)

# Retrieve the stored memory (2)
response = agent.invoke(
    {"messages": [{"role": "user", "content": "What are my lighting preferences?"}]}
)
print(response["messages"][-1].content)
# Output: "You've told me that you prefer dark mode."
  1. The agent gets to decide what and when to store the memory. No special commands needed—just chat normally and the agent uses create_manage_memory_tool to store relevant details.

  2. The agent maintains context between chats. When you ask about previous interactions, the LLM can invoke create_search_memory_tool to search for memories with similar content. See Memory Tools to customize memory storage and retrieval, and see the hot path quickstart for a more complete example on how to include memories without the agent having to expliictly search.

The agent can now store important information from conversations, search its memory when relevant, and persist knowledge across conversations.

Next Steps

For more examples and detailed documentation:

Metadata

Release files for langmem 0.0.26

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for langmem 0.0.26
File Size Uploaded
langmem-0.0.26.tar.gz 211.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for langmem 0.0.26
File Interpreter ABI Platform
langmem-0.0.26-py3-none-any.whl Python 3 none any Details

Total release size: 277.2 kB

Release files / langmem-0.0.26.tar.gz

Download URL langmem-0.0.26.tar.gz
Size 211.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3c28641272eef7251420cce45e5c142ff17a665ee504fe392801f820c323c61a
BLAKE2b-256 checksum
How to use checksums
3de986c4107edb6008ed7b88029edebf775bc41917a336bb902966e95b8a0212
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.3

Release files / langmem-0.0.26-py3-none-any.whl

Download URL langmem-0.0.26-py3-none-any.whl
Size 66.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6c35fd8f442d649543fd13fc55964b63b6e6cd7a37a150c0fc5ef20e096f6433
BLAKE2b-256 checksum
How to use checksums
6ff1fb2be7053e40ac20dcb920e53f0ce31de9a9e8cff9a01d4a6132346598fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.6.3

Release history Release notifications | RSS feed

0.0.30

2 release files

0.0.29

2 release files

0.0.27

2 release files

This release

0.0.26 This release

2 release files

0.0.25

2 release files

0.0.24

2 release files

0.0.23

2 release files

0.0.22

2 release files

0.0.17

2 release files

0.0.16

2 release files

0.0.14

1 release file

0.0.13

2 release files

0.0.12

1 release file

0.0.11

2 release files

0.0.10

2 release files

0.0.9

2 release files

0.0.8

2 release files

0.0.6

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page