Mem0 provides a suite of tools for storing, searching, and retrieving memories, enabling agents to maintain context and learn from past interactions. The tools are built as Langchain tools, making them easily integrable with any AI agent implementation.
class Message(BaseModel): role: str = Field(description="Role of the message sender (user or assistant)") content: str = Field(description="Content of the message")class AddMemoryInput(BaseModel): messages: List[Message] = Field(description="List of messages to add to memory") user_id: str = Field(description="ID of the user associated with these messages") metadata: Optional[Dict[str, Any]] = Field(description="Additional metadata for the messages", default=None) class Config: json_schema_extra = { "examples": [{ "messages": [ {"role": "user", "content": "Hi, I'm Alex. I'm a vegetarian and I'm allergic to nuts."}, {"role": "assistant", "content": "Hello Alex! I've noted that you're a vegetarian and have a nut allergy."} ], "user_id": "alex", "metadata": {"food": "vegan"} }] }
def add_memory(messages: List[Message], user_id: str, metadata: Optional[Dict[str, Any]] = None) -> Any: """Add messages to memory with associated user ID and metadata.""" message_dicts = [msg.dict() for msg in messages] return client.add(message_dicts, user_id=user_id, metadata=metadata)add_tool = StructuredTool( name="add_memory", description="Add new messages to memory with associated metadata", func=add_memory, args_schema=AddMemoryInput)
def search_memory(query: str, filters: Dict[str, Any]) -> Any: """Search memory with the given query and filters.""" return client.search(query=query, filters=filters)search_tool = StructuredTool( name="search_memory", description="Search through memories with a query and filters", func=search_memory, args_schema=SearchMemoryInput)
All tools are implemented as Langchain StructuredTool instances, making them compatible with any AI agent that supports the Langchain tools interface. To use these tools with your agent:
Initialize the tools as shown above
Add the tools to your agent’s toolset
The agent can now use these tools to manage memories through natural language interactions
Each tool provides structured input validation through Pydantic models and returns consistent responses that can be processed by your agent.
LangChain Integration
Build conversational agents with LangChain and Mem0