Time to complete: ~15 minutes · Languages: Python
Setup
Store and Retrieve Scoped Memories
Let’s start by storing Cam’s travel preferences and retrieving them:When Memories Leak
When Nora adds a chef agent, Cam’s travel preferences leak into food recommendations: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:Separate Apps with app_id
Nora white-labels her travel service for a sports brand. Useapp_id to keep enterprise data separate:
Wildcards (
"*" ) only match non-null values. Make sure you write memories with explicit app_id values.Production Patterns
Complete Example
Putting it all together - here’s how to properly scope memories:When to Use Each Identifier
| Identifier | When to Use | Example Values |
|---|---|---|
user_id | Individual preferences that persist across all interactions | cam_traveler, sarah_exec, team_alpha |
agent_id | Different AI roles need separate context | travel_agent, concierge, customer_support |
app_id | White-label deployments or separate products | travel_app_ios, enterprise_portal, partner_integration |
run_id | Temporary sessions that should be isolated | support_ticket_9234, chat_session_456, booking_flow_789 |
Troubleshooting Common Issues
My search returns empty results!
Problem: UsingAND 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
-
Use consistent identifier formats
-
Print filters when debugging
-
Clean up temporary sessions
Summary
You learned how to:- Store memories with proper entity scoping using
user_id,agent_id,app_id, andrun_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.