LangMem

repository·main·Indexed 23 days ago

https://github.com/langchain-ai/langmem

A library for AI agents to learn and adapt from interactions over time, providing prebuilt utilities for memory management and retrieval. It features native LangGraph integration, tools for extracting and searching long-term memory (Semantic, Episodic, and Procedural), and background memory extraction via create_memory_store_manager. Supports various storage backends including InMemoryStore and AsyncPostgresStore.

Tokens
37.1K
Snippets
60
Records
116
Agent score
80%

What's inside langmem

  1. LangMem API Reference Overview

    main

    The LangMem API is organized into four primary functional areas:

    1. Memory Management: Utilities for extracting and managing memories, including stateless extraction and stateful management using BaseStore.
    2. Memory Tools: Agent-facing tools that allow an LLM to interact with memory, such as storing, updating, and searching memories.
    3. Prompt Optimization: Tools for optimizing single prompts or entire multi-prompt systems.
    4. Utilities: Low-level utilities for namespace templating and remote/background execution of memory management tasks.
  2. Common namespace organization patterns

    main

    When designing memory isolation, you can structure namespaces using different levels of granularity. Common patterns include:

    1. Organization-level: Isolate memories for an entire company or group using an {org_id}.
    2. User within organization: Create a hierarchy where memories are nested under an organization and then a specific user (e.g., ("memories", "{org_id}", "{user_id}")). This allows for tools like create_search_memory_tool to potentially search across an entire organization if the namespace is configured at the {org_id} level.
    3. Categorical/Feature-level: Organize memories by type or specific agent context (e.g., ("agent_name", "memories", "{user_id}", "preferences")).
    # Organization-level
    tool = create_manage_memory_tool(
        namespace=("memories", "{org_id}")
    )
    app = create_react_agent("anthropic:claude-3-5-sonnet-latest", tools=[tool])
    app.invoke(
        {"messages": [{"role": "user", "content": "I'm questioning the new company health plan.."}]},
        config={"configurable": {"org_id": "acme"}}
    )
    
    # User within organization
    tool = create_manage_memory_tool(
        namespace=("memories", "{org_id}", "{user_id}")
    )
    # If you wanted to, you could let the agent
    # search over all users within an organization
    tool = create_search_memory_tool(
        namespace=("memories", "{org_id}")
    )
    app = create_react_agent("anthropic:claude-3-5-sonnet-latest", tools=[tool])
    app.invoke(
        {"messages": [{"role": "user", "content": "What's our policy on dogs at work?"}]},
        config={"configurable": {"org_id": "acme", "user_id": "alice"}}
    )
    
    # Categorical organization
    tool = create_manage_memory_tool(
        namespace=("agent_smith", "memories", "{user_id}", "preferences")
    )
    app = create_react_agent("anthropic:claude-3-5-sonnet-latest", tools=[tool])
    app.invoke(
        {"messages": [{"role": "user", "content": "I like dolphins"}]},
        config={"configurable": {"user_id": "alice"}}
    )
  3. LangMem Integration Patterns: Core API vs. Stateful Integration

    main

    LangMem's functionality is organized into two distinct layers depending on whether you want side-effect-free transformations or built-in persistence.

    1. Core API (Functional)

    These are pure functions that transform memory state without side effects. They do not depend on a specific database and can be used in any application.

    • Memory Managers: Used to extract, update, remove, or consolidate memories based on new information.
    • Prompt Optimizers: Used to update prompt rules and core behavior based on conversation history.

    2. Stateful Integration (LangGraph-based)

    These components build on top of the Core API and are designed for users of LangGraph Platform or LangGraph OSS. They use LangGraph's BaseStore to automatically persist changes.

    • Store Managers: Automatically persist extracted memories to a storage layer.
    • Memory Management Tools: Provide agents with direct tools to perform memory operations (e.g., saving or retrieving memories).
  4. Episodic Memory vs. Semantic Memory

    main

    In LangMem, memory is categorized by the type of information it captures:

    • Semantic Memory: Builds a knowledge base of facts (e.g., "Python is a programming language"). It answers the question "what".
    • Episodic Memory: Captures expertise and the reasoning process behind successful interactions (e.g., "explaining Python using snake analogies confused users, but comparing it to recipe steps worked well"). It answers the question "how".

    Use episodic memory to help agents:

    • Adapt teaching or communication styles based on what worked previously.
    • Learn from successful problem-solving approaches.
    • Build a library of proven techniques and reasoning chains.
  5. How memory namespaces and scoping work

    main

    Namespaces allow you to add scope to memories, acting like directories on a computer. This is essential for separating data between users, assistants, or organizations.

    If you use bracketed variables in a namespace (e.g., "{user_id}"), LangMem will dynamically replace them with values from the configurable field in the RunnableConfig at runtime.

    Common Organization Patterns:

    Organization PatternNamespace ExampleUse Case
    By user("memories", "{user_id}")Separate memories per user
    By assistant("memories", "{assistant_id}")An assistant may have memories that span multiple users
    By user & organization("memories", "{organization_id}", "{user_id}")Search across an organization while scoping per user
    Further subdivisions("memories", "{user_id}", "manual_memories")Organize different types of user data
  6. How memory tools and storage work together

    main

    LangMem provides two primary tools that interface with a LangGraph BaseStore:

    • create_manage_memory_tool(namespace): Enables the agent to extract and store relevant details from the conversation into the specified namespace.
    • create_search_memory_tool(namespace): Enables the agent to search for existing memories within the specified namespace that are semantically similar to the current context.

    Storage Options:

    • InMemoryStore: Keeps memories in process memory. Use this for development, but note that memories are lost on restart.
    • AsyncPostgresStore: A database-backed store suitable for production to persist memories across server restarts.

    Behavior: The agent decides autonomously when to use these tools. It uses the management tool to maintain consistency and the search tool to retrieve context from past interactions without requiring explicit user commands.

  7. How memory formation works: Conscious vs. Subconscious

    main

    LangMem supports two primary patterns for forming memories, allowing you to balance user experience (latency) with the depth of learning (accuracy/patterns).

    Conscious Formation (Hot Path)

    This happens during the conversation. The agent actively decides to save or update memories as it interacts with the user.

    • Pros: Immediate updates to context; easy to implement using agent tools.
    • Cons: Higher latency for the user; adds complexity to the agent's decision-making.
    • Use Case: Critical context updates that must be known for the very next turn.

    Subconscious Formation (Background)

    This happens after the conversation or during periods of inactivity. An LLM reflects on the interaction to extract insights.

    • Pros: No impact on response latency; better at finding long-term patterns and summaries.
    • Cons: Updates are delayed.
    • Use Case: Pattern analysis, long-term summaries, and ensuring high recall of extracted information.
  8. Use Memory Tools for agentic memory management

    main

    LangMem provides specialized tools that allow an agent to interact with its long-term memory. These tools are built on top of LangGraph's BaseStore to ensure persistence.

    Key tools include:

    • create_manage_memory_tool: Allows an agent to manage (add, update, or delete) memories.
    • create_search_memory_tool: Allows an agent to search through stored memories to retrieve relevant context.
  9. How long-term memory works in LangMem

    main

    LangMem enables agents to retain important information across multiple conversations. The core memory operation follows a three-step pattern:

    1. Input: Accept conversation(s) and the current memory state.
    2. Processing: Prompt an LLM to determine how to expand or consolidate the existing memory state based on the new information.
    3. Output: Respond with the updated memory state.

    When designing a memory system, you should define:

    • Content Type: What the agent should learn (e.g., facts, event summaries, or rules/style).
    • Formation Logic: When and who (which process) should form the memories.
    • Storage Strategy: Where memories are stored (e.g., directly in the prompt or in a semantic store), which dictates how they are recalled.
  10. How create_memory_manager and create_memory_store_manager differ

    main

    LangMem provides two primary patterns for extracting and enriching memory collections:

    1. create_memory_manager: A functional API where the developer is responsible for managing storage and updates. It returns instructions (like ExtractedMemory or RemoveDoc) that the developer must then apply to their own data store.
    2. create_memory_store_manager: A managed API that handles the entire lifecycle—searching for existing memories, performing upserts, and executing deletes—directly within a configured LangGraph BaseStore.
  11. Difference between Store and Checkpointer

    main

    It is important to distinguish between the Store and the Checkpointer (e.g., MemorySaver) when building agents:

    • Store (BaseStore): Used for long-term, cross-thread memory. It allows you to store and retrieve information according to a preferred hierarchy (namespaces). It persists even when a conversation (thread) ends.
    • Checkpointer (MemorySaver): Used for short-term memory. It tracks the state of the agent/graph within a specific "thread" (conversation history). This ensures individual conversations remain independent and allows for durable execution of a single session.