Advanced Memory Creation
Async Client (Python)
For asynchronous operations in Python, use the AsyncMemoryClient:Python
Detailed Memory Creation Examples
Long-term memory with full context
When passing
user_id
, memories are primarily created based on user messages, but may be influenced by assistant messages for contextual understanding. For example, in a conversation about food preferences, both the user’s stated preferences and their responses to the assistant’s questions would form user memories. Similarly, when using agent_id
, assistant messages are prioritized, but user messages might influence the agent’s memories based on context.Example:Metadata allows you to store structured information (location, timestamp, user state) with memories. Add it during creation to enable precise filtering and retrieval during searches.
Short-term memory for sessions
Agent memories
The
agent_id
retains memories exclusively based on messages generated by the assistant or those explicitly provided as input to the assistant. Messages outside these criteria are not stored as memory.Dual user and agent memories
When you provide bothuser_id
and agent_id
, Mem0 will store memories for both identifiers separately:
- Memories from messages with
"role": "user"
are automatically tagged with the provideduser_id
- Memories from messages with
"role": "assistant"
are automatically tagged with the providedagent_id
- During retrieval, you can provide either
user_id
oragent_id
to access the respective memories - You can continuously enrich existing memory collections by adding new memories to the same
user_id
oragent_id
in subsequent API calls, either together or separately, allowing for progressive memory building over time - This dual-tagging approach enables personalized experiences for both users and AI agents in your application
Advanced Search Operations
Search with Custom Filters
Our advanced search allows you to set custom search filters. You can filter by user_id, agent_id, app_id, run_id, created_at, updated_at, categories, and text. The filters support logical operators (AND, OR) and comparison operators (in, gte, lte, gt, lt, ne, contains, icontains,*
). The wildcard character (*
) matches everything for a specific field.
For the categories field specifically:
- Use
contains
for partial matching (e.g.,{"categories": {"contains": "finance"}}
) - Use
in
for exact matching (e.g.,{"categories": {"in": ["personal_information"]}}
).
version
as v2
in the search method.
Example 1: Search using user_id and agent_id filters
Example 2: Search using date filters
Example 3: Search using categories filters
Example 4: Search using NOT filters
Example 5: Search using wildcard filters
Advanced Retrieval Operations
Get All Memories with Pagination
The
get_all
method supports two output formats: v1.0
(default) and v1.1
. To use the latest format, which provides more detailed information about each memory operation, set the output_format
parameter to v1.1
. We’re soon deprecating the default output format for get_all() method, which returned a list. Once the changes are live, paginated response will be the only supported format, with 100 memories per page by default. You can customize this using the
page
and page_size
parameters.Get all memories of a user
Get all memories by categories
You can filter memories by their categories when using get_all:Get all memories using custom filters
Our advanced retrieval allows you to set custom filters when fetching memories. You can filter by user_id, agent_id, app_id, run_id, created_at, updated_at, categories, and keywords. The filters support logical operators (AND, OR) and comparison operators (in, gte, lte, gt, lt, ne, contains, icontains,*
). The wildcard character (*
) matches everything for a specific field.
For the categories field specifically:
- Use
contains
for partial matching (e.g.,{"categories": {"contains": "finance"}}
) - Use
in
for exact matching (e.g.,{"categories": {"in": ["personal_information"]}}
).
version
as v2
in the get_all method.
Memory Management Operations
Memory History
Get history of how a memory has changed over time.Update Memory
Update a memory with new data. You can update the memory’s text, metadata, or both.Batch Operations
Batch Update Memories
Update multiple memories in a single API call. You can update up to 1000 memories at once.Batch Delete Memories
Delete multiple memories in a single API call. You can delete up to 1000 memories at once.Entity Management
Get All Users
Get all users, agents, and runs which have memories associated with them.Delete Operations
Delete specific memory:Reset Client
Natural Language Delete
You can also delete memories using natural language commands:Monitor Memory Operations
You can monitor memory operations on the platform dashboard: