{"slug": "give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re", "title": "Give Your Mem0 Agent Session-Scoped Memory in 15 Minutes (One Filter You're Probably Skipping)", "summary": "A developer warns that Mem0 agents can leak memory across users and sessions if the search() call is not scoped with the same user_id, agent_id, and run_id used during add(). The post demonstrates how failing to pass these identity filters on retrieval causes one user's memories to surface in another's context, and shows how to fix it with consistent scoping and metadata filters.", "body_md": "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.\n\nThis 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.\n\nMost Mem0 quickstarts look like this:\n\n``` python\nfrom mem0 import Memory\n\nm = Memory()\n\nm.add(\n    \"I prefer flights with no layovers and I'm vegetarian\",\n    user_id=\"alice\",\n)\n```\n\nThat looks scoped — you passed `user_id=\"alice\"`\n\n. The bug isn't in `add()`\n\n. It's in `search()`\n\n, three files away, written by a different part of the team (or you, two sprints later) without the same discipline:\n\n```\n# somewhere in the RAG/retrieval layer\nrelevant = m.search(query=\"what are the user's travel preferences?\")\n```\n\nNo `user_id`\n\n. No `filters`\n\n. 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.\n\nEvery `search()`\n\ncall needs the same scoping identity as the `add()`\n\ncall that created the memory. If you passed `user_id`\n\non write, pass it on every read:\n\n```\nrelevant = m.search(\n    query=\"what are the user's travel preferences?\",\n    user_id=\"alice\",\n)\n```\n\nThat's the 80% fix. But scoping by `user_id`\n\nalone 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.\n\nMem0 supports three identity dimensions, not one: `user_id`\n\n, `agent_id`\n\n, and `run_id`\n\n. If your product has multiple agents (a support bot and a booking bot, say) sharing the same user base, scoping by `user_id`\n\nalone means the booking bot's memories leak into the support bot's context — technically the right user, wrong agent, still a correctness bug.\n\n```\nm.add(\n    \"User wants the booking bot to always confirm price before charging\",\n    user_id=\"alice\",\n    agent_id=\"booking-bot\",\n)\n\nm.add(\n    \"User asked support to stop sending SMS notifications\",\n    user_id=\"alice\",\n    agent_id=\"support-bot\",\n)\n\n# retrieval inside booking-bot's context\nrelevant = m.search(\n    query=\"payment preferences\",\n    user_id=\"alice\",\n    agent_id=\"booking-bot\",\n)\n```\n\nWithout `agent_id`\n\non both calls, `search()`\n\nfrom 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.\n\n`run_id`\n\nis 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).\n\nOnce you're past simple identity scoping, Mem0's platform API accepts a `filters`\n\ndict with `AND`\n\n/`OR`\n\nlogic 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`\n\n\":\n\n```\nm.add(\n    \"Card ending 4242 declined for insufficient funds\",\n    user_id=\"alice\",\n    agent_id=\"billing-bot\",\n    metadata={\"category\": \"payment_issue\", \"resolved\": False},\n)\n\nrelevant = m.search(\n    query=\"payment issues\",\n    user_id=\"alice\",\n    agent_id=\"billing-bot\",\n    filters={\n        \"AND\": [\n            {\"category\": \"payment_issue\"},\n            {\"resolved\": False},\n        ]\n    },\n)\n```\n\nThis 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.\n\nGo do this right now, it's faster than reading the rest of this article twice:\n\n`.search(`\n\ncall against your Mem0 client.`user_id`\n\n(and `agent_id`\n\n/`run_id`\n\nif you use them) as the `add()`\n\ncalls that populate that memory space.`search()`\n\ncall missing scoping is a live cross-tenant leak — fix it before anything else on this list.`agent_id`\n\nto every add/search pair, not just the ones you've noticed problems with.`user_id=\"test-a\"`\n\n, search as `user_id=\"test-b\"`\n\n, 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.", "url": "https://wpnews.pro/news/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re", "canonical_source": "https://dev.to/mukesh_13/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-youre-probably-skipping-5139", "published_at": "2026-08-27 19:11:49+00:00", "updated_at": "2026-08-27 19:18:25.710444+00:00", "lang": "en", "topics": ["ai-agents", "developer-tools", "machine-learning"], "entities": ["Mem0"], "alternates": {"html": "https://wpnews.pro/news/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re", "markdown": "https://wpnews.pro/news/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re.md", "text": "https://wpnews.pro/news/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re.txt", "jsonld": "https://wpnews.pro/news/give-your-mem0-agent-session-scoped-memory-in-15-minutes-one-filter-you-re.jsonld"}}