Skip to main content
Nora runs a travel service. When she stored all memories in one bucket, a recruiter’s nut allergy accidentally appeared in a traveler’s dinner reservation. Let’s fix this by properly separating memories for different users, agents, and applications.
Time to complete: ~15 minutes · Languages: Python

Setup

Grab an API key from the Mem0 dashboard to get started.

Store and Retrieve Scoped Memories

Let’s start by storing Cam’s travel preferences and retrieving them:
The memory is now stored. Let’s retrieve those memories with the same identifiers:
Output:
Memories can be written with several identifiers, but each search resolves one entity boundary at a time. Run separate queries for user and agent scopes, as shown above, rather than combining both in a single filter.

When Memories Leak

When Nora adds a chef agent, Cam’s travel preferences leak into food recommendations:
Output:
The travel preferences appear because we only filtered by user_id. The chef agent shouldn’t see hotel preferences.

Fix the Leak with Proper Filters

First, let’s add a memory specifically for the chef agent:
Now search within the chef’s scope:
Output:
Now the chef agent only sees its own food preferences. The hotel preferences stay with the travel agent.

Separate Apps with app_id

Nora white-labels her travel service for a sports brand. Use app_id to keep enterprise data separate:
Output:
Wildcards ("*" ) only match non-null values. Make sure you write memories with explicit app_id values.
Need a deeper tour of AND vs OR, nested filters, or wildcard tricks? Check the Memory Filters v2 guide for full examples you can copy into this flow.
When the sports brand offboards, delete all their data:
Output:

Production Patterns

Complete Example

Putting it all together - here’s how to properly scope memories:
Output:

When to Use Each Identifier

IdentifierWhen to UseExample Values
user_idIndividual preferences that persist across all interactionscam_traveler, sarah_exec, team_alpha
agent_idDifferent AI roles need separate contexttravel_agent, concierge, customer_support
app_idWhite-label deployments or separate productstravel_app_ios, enterprise_portal, partner_integration
run_idTemporary sessions that should be isolatedsupport_ticket_9234, chat_session_456, booking_flow_789

Troubleshooting Common Issues

My search returns empty results!

Problem: Using AND with exact matches but some fields might be null. Solution:

OR gives results but AND doesn’t

This confirms you have a field mismatch. The memory exists but some identifier values don’t match exactly. Always check what’s actually stored:

Best Practices

  1. Use consistent identifier formats
  2. Print filters when debugging
  3. Clean up temporary sessions

Summary

You learned how to:
  • Store memories with proper entity scoping using user_id, agent_id, app_id, and run_id
  • Prevent memory leaks between different agents and applications
  • Clean up data for specific tenants or sessions
  • Use wildcards to query across scoped memories

Next Steps

Deep Dive: Memory Filters v2

Control Memory Ingestion

Using Mem0? Star us on GitHub to help more developers discover memory for AI apps.