The add() operation is a basic part of Mem0's memory management system. It's how you store information your application or agent can later recall. Whether you're saving user preferences, maintaining conversation context, or building consistent AI personalities, understanding how to use add() effectively is key to building memory-enhanced AI applications.
The Basics of add()
The basic structure of the add() operation is:
add() also accepts a custom_categories parameter for classifying memories into domain-specific buckets beyond Mem0's defaults; see Understanding Custom Categories for how that works.
The Different Types of Memories
The add() operation allows you to store memories in two different contexts:
Long-term memories
Session-based memories
Let's dive into how to use each one.
1. Long-term Memories
Use this when you need to store information that should persist across multiple sessions:
To store a memory, you need to provide:
either a
user_idoragent_id, this binds the memory and provides context when retrieving memories in the future.the
messagesparameter can accept either a string or a list of message dictionariesoptional
metadatato attach additional context for the memories, e.g., thecategorythe memory belongs to
add() also supports includes and excludes parameters for controlling which parts of a conversation get stored in the first place; useful when you want the AI to remember technical preferences but ignore small talk, for example. See Customizing Memory in Mem0 for the full pattern.
2. Session-Based Memories
Short-term memories in Mem0 are crucial for managing session-specific information. For instance, during a customer support session, Mem0 can retain details such as the customer's recent queries, issues they're facing, and their preferred communication style. This allows support agents to maintain context throughout the session, providing quick, personalized assistance without requiring the user to repeat information. Once the session ends, the memory resets, ensuring that temporary information doesn’t carry over unnecessarily, keeping interactions fresh for the next session.
For short-term memories, you need to include either a user_id or agent_id and a run_id. This associates the session to a particular user or agent.
Output Formats
Mem0 supports two output formats:
The difference is that v1.0 returns a basic operation status, whether it was successful or not, and v1.1 has detailed information about memory operations. For example, add operations return the saved memory and the event type, which is ADD. Search operations return the memory and other details like the conversation that the memory was extracted from.
Conclusion
The add() operation is your primary tool for storing memories in Mem0. By understanding its parameters and best practices, you can effectively manage different types of memories in your AI applications. Always use the appropriate identifiers, consider the persistence requirements of your memories, and leverage metadata for better organization. Once memories are stored, Mem0's search() operation is how you retrieve them — basic queries, output formats, and advanced filtering by user_id, agent_id, and date range.
View Mem0's documentation to learn more about integrating our memory features. You can also reach out to us at [email protected].
GET TLDR from:
Summarize
Website/Footer
Summarize
Website/Footer
Summarize
Website/Footer
Summarize
Website/Footer













