MemDream: long-term memory for AI agents that keeps itself correct
An open-source Python library that decides whether each new fact adds to, replaces, or merges with what your agent already knows, then tidies up in the background.
Tell your agent “I live in Chennai.” A month later, tell it “I moved to Bangalore last month.”
Most agent memory is a vector database with an add() call, so it now holds both facts. Ask “where does the user live?” and you get whichever sentence is worded closer to the question. Nothing was missing from memory. The problem is that nothing ever decided which fact is still true.
I built MemDream to make that decision. It’s an open-source Python library (Apache 2.0) that gives an AI agent long-term memory that maintains itself.
pip install memdream
What it does
Every new fact is compared with what the agent already knows about that user, and one of four things happens:
Decision When What happens to the old memory
ADD A genuinely new fact Nothing
SUPERSEDE It contradicts something stored Kept as history, marked superseded, linked to its replacement
MERGE The same fact, said better Kept as history, linked to the canonical version
NOOP It adds nothing Unchanged, and the reason is recorded
Then, in the background, a process called Dream reads across a user’s memories, writes down patterns that no single memory states, and retires facts that are no longer true.
Thirty seconds of code
from memdream import MemDream
mem = MemDream(
user_id=”alice”,
prompts=”./prompts”,
llm=”openai:gpt-4o-mini”,
embeddings=”openai:text-embedding-3-small”,
database=”memory”, # no database server needed to try it
)
mem.add(“I live in Chennai.”)
mem.add(“I moved to Bangalore last month.”) # SUPERSEDE: Chennai becomes history
print(mem.search(“Where does the user live?”, latest_only=True).as_context())
– User lives in Bangalore.
database=”memory” keeps everything inside your process, so you can try it with no database server. For production, point the same call at MongoDB or PostgreSQL. Nothing else changes.
It’s a library, not a service. There’s no server to deploy and no worker to run. Your agent imports it, and the whole engine runs in your process.
In an agent loop
memories = mem.search(user_message, latest_only=True) # recall
reply = your_llm(system_prompt + memories.as_context(), user_message)
mem.add(user_message) # remember
Recall before responding, and remember after. If you remember first, the user’s current message becomes context for answering itself. In a web server, use AsyncMemDream (the same methods, awaited) and run the add in the background so the user isn’t waiting on it.
You write the prompts
MemDream ships no built-in prompts. The engine knows how to extract facts, choose between adding and superseding, find patterns and compact old memories. What it says to the model is up to you, because a memory layer for a clinic, a bank and a support desk should remember different things.
python -m memdream prompts ./prompts
That gives you a working starter set to edit. MemDream appends the output JSON format and validates the model’s answer, so your prompt only has to describe your domain. In practice, two or three worked examples (an input message and the fact you want stored) change behaviour more than paragraphs of instructions.
What happens on add()
Cheap, deterministic checks run first. The model runs last, on as little as possible:
your message
→ triage “hi”, “thanks”, “ok” never reach the model
→ extraction (LLM) pull out durable facts, using your prompt
→ policy confidence and importance floors, sensitivity rules
→ duplicate check a hash lookup, free if the user repeats themselves
→ find candidates similar memories for this user only
→ decision (LLM) ADD / SUPERSEDE / MERGE / NOOP, using your prompt
→ validation drop invented ids, protect permanent facts
→ state change done by code, never by the model
→ vector index + audit log
Measured with gpt-4o-mini and text-embedding-3-small, a message with a new fact costs about $0.0007 (two model calls). Small talk is filtered out before any model call, so “thanks!” costs nothing.
Dream runs by itself
With the default auto_dream=True there’s no cron job or queue to set up:
What When
Synthesis: pattern memories such as “User usually orders vegetarian on weekdays”, linked to their evidence After a user gains 20 new memories, at most every 6 hours
Expiry: retires memories whose validity has ended Once a day
Compaction: folds piles of low-value memories into one summary Off by default
Decay: retires what nothing has recalled in a long time Off by default
Retiring is a status change, not a deletion, so a wrong call can be undone. Set auto_dream=False and call mem.synthesize() and mem.consolidate() on your own schedule instead.
Design decisions
History is kept. Superseded memories stay, with a pointer to what replaced them, so “where did they live before?” can still be answered with mem.history(memory_id).
The model proposes; the code decides. The model’s answer is checked against the memories it was actually shown. It can’t touch a memory it never saw, and it can’t retire protected ones.
Users are isolated by construction. Every storage call needs the user’s scope, and each user’s vectors live in their own partition.
Every decision is explainable. Each one writes an audit row with the model’s reason, NOOPs included, so mem.audit() answers “why wasn’t this remembered?”
Delete means delete. mem.forget() retires a memory and can be undone. mem.delete() erases it, including its text in the audit log. mem.purge(confirm=True) erases everything about one user, for requests like GDPR erasure.
Bring your own stack
LLMs: OpenAI, Azure OpenAI, Anthropic, Gemini, Ollama, Together, Fireworks, or any OpenAI-compatible endpoint
Embeddings: OpenAI, Azure OpenAI, Gemini, Ollama
Database: MongoDB, PostgreSQL, or in-memory
Vector index: FAISS (the default), Qdrant, pgvector, Chroma, Pinecone, Milvus, Azure AI Search
With Ollama, the whole thing runs on your own machine with no API key.
Where it stands, and how you can help
MemDream is at version 0.1.1. CI runs the test suite on Python 3.10 to 3.13 and against real MongoDB, PostgreSQL with pgvector, and Qdrant. It’s early, and I’d rather hear what breaks now than later.
Next on my list:
Public numbers on the LongMemEval and LoCoMo memory benchmarks
Integration examples for LangChain, LlamaIndex, CrewAI and the OpenAI Agents SDK
More vector stores and model providers
If you build agents, please try it and open an issue about anything that confused you. Contributions are welcome. A new vector store, provider or example makes a good first contribution, because each one is self-contained.
GitHub: https://github.com/Mamlesh18/MemDream
PyPI: https://pypi.org/project/memdream/
If MemDream is useful to you, a star on GitHub helps other people find it.
