DEV Community

Cover image for LangGraph With MongoDB: Building Conversational Long-Term Memory for Intelligent AI Agents
Pash10g for MongoDB

Posted on

LangGraph With MongoDB: Building Conversational Long-Term Memory for Intelligent AI Agents

Introduction: The memory problem in AI agents

LangGraph has proven to be one of the most adoptable AI frameworks for building AI agents, but there's been a critical missing piece: persistent, intelligent memory. Most AI agents today suffer from amnesia—they forget everything between conversations, can't learn from past interactions, and fail to build meaningful relationships with users.

MongoDB Atlas is a scalable and extremely stable database solution perfect for the AI era. The flexibility of the document model, combined with the ability to have your operational data right next to your vectorized data for semantic and hybrid searches, makes MongoDB Atlas a robust solution for AI memory management.

LangMem is a powerful new addition to the LangChain suite that allows you to add sophisticated, long-term memory management to your AI agents. Combined with MongoDB's built-in checkpointer and the new MongoDB Store for LangGraph, we now have a complete solution— Reasoning AI → Tools → Storage → Memory —to form a new level of intelligent, context-aware, and personalized experiences.

Technical deep dive: how it works

The architecture

Our solution combines three critical components that work together seamlessly:

MongoDBStore : Vector-based semantic memory storage with intelligent retrieval, as well as a key value store

MongoDBSaver : Conversation state persistence and resumption

Memory Management Tools : Agent-controlled memory operations

Let's explore each component and how they integrate.

Component 1: MongoDBStore—semantic memory storage

The MongoDBStore handles long-term memory storage using vector embeddings, via Voyage AI state-of-the-art models for semantic search:

from langgraph.store.mongodb.base import MongoDBStore, VectorIndexConfig
from langchain_voyageai import VoyageAIEmbeddings

# Initialize MongoDB connection
client = MongoClient(MONGODB_URI)
db = client["memories"]
collection = db["memory_store"]

# Create the store with vector search capabilities
store = MongoDBStore(
    collection=collection,
    index_config=VectorIndexConfig(
        fields=None,          # Auto-detect fields for indexing
        filters=None,         # No additional filters
        dims=1536,           # OpenAI embedding dimensions
        embed=VoyageAIEmbeddings(model="voyage-3.5")   # Embedding model for vector search
    )
auto_index_timeout=70
)
Enter fullscreen mode Exit fullscreen mode

Key features:

  • Semantic search : Finds memories based on meaning, not just keywords
  • Namespace organization : Memories can be organized by category ("memories", "preferences", etc.)
  • Vector indexing : Automatic creation of MongoDB Atlas Vector Search indexes
  • Scalable storage : Handles thousands of memories without performance degradation
Component 2: MongoDBSaver—conversation checkpointing

The MongoDBSaver manages conversation state and enables thread resumption:

from langgraph.checkpoint.mongodb import MongoDBSaver

# Initialize the checkpointer
checkpointer = MongoDBSaver(
    client,                            # MongoDB client
    db_name="memories",               # Database name
    collection_name="thread_checkpoints"   # Collection for conversation state
)
Enter fullscreen mode Exit fullscreen mode

What it saves:

  • Complete message history for each conversation thread
  • Agent state between interactions
  • Tool call results and intermediate steps
  • Configuration and metadata

Thread management:

# Each conversation gets a unique thread ID
config = {"configurable": {"thread_id": "thread-a"}}

# The checkpointer automatically:
# 1. Saves state after each interaction
# 2. Loads previous state when thread resumes
# 3. Maintains conversation context
Enter fullscreen mode Exit fullscreen mode
Integration—how they work together

Here's how the components integrate in a complete agent:

from langgraph.prebuilt import create_react_agent
from langmem import create_manage_memory_tool

def prompt(state, store):
    """Dynamic prompt that injects relevant memories"""
    # Semantic search for relevant memories
    memories = store.search(
        ("memories",),   # Namespace
        query=state["messages"][-1].content,   # Current user message
    )
    system_msg = f"""You are a shopping assistant with persistent memory.
## Relevant Memories
<memories>
{memories}
</memories>
Use these memories to provide personalized responses."""
    return [{"role": "system", "content": system_msg}, *state["messages"]]

