Skip to main content

Prebuilt utilities for memory management and retrieval.

Project description

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

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
store = InMemoryStore() # (2)

# 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:

Requirements

  • Python 3.11+
  • Access to a supported LLM provider (Anthropic, OpenAI, etc.)
  • Optional: a LangGraph BaseStore implementation for persistent storage (for the stateful primitives); if you're deploying on LangGraph Platform, this is included without any additional configuration.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langmem-0.0.5rc12.tar.gz (46.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langmem-0.0.5rc12-py3-none-any.whl (51.2 kB view details)

Uploaded Python 3

File details

Details for the file langmem-0.0.5rc12.tar.gz.

File metadata

  • Download URL: langmem-0.0.5rc12.tar.gz
  • Upload date:
  • Size: 46.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.29

File hashes

Hashes for langmem-0.0.5rc12.tar.gz
Algorithm Hash digest
SHA256 199ae8eb3616cef2ee82d908a68a26455b8c50979b4a5c15dc646c6f7eafd4a4
MD5 374a7a27aa4c86f03a88df312d9d9289
BLAKE2b-256 69430afc7fe2378ceeedea7ad688eac08f5b32bccb2611de7dc776e7095cef0c

See more details on using hashes here.

File details

Details for the file langmem-0.0.5rc12-py3-none-any.whl.

File metadata

File hashes

Hashes for langmem-0.0.5rc12-py3-none-any.whl
Algorithm Hash digest
SHA256 c0fb6bf5b9da992933932eae4d036f3ece8e98ede40501c8a496bb31a81f828a
MD5 00d62e8779b3324e544fa18d742ff775
BLAKE2b-256 9cf67c06cdbc421aecc1319a9f2fe159a8938dff55f19199a3175a53b1d0394d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page