Skip to main content
Integrate Mem0’s memory capabilities with OpenAI’s Agents SDK to create AI agents with persistent memory. You can create agents that remember past conversations and use that context to provide better responses.

Installation

First, install the required packages:
You’ll also need a custom agents framework for this implementation.

Setting Up Environment Variables

Store your Mem0 API key as an environment variable:
Or in your Python script:

Code Structure

The integration consists of three main components:
  1. Context Manager: Defines user context for memory operations
  2. Memory Tools: Functions to add, search, and retrieve memories
  3. Memory Agent: An agent configured to use these memory tools

Step-by-Step Implementation

1. Import Dependencies

2. Define Memory Context

3. Initialize the Mem0 Client

4. Create Memory Tools

Add to Memory

Search Memory

Get All Memories

5. Configure the Memory Agent

6. Implement the Main Runtime Loop

Usage Examples

Storing Information

Searching Memory

Retrieving All Memories

Advanced Configuration

Custom User IDs

You can specify different user IDs to maintain separate memory stores for multiple users:

Resources


OpenAI Tool Calls with Mem0

Extend OpenAI assistants with tool-based memory operations.

Build a Mem0 Companion

Learn the core patterns for memory-powered agents with any SDK.
Enjoying Mem0? Star us on GitHub — it takes two seconds and helps more developers discover open-source memory.