# Create the complete agent
agent = create_react_agent(
    "openai:gpt-4o",
    prompt=lambda state: prompt(state, store),   # Inject memories into prompt
    tools=[
        create_manage_memory_tool(namespace=("memories",)),   # Memory management
        search_products   # Domain-specific tools
    ],
    store=store,            # Long-term memory storage
    checkpointer=checkpointer,   # Conversation persistence
)
Enter fullscreen mode Exit fullscreen mode
Memory lifecycle in action

Here's what happens during a complete interaction:

# 1. User starts conversation
config = {"configurable": {"thread_id": "user-123"}}
response = agent.invoke({
    "messages": [{"role": "user", "content": "I am vegan"}]
}, config=config)

# Behind the scenes:
# a) MongoDBSaver loads any existing conversation state
# b) MongoDBStore searches for relevant memories (initially empty)
# c) Agent processes the message and decides to save preference
# d) create_manage_memory_tool saves "User is vegan" to MongoDBStore
# e) MongoDBSaver saves the complete interaction state

# 2. Later conversation (same or different thread)
new_config = {"configurable": {"thread_id": "user-123-session-2"}}
response = agent.invoke({
    "messages": [{"role": "user", "content": "Help me find pasta"}]
}, config=new_config)

# Behind the scenes:
# a) MongoDBSaver loads thread state (may be empty for new thread)
# b) MongoDBStore searches memories for "pasta" context
# c) Vector search finds "User is vegan" as relevant
# d) Agent provides vegan pasta recommendations
# e) State saved for future resumption
Enter fullscreen mode Exit fullscreen mode

Real-world implementation example

Let's see a complete shopping assistant:

import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)

db = client["ai_shop"]
collection = db["products"]

@tool
def search_products(query: str) -> str:
    """Searches for products in the database using vector search."""

    vectorstore = MongoDBAtlasVectorSearch(collection, OpenAIEmbeddings(), text_key="title", embedding_key="embedding", index_name="vector_index")
    docs = vectorstore.similarity_search(query, k=5)

    return "\n".join([str(doc.metadata) for doc in docs])



def create_shopping_agent():
    """Complete shopping assistant with memory"""

    # Memory storage setup
    store = MongoDBStore(
        collection=client.memories.user_preferences,
        index_config=VectorIndexConfig(
            dims=1536,
            embed=VoyageAIEmbeddings(model="voyage-3.5"),
            fields=["content"],
            filters=["active"]
        )
    )

    # Conversation persistence
    checkpointer = MongoDBSaver(
        client,
        db_name="shopping_assistant",
        collection_name="conversations"
    )

    # Dynamic prompt with memory injection
    def enhanced_prompt(state, store):
        user_query = state["messages"][-1].content

        # Search for relevant user preferences
        memories = store.search(
            ("preferences",),
            query=user_query,
            limit=3,
            filter={"active": True}
        )

        # Search for relevant past purchases
        purchase_history = store.search(
            ("purchases",),
            query=user_query,
            limit=2
        )

        system_msg = f"""You are an expert shopping assistant with access to:
        - Product search capabilities
        - User preference memory
        - Purchase history

## User Preferences
{memories}

## Recent Purchase Context
{purchase_history}

Provide personalized, helpful shopping advice."""

        return [{"role": "system", "content": system_msg}, *state["messages"]]

    # Create agent with all capabilities
    return create_react_agent(
        "openai:gpt-4o",
        prompt=lambda state: enhanced_prompt(state, store),
        tools=[
            create_manage_memory_tool(namespace=("preferences",)),
            create_manage_memory_tool(namespace=("purchases",)),
            search_products
        ],
        store=store,
        checkpointer=checkpointer
    )

# Usage example
agent = create_shopping_agent()


def get_user_config(user_id, thread_id="default_thread"):
    return {"configurable": {"user_id": user_id, "thread_id": thread_id}}


# Conversation 1: Learning preferences
config = get_user_config("user123")
response = agent.invoke({
    "messages": [{"role": "user", "content": "I'm vegan and prefer organic products"}]
}, config=config)

print(response["messages"][-1].content)

# Conversation 2: Using learned preferences (different session)
config = get_user_config("user123", "mobile-app")
response = agent.invoke({
    "messages": [{"role": "user", "content": "Find me some pasta options"}]
}, config=config)
# Agent automatically applies vegan + organic filters

print(response["messages"][-1].content)
Enter fullscreen mode Exit fullscreen mode

Problems this solution solves

1. Memory persistence across sessions

Traditional agents lose all context when a conversation ends. With MongoDB persistence, memories survive server restarts, deployments, and extended periods between interactions.

2. Intelligent memory retrieval

Instead of dumping all past information into the context, vector search ensures only relevant memories are retrieved, keeping conversations focused and efficient while maintaining important context.

3. Scalable memory management

MongoDB's document model and Atlas's vector search capabilities mean your agent's memory can grow without performance degradation. The system gracefully handles thousands of memories per user.

4. Cross-conversation learning

Memories aren't tied to specific conversation threads. An agent can learn a user's preferences in one conversation and apply that knowledge in completely separate interactions days or weeks later.

5. Self-managing memory

Agents can autonomously decide what information is worth remembering, updating outdated preferences, and even forgetting irrelevant details—just like human memory.

MongoDB Atlas: production-grade AI memory infrastructure

Multi-tenant memory architecture

For applications serving multiple users:

def create_user_scoped_agent(user_id: str):
    """Create agent with user-isolated memory"""
    # User-specific memory namespace
    user_namespace = ("users", user_id, "memories")
    store = MongoDBStore(
        collection=collection,
        index_config=VectorIndexConfig(
            dims=1536,
            embed=OpenAIEmbeddings(),
            # Filter ensures user isolation
            filters={"metadata.user_id": user_id}
        )
    )
    # Memory tools scoped to user
    memory_tool = create_manage_memory_tool(
        namespace=user_namespace,
        # Add user_id to all memories
        metadata_filter={"user_id": user_id}
    )
    return create_react_agent(
        "openai:gpt-4o",
        tools=[memory_tool],
        store=store,
        checkpointer=MongoDBSaver(
            client,
            db_name="multi_tenant_memories",
            collection_name=f"user_{user_id}_checkpoints"
        )
    )
Enter fullscreen mode Exit fullscreen mode

Key benefits for developers

🎯 Production-ready architecture
  • Automatic scaling : MongoDB Atlas handles traffic spikes and data growth
  • High availability : Built-in replication and failover
  • Global distribution : Deploy memory close to your users
  • Enterprise security : Encryption, authentication, and compliance-ready
🔧 Flexible integration
  • Zero breaking changes : Add to existing LangGraph agents without modification
  • Multiple namespaces : Organize memories by type, user, or domain
  • Custom embeddings : Use any embedding model (VoyageAI [recommended], OpenAI, Cohere, local models)
  • Metadata filtering : Precise memory retrieval with custom filters
💡 Intelligent memory management
  • Semantic search : Vector-based retrieval finds contextually relevant memories
  • Automatic indexing : MongoDB Atlas creates optimized vector search indexes
  • Memory lifecycle : Agents can create, update, and delete their own memories
  • Cross-thread persistence : Memories survive conversation boundaries

Use cases that transform user experience

Personal AI assistants
  • Remember user preferences, habits, and goals.
  • Provide increasingly personalized recommendations.
  • Build genuine rapport through consistent context.
Customer support agents
  • Recall past support interactions and resolutions.
  • Understand customer context and history.
  • Provide continuity across different support channels.
Educational AI tutors
  • Track learning progress and identify knowledge gaps.
  • Adapt teaching strategies based on student responses.
  • Remember what explanations worked best for each student.
Healthcare AI companions
  • Monitor long-term health trends and symptoms.
  • Remember medication schedules and reactions.
  • Provide consistent care coordination.

Getting started

Please see our notebook for code implementation details.

The future of AI conversations

This integration represents a fundamental shift in how we build AI agents. Instead of stateless, forgetful bots, we can now create AI companions that truly learn, remember, and grow with their users.

The combination of LangGraph's agent orchestration, LangMem's memory management, and MongoDB's robust data platform creates a foundation for the next generation of AI applications—ones that feel less like tools and more like digital partners.

Conclusion

Memory transforms everything. It's the difference between a helpful tool and a trusted companion, between answering questions and understanding needs, between artificial intelligence and artificial empathy.

With LangMem and MongoDB, that transformation is now within reach for every developer building AI agents. The future of conversational AI isn't just smarter responses—it's relationships that persist, grow, and deepen over time.

Ready to give your AI agents a memory? Start building with the complete code example in our interactive notebook and see how persistent memory can transform your user experiences today.

Want to learn more? Check out the MongoDB Atlas Vector Search documentation and explore the LangGraph integration tutorial for advanced memory management patterns.

Top comments (0)