You add memory to your agent with Mem0, ship it, and it works great in your dev environment where you're the only user. Then you go multi-tenant — real users, real sessions — and three weeks later someone reports that the agent "remembers" something they never told it. It's not a hallucination. It's another user's memory, served straight from your own vector store.
This is the single most common Mem0 integration bug I run into, and the fix is one filter you're probably not passing consistently. Here's the 15-minute version.
The setup that causes it
Most Mem0 quickstarts look like this:
from mem0 import Memory
m = Memory()
m.add(
"I prefer flights with no layovers and I'm vegetarian",
user_id="alice",
)
That looks scoped — you passed user_id="alice". The bug isn't in add(). It's in search(), three files away, written by a different part of the team (or you, two sprints later) without the same discipline:
# somewhere in the RAG/retrieval layer
relevant = m.search(query="what are the user's travel preferences?")
No user_id. No filters. Mem0 will happily return the closest semantic matches across every memory in the store — Alice's vegetarian preference bleeding into Bob's session, or worse, into an agent that's actively talking to Bob. The write path was scoped. The read path wasn't. Because both calls succeed and return plausible-looking data, this ships, passes QA (one tester, one session), and only shows up once you have concurrent real users.
The one-line fix
Every search() call needs the same scoping identity as the add() call that created the memory. If you passed user_id on write, pass it on every read:
relevant = m.search(
query="what are the user's travel preferences?",
user_id="alice",
)
That's the 80% fix. But scoping by user_id alone isn't enough once you have more than one agent or more than one conversation thread per user — which is most real products within a month of launch.
The part people miss: agent_id and run_id
Mem0 supports three identity dimensions, not one: user_id, agent_id, and run_id. If your product has multiple agents (a support bot and a booking bot, say) sharing the same user base, scoping by user_id alone means the booking bot's memories leak into the support bot's context — technically the right user, wrong agent, still a correctness bug.
m.add(
"User wants the booking bot to always confirm price before charging",
user_id="alice",
agent_id="booking-bot",
)
m.add(
"User asked support to stop sending SMS notifications",
user_id="alice",
agent_id="support-bot",
)
# retrieval inside booking-bot's context
relevant = m.search(
query="payment preferences",
user_id="alice",
agent_id="booking-bot",
)
Without agent_id on both calls, search() from the booking bot can surface the SMS-notification memory that belongs to a completely different conversational context. It's not wrong data exactly — it's real, it's Alice's — but it's the wrong memory for this agent to be reasoning with, and it will show up in the prompt as if it's relevant.
run_id is the same idea one level down: scope to a single session or task run when you don't want memory to carry across unrelated conversations with the same agent (a returning support ticket vs. an old, resolved one, for example).
Advanced filtering for anything beyond exact match
Once you're past simple identity scoping, Mem0's platform API accepts a filters dict with AND/OR logic on top of metadata you attach at write time — useful for things like "only memories from the last 30 days" or "only memories tagged billing":
m.add(
"Card ending 4242 declined for insufficient funds",
user_id="alice",
agent_id="billing-bot",
metadata={"category": "payment_issue", "resolved": False},
)
relevant = m.search(
query="payment issues",
user_id="alice",
agent_id="billing-bot",
filters={
"AND": [
{"category": "payment_issue"},
{"resolved": False},
]
},
)
This is what turns "the agent remembers everything about this user" into "the agent remembers the right thing for this exact context" — which is the actual goal, not raw recall.
The 15-minute checklist
Go do this right now, it's faster than reading the rest of this article twice:
- Grep your codebase for every
.search(call against your Mem0 client. - For each one, check it passes the same
user_id(andagent_id/run_idif you use them) as theadd()calls that populate that memory space. - Any
search()call missing scoping is a live cross-tenant leak — fix it before anything else on this list. - If two agents share a user base, add
agent_idto every add/search pair, not just the ones you've noticed problems with. - Write one test: add a scoped memory for
user_id="test-a", search asuser_id="test-b", assert the result is empty. This is the regression test that catches the bug before your users do.
The underlying lesson generalizes past Mem0: any memory or retrieval layer that supports scoping only prevents leaks if scoping is enforced symmetrically on both write and read. Write-side discipline without read-side discipline isn't partial protection — it's a false sense of security with a matching demo that works perfectly until a second user shows up.
Top comments (2)
The read-side boundary is the part I would test first. A scoped write path gives you a comforting demo, but the failure only appears when retrieval has two plausible users in the same store. Semantic search makes that bug look normal until the wrong preference shows up in a real session.
This is a very important distinction: memory correctness is primarily a retrieval-boundary problem, not just a storage problem.
The symmetric scoping principle you describe is exactly right. A system can have perfectly isolated add() operations and still become a cross-tenant data leak if retrieval doesn't enforce the same identity constraints.
I’d take the implementation one step further by making the scope non-optional at the application boundary. Instead of allowing developers throughout the codebase to call m.search() directly, wrap Mem0 behind a session-aware memory service that requires something like:
tenant_id + user_id + agent_id + session/run_id
Then enforce those fields server-side rather than trusting individual callers to remember them. This also makes it much easier to audit and test.
I especially like the negative test you suggested. I’d expand it into an isolation matrix:
User A → User B: no results
Agent A → Agent B: no results
Session A → Session B: no results
Tenant A → Tenant B: no results
Correct scope → expected results
And importantly, test both semantic retrieval and metadata filtering. A filter bug can be just as dangerous as a missing user_id.
There’s also a subtle architectural point here: relevance ≠ authorization. A vector database may correctly determine that another user's memory is semantically relevant, but that doesn't mean the current agent is authorized to see it. Authorization/scoping must happen before the LLM receives the retrieved context.
For production systems, I’d also recommend logging the effective retrieval scope (without logging sensitive memory contents) so unexpected cross-scope queries can be detected and investigated.
This pattern generalizes extremely well beyond Mem0 to RAG pipelines, vector databases, cached agent state, and multi-tenant AI systems.
Great write-up. The "one missing filter" framing is simple, but the underlying lesson is much bigger: retrieval boundaries should be enforced as an architectural invariant, not a developer convention.
I’d be interested in exchanging ideas around production-grade multi-agent/RAG architecture and long-term AI automation projects.