# langchain-ai/langmem architecture

> Architecture diagram of the GitHub repository langchain-ai/langmem, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/langchain-ai/langmem
- Repository: https://github.com/langchain-ai/langmem

## Overview

##### Project Type and Purpose
- LangMem is clearly a library that supports agents in handling memory. It provides core memory APIs, tools for memory management such as create_manage_memory_tool and create_search_memory_tool, and integrations with both in-process (InMemoryStore) and DB-backed (AsyncPostgresStore) stores.
- The README outlines usage examples, integration points, and key features such as “Background memory manager” and “Memory management tools”, which indicate that the library’s primary function is to capture, consolidate, and search information from conversations.

##### File Structure to Identify Key Components and Modules
- Top-Level Directories and Files:
- docs: Contains all documentation, guides (like “hot_path_quickstart”, “background_quickstart”), conceptual guides, API reference, and even templates for generating docs.
- src: Houses the core code in the “src/langmem” package.
- tests: Contains test cases including end-to-end and unit tests (e.g., test_summarization.py, test_docstring_examples.py).
- examples: Provides example Jupyter notebooks and standalone examples demonstrating how to use the library.
- Within the src/langmem directory, notice several submodules:
- graphs: Contains modules related to graph-based memory management (e.g., auth, prompts, semantic).
- knowledge: Deals with extracting relevant information from conversations.
- prompts: Manages handling, layering, optimization, and different types (stateful/stateless) of prompts.
- short_term: Handles the summarization logic for short-term memory.
- utils, errors, reflection: Additional support functions and error handling.
- CI/CD Related Files (.github/workflows) and configuration files (pyproject.toml, pytest.ini, Makefile) emphasize best practices in testing and deployment.

##### Main Components and Their Relationships
a. Memory Tools (Core API):
- The memory tools (create_manage_memory_tool and create_search_memory_tool) serve as the primary APIs for storing and retrieving memory.
- They serve as the “glue” between agents (built with LangGraph or custom ones) and the internal memory modules.
b. Memory Store Abstraction:
- The library uses storage layers like InMemoryStore and potentially persistent stores (like AsyncPostgresStore). The memory store is abstracted so that any compatible storage backend can be used.
c. Memory Processing Pipelines:
- The “knowledge” module is in charge of extracting important details from a conversation.
- The “prompts” module provides utilities for prompt handling (e.g., optimization, layered prompts) and may interact with external LLMs.
- The “short_term” module contains summarization functionality to manage active context.
d. Agent Interface and External Integration:
- The README example shows how an agent (created using LangGraph’s create_react_agent) integrates these tools. The agent sends user messages to the memory management tools, which in turn extract, store, or retrieve memory as needed.
- The use of external API keys (e.g., ANTHROPIC_API_KEY) indicates that communication with external LLM providers is critical.

##### Highlighting Architectural Patterns and Design Principles
- Modularity: Each component (memory extraction, prompt management, memory storage) is separated into submodules, emphasizing separation of concerns.
- Abstraction: The use of storage abstractions (InMemoryStore versus DB-backed stores) allows the library to be used flexibly in different environments.
- Extensibility: With the design centered on “tools” that can plug into any LangGraph app or be integrated into custom agents, LangMem is built to be extended or configured based on specific needs.
- Integration-centric: The library hinges on integration with external agents and LLM providers, ensuring that data flows reliably between in-memory processing and persistent storage.

##### Summarizing Recommendations
- Drill down into each module (as seen in src/langmem) to illustrate responsibilities, inter-module communication, and integration points.
- Separation of tools (for the “hot path” immediate memory operations) and background processing (consolidating and updating agent knowledge).
- Finally, note any CI/CD pipelines, advanced documentation structure, and testing directories just to show best practices in quality and deployment even if they’re not central to runtime architecture.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Agent Layer
    A["Agent (LangGraph)"]:::agent

    %% Memory Tools Layer
    MT["Memory Tools API"]:::memoryAPI

    %% Memory Processing Modules
    subgraph "Memory Processing Modules"
        KE["Knowledge Extraction Module"]:::processing
        PM["Prompt Management Module"]:::processing
        GM["Graph-Based Memory Management"]:::processing
        ST["Short-Term Memory Summarization"]:::processing
    end

    %% Support Modules
    subgraph "Support Modules"
        Utils["Utility Functions"]:::support
        Err["Error Handling Support"]:::support
    end

    %% Memory Store and External Dependencies
    subgraph "Storage & External Dependencies"
        MS["Memory Store Abstraction"]:::store
        LLM["LLM Provider (Anthropic/Claude)"]:::external
    end

    %% Connections
    A -->|"calls"| MT
    MT -->|"uses"| KE
    MT -->|"uses"| PM
    MT -->|"uses"| GM
    MT -->|"uses"| ST
    MT -->|"stores_to"| MS
    MT -->|"supported_by"| Utils
    MT -->|"supported_by"| Err

    KE -->|"saves"| MS
    PM -->|"saves"| MS
    GM -->|"saves"| MS
    ST -->|"saves"| MS

    PM -->|"calls"| LLM

    %% Click Events
    click MT "https://github.com/langchain-ai/langmem/blob/main/src/langmem/knowledge/tools.py"
    click KE "https://github.com/langchain-ai/langmem/blob/main/src/langmem/knowledge/extraction.py"
    click GM "https://github.com/langchain-ai/langmem/tree/main/src/langmem/graphs"
    click PM "https://github.com/langchain-ai/langmem/tree/main/src/langmem/prompts"
    click ST "https://github.com/langchain-ai/langmem/blob/main/src/langmem/short_term/summarization.py"
    click Utils "https://github.com/langchain-ai/langmem/blob/main/src/langmem/utils.py"
    click Err "https://github.com/langchain-ai/langmem/blob/main/src/langmem/errors.py"

    %% Styles
    classDef agent fill:#f9d423,stroke:#333,stroke-width:2px;
    classDef memoryAPI fill:#42a5f5,stroke:#333,stroke-width:2px;
    classDef processing fill:#66bb6a,stroke:#333,stroke-width:2px;
    classDef store fill:#ab47bc,stroke:#333,stroke-width:2px;
    classDef external fill:#ef5350,stroke:#333,stroke-width:2px;
    classDef support fill:#ffca28,stroke:#333,stroke-width:2px;
```
