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.
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:
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.
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.
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",
)
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).
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.
Go do this right now, it's faster than reading the rest of this article twice:
.search(
call against your Mem0 client.user_id
(and agent_id
/run_id
if you use them) as the add()
calls that populate that memory space.search()
call missing scoping is a live cross-tenant leak — fix it before anything else on this list.agent_id
to every add/search pair, not just the ones you've noticed problems with.user_id="test-a"
, search as user_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.