Keep memories separate by tagging each write and query with user, agent, app, and session identifiers.
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.
# User scope returns user's memory{'results': [{'memory': 'avoids shellfish and prefers boutique hotels', ...}]}# Agent scope returns agent's own memory{'results': [{'memory': 'Cam prefers boutique hotels and avoids shellfish', ...}]}
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—just like above—rather than combining both in a single filter.
Putting it all together - here’s how to properly scope memories:
# Store memories with all identifiersclient.add( [{"role": "user", "content": "I need a hotel near the conference center."}], user_id="exec_123", agent_id="booking_assistant", app_id="enterprise_portal", run_id="trip-2025-03")# Retrieve with the same scopefilters = { "AND": [ {"user_id": "exec_123"}, {"app_id": "enterprise_portal"}, {"run_id": "trip-2025-03"} ]}# Alternative: Use wildcards if you're not sure about some fields# filters = {# "AND": [# {"user_id": "exec_123"},# {"agent_id": "*"}, # Match any agent# {"app_id": "enterprise_portal"},# {"run_id": "*"} # Match any run# ]# }results = client.search("Hotels near conference", filters=filters)# Debug: Print the filter you're usingprint(f"Searching with filters: {filters}")# If no results, try a broader search to see what's storedif not results["results"]: print("No results found! Trying broader search...") broader = client.get_all(filters={"user_id": "exec_123"}) print(broader)print(results["results"][0]["memory"])
This confirms you have a field mismatch. The memory exists but some identifier values don’t match exactly.Always check what’s actually stored:
# Get all memories for the user to see the actual field valuesall_mems = client.get_all(filters={"user_id": "your_user_id"})print(json.dumps(all_mems, indent=2))