Memori

Engineering

Written by Bobur Umurzokov

Memory for AI Agents with MongoDB

Powering long-term memory for AI Agents with Memori and MongoDB.

In this post, we'll explore how to give your AI long-term memory using MongoDB and Memori, an open-source memory framework for AI agents.

You'll learn how memory works, why it's important, and how to build your own chat agent that remembers using just a few lines of Python code.

What Is Memory for AI Agents?

Most chatbots and AI agents today don't remember previous interactions. Each time you ask a question, the model sees it as new input. But in the real world, memory matters. If an AI is helping you plan a project, it should remember your goals, past discussions, and preferences.

If it's a customer support bot, it should recall your name, issue history, and account status.

Why Memory Is Important

Here are a few examples where memory changes everything:

Use CaseWithout MemoryWith Memory
Customer SupportEach ticket starts from zeroKnows your previous issues and solutions
Personal AssistantForgets tasks and meetingsRemembers your schedule and priorities
Shopping AssistantRepeats questions every timeLearns your preferences and size
Education TutorRe-explains same conceptsTracks your progress and weaknesses

Memory lets AI go beyond single-turn conversations to build continuity, personalization, and intelligence.

Why MongoDB for AI Memory?

When designing AI memory, you need a database that can handle flexible data: text, metadata, context, and relationships.

MongoDB is perfect for this because it's:

  • Document-based: You can store each memory as a JSON-like document.
  • Flexible schema: No need for rigid tables; your data can evolve as your AI learns more.
  • Scalable: Ideal for agents that process millions of conversations.
  • Well-integrated: Easy to connect with Python, FastAPI, and Memori.

Instead of managing embeddings or vector databases right away, you can start simply with MongoDB to store structured memory about users, topics, or events.

What Is Memori?

Memori is an open-source framework that adds structured memory to AI agents. It can work with Postgres, SQLite, or MongoDB, and integrates naturally with OpenAI, Anthropic, or local models. Memori acts like a "memory layer" between your AI model and the real world.

It automatically records conversations, extracts important facts, and allows your agent to recall them later.

Memori + MongoDB = Smart, Scalable Memory

With the MongoDB integration, you can use Memori to:

  • Store each message, context, and summary as a document.
  • Query or filter memory entries by user, topic, or timestamp.
  • Ingest facts automatically while your AI chats.
  • Retrieve memory to make context-aware decisions.

Let's build a working example.

Building an AI Agent That Remembers

Below is a full example that connects Memori to MongoDB, and tracks conversations while you chat with GPT-5.

from openai import OpenAI
from memori import Memori

# Initialize OpenAI client
openai_client = OpenAI()

print("Initializing Memori with MongoDB database...")
memory = Memori(
    database_connect="mongodb://127.0.0.1:56145/memori",
    conscious_ingest=True,
    auto_ingest=True,
)

print("Enabling memory tracking...")
memory.enable()

print("Memori MongoDB Demo - Chat with GPT-4o while memory is being tracked")
print("Type 'exit' or press Ctrl+C to quit")
print("-" * 50)

while 1:
    try:
        user_input = input("User: ")
        if not user_input.strip():
            continue

        if user_input.lower() == "exit":
            print("Goodbye!")
            break

        print("Processing your message with memory tracking...")
        response = openai_client.chat.completions.create(
            model="gpt-5", messages=[{"role": "user", "content": user_input}]
        )
        print(f"AI: {response.choices[0].message.content}")
        print()  # Add blank line for readability
    except (EOFError, KeyboardInterrupt):
        print("\nExiting...")
        break
    except Exception as e:
        print(f"Error: {e}")
        continue

What's Happening Behind the Scenes

Let's break it down step by step:

  1. Initialize Memori

    memory = Memori(
        database_connect="mongodb://127.0.0.1:56145/memori",
        conscious_ingest=True,
        auto_ingest=True,
    )
    

    This connects Memori to your MongoDB instance.

    The memori database will store all conversation logs, extracted facts, and agent insights.

    • auto_ingest=True means Memori automatically saves the conversation context.
    • conscious_ingest=True enables higher-level understanding (it summarizes and structures the memory).
  2. Enable Memory Tracking

    memory.enable()
    

    This activates Memori's recording system.

    Every interaction with OpenAI's API will be intercepted and stored automatically.

  3. Talk to GPT-4o

    response = openai_client.chat.completions.create(
        model="gpt-4o", messages=[{"role": "user", "content": user_input}]
    )
    

    You can talk naturally with GPT-4o, and behind the scenes, Memori captures:

    • The user message
    • The AI's response
    • The timestamp
    • Extracted entities or facts
  4. Store in MongoDB

    Memori saves this data as a document in MongoDB, like this:

    {
      "_id": "6717a0df8f14a",
      "role": "user",
      "content": "What's the weather like today?",
      "timestamp": "2025-10-23T12:30:45Z",
      "session_id": "abc123",
      "metadata": {
        "entities": ["weather", "today"],
        "topic": "weather"
      }
    }
    

    Each session and message becomes part of a structured memory store that can be searched and reused.

Querying Memory from MongoDB

Once Memori saves your chat logs in MongoDB, you can explore them directly.

Open your MongoDB shell or GUI (like MongoDB Compass) and check:

use memori
db.memory_entries.find().pretty()

You'll see all previous chats with details such as:

  • Who said what
  • When it happened
  • The extracted topics
  • Auto-generated summaries

You can also build filters:

db.memory_entries.find({ 'metadata.topic': 'project' });

This helps your AI recall everything related to a given topic or user context.

Adding Memory Recall to Your Agent

So far, we've stored memory. Now let's use it.

We can fetch previous messages from MongoDB and include them as context before sending a new prompt to GPT-4o.

Here's a simple function that recalls past user inputs:

def recall_memory(user_query):
    # Search MongoDB for related past memories
    from pymongo import MongoClient
    client = MongoClient("mongodb://127.0.0.1:56145/")
    db = client["memori"]
    collection = db["memory_entries"]

    related = collection.find(
        {"content": {"$regex": user_query, "$options": "i"}}
    ).sort("timestamp", -1).limit(3)

    past_context = [doc["content"] for doc in related]
    return "\n".join(past_context)

Then include it before sending to the model:

context = recall_memory(user_input)

response = openai_client.chat.completions.create(
    model="gpt-4o",
    messages=[
        {"role": "system", "content": f"Past context:\n{context}"},
        {"role": "user", "content": user_input},
    ],
)

Now your AI will recall relevant previous chats from MongoDB automatically. For example, remembering user preferences or prior discussions.

How Memory Structure Works

Memori structures memory in layers — similar to how human memory works:

LayerDescriptionStored In
Short-termTemporary chat context (recent turns)In-memory or Redis
Long-termPersistent structured facts, user data, summariesMongoDB
EpisodicSpecific sessions and eventsMongoDB
SemanticExtracted knowledge (like "Bobur is in Tallinn")MongoDB or SQL

MongoDB helps especially with episodic and semantic memory —

storing data in flexible JSON-like format for easy recall and reasoning.

Example: Remembering User Preferences

Let's see how memory can make an AI assistant smarter.

Say you have this conversation:

User: I'm vegan.
AI: Got it! I'll remember that.
User: Suggest me dinner ideas.
AI: How about a lentil curry or grilled tofu salad?

Memori automatically stores the first statement (I'm vegan) as a fact about the user.

When the user later asks for dinner ideas, the agent recalls that fact — no need to repeat.

This can be represented in MongoDB as:

{
  "user_id": "user_123",
  "fact": "User is vegan",
  "context": "preferences",
  "timestamp": "2025-10-23T12:34:00Z"
}

Later, when the agent generates a reply, Memori searches these facts and adds them as context automatically.

Making Memory Scalable

As your AI handles more users, you might want to:

  • Store each user's memory in a separate collection or namespace.
  • Add TTL (time-to-live) indexes to expire old data.
  • Summarize long sessions into compact knowledge entries.

MongoDB supports all of these natively.

Example: Using TTL Index

db.memory_entries.createIndex(
  { timestamp: 1 },
  { expireAfterSeconds: 60 * 60 * 24 * 30 }, // 30 days
);

This keeps your memory fresh by automatically cleaning up older conversations.

Adding Memory Summarization

For large memories, it's useful to summarize past context into concise notes.

Memori can automatically do this using conscious_ingest=True,

but you can also build your own summarization logic.

Here's an example:

def summarize_memory():
    from pymongo import MongoClient
    from openai import OpenAI
    client = MongoClient("mongodb://127.0.0.1:56145/")
    db = client["memori"]
    entries = db["memory_entries"].find().limit(20)

    text = "\n".join([e["content"] for e in entries])
    openai_client = OpenAI()
    summary = openai_client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[
            {"role": "system", "content": "Summarize the following conversation briefly."},
            {"role": "user", "content": text},
        ],
    )
    print(summary.choices[0].message.content)

You can store these summaries back in MongoDB as "knowledge notes."

When to Use MongoDB vs SQL

Memori supports both MongoDB and SQL (Postgres, MySQL, SQLite).

Here's a quick comparison:

FeatureMongoDBSQL
Schema flexibility✅ Dynamic❌ Fixed
Nested JSON data✅ Natural fit⚠️ Requires JSONB
Query complexitySimple filtersAdvanced joins
ScalingHorizontalVertical (mostly)
Ideal forUnstructured or conversational dataStructured analytics

If your agent deals mostly with unstructured conversations or semi-structured facts, MongoDB is perfect. If you need analytics or joins, go with Postgres.

Integrating Memory into Apps

You can plug Memori with MongoDB into any app backend:

  • FastAPI for REST APIs
  • LangChain or Agno for agent frameworks
  • Streamlit for live chat UIs
  • DigitalOcean or MongoDB Atlas for cloud deployment

Example (FastAPI integration):

from fastapi import FastAPI, Request
from memori import Memori
from openai import OpenAI

app = FastAPI()

memory = Memori(
    database_connect="mongodb://localhost:56145/memori",
		auto_ingest=True
)

memory.enable()
openai_client = OpenAI()

@app.post("/chat")
async def chat(request: Request):
    body = await request.json()
    user_input = body["message"]

    response = openai_client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": user_input}],
    )
    return {"reply": response.choices[0].message.content}

Now every chat request automatically goes through Memori, and all context is saved in MongoDB.

Conclusion: Memory Is the Next Step for AI Agents

We're moving from prompt-based AI to memory-based AI. Large language models are powerful, but without memory, they're like people with short-term amnesia.

By adding MongoDB as a memory store and Memori as the engine,

you can make your AI smarter, consistent, and truly helpful.

Here's what you gain:

  • Long-term continuity in conversations
  • Context-aware answers
  • Personalization and reasoning
  • Easy-to-query structured storage
  • Simple integration with Python and OpenAI

Whether you're building a customer support bot, a research agent, or a personal assistant,

giving it memory is how you make it truly intelligent.

Next Steps

  • Try the example script above.
  • Install MongoDB locally or use MongoDB Atlas.
  • Explore Memori on GitHub: github.com/GibsonAI/memori.
  • Experiment with storing, recalling, and summarizing conversations.