> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://respan.ai/docs/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://respan.ai/docs/_mcp/server.

# Mem0

> Use Mem0 with Respan — gateway routing for memory-backed LLM apps with full observability.

[Mem0](https://mem0.ai/) is a self-improving memory layer for LLM applications, enabling personalized AI experiences. Use it with Respan to add memory to your AI product and route LLM calls through the Respan gateway for full observability over every memory add, search, and generation.

#### Set up Respan

Create an account at [platform.respan.ai](https://platform.respan.ai) and grab an [API key](https://platform.respan.ai/platform/gateway/api-keys). For gateway, also add [credits](https://platform.respan.ai/platform/gateway/credits) or a [provider key](https://platform.respan.ai/platform/gateway/api-keys?providers=1).

Run `npx @respan/cli setup` to set up with your coding agent.

#### Gateway

> **Note**
>
> Mem0 doesn't expose its own SDK-level tracing instrumentor; route all calls through the Respan gateway to get full observability automatically.

## Setup

#### Prerequisites

* A [Respan API key](https://platform.respan.ai/platform/gateway/api-keys)
* A [Mem0 API key](https://app.mem0.ai/dashboard/api-keys)

#### Install Mem0

```bash
pip install mem0ai
```

#### Set environment variables

```bash
export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
export MEM0_API_KEY="YOUR_MEM0_API_KEY"
```

#### Add memory with Mem0 SDK

Configure Mem0's LLM provider to use the Respan gateway.

```python
import os
from mem0 import Memory

config = {
    "llm": {
        "provider": "openai",
        "config": {
            "model": "gpt-4.1-nano",
            "temperature": 0.0,
            "api_key": os.environ["RESPAN_API_KEY"],
            "openai_base_url": "https://api.respan.ai/api/",
        },
    }
}

m = Memory.from_config(config_dict=config)

result = m.add(
    "I like to take long walks on weekends.",
    user_id="alice",
    metadata={"category": "hobbies"},
)
print(result)
```

## Add memory with the OpenAI SDK

Use the OpenAI SDK pointed at the Respan gateway and pass `mem0_params` via `extra_body` to add or search memories alongside the LLM call.

```python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url="https://api.respan.ai/api/",
)

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Context: I like eating carrots, and I like to play basketball."},
]

response = client.chat.completions.create(
    model="gpt-4.1-nano",
    messages=messages,
    extra_body={
        "mem0_params": {
            "user_id": "user_1",
            "org_id": "org_1",
            "api_key": os.environ["MEM0_API_KEY"],
            "add_memories": {"messages": messages},
        }
    },
)
print(response.choices[0].message.content)
```

## Search memories

Pass `search_memories` to retrieve relevant memories before generation.

```python
response = client.chat.completions.create(
    model="gpt-4.1-nano",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Based on {{mem0_search_memories_response}}, what is the user's favorite food?"},
    ],
    extra_body={
        "mem0_params": {
            "user_id": "user_1",
            "org_id": "org_1",
            "api_key": os.environ["MEM0_API_KEY"],
            "search_memories": {
                "query": "What is the user's favorite food?",
                "top_k": 1,
                "fields": ["text"],
                "rerank": True,
                "keyword_search": True,
                "filter_memories": True,
            },
        }
    },
)
```

## Use the Respan SDK for type-safe params

```bash
pip install respan-sdk
```

```python
import os
from openai import OpenAI
from respan_sdk.respan_types.services_types.mem0_types import (
    Mem0Params, AddMemoriesParams, SearchMemoriesParams,
)

client = OpenAI(
    api_key=os.environ["RESPAN_API_KEY"],
    base_url="https://api.respan.ai/api/",
)

memory_msgs = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "Context: I like eating carrots."},
]

response = client.chat.completions.create(
    model="gpt-4.1-nano",
    messages=memory_msgs,
    extra_body={
        "mem0_params": Mem0Params(
            user_id="user_1",
            org_id="org_1",
            api_key=os.environ["MEM0_API_KEY"],
            add_memories=AddMemoriesParams(messages=memory_msgs),
        ).model_dump(exclude_none=True),
    },
)
```