> ## Documentation Index
> Fetch the complete documentation index at: https://sambanova-systems.mintlify.site/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Mem0 integration guide

Mem0 is a memory engine designed to maintain contextual conversations, ensuring users don't have to repeat themselves and that your agents provide consistent, continuous responses. It offers an adaptive memory solution tailored for teams who want to manage everything on their own infrastructure. You retain full control over the stack, data, and customizations.

## Prerequisites

Before starting, ensure you have:

* A [SambaCloud](https://cloud.sambanova.ai/apis) account and API key.
* **An OpenAI API key.** Mem0 needs an embedding model in addition to a chat model, and [SambaNova embeddings are available on SambaStack only](/docs/en/features/embeddings). On SambaCloud you must supply the embedder from another provider, so `OPENAI_API_KEY` is required alongside your SambaNova key. See [How Mem0 uses SambaNova](#how-mem0-uses-sambanova) for which calls go where.
* Python 3.10 or later. The `langchain` and `langchain-sambanova` releases pinned by the example repository require 3.10, so `pip install -r requirements.txt` fails on 3.9.
* No vector store to install or run. Mem0 defaults to Qdrant in local embedded mode and writes to a directory on your machine.

<Note>
  macOS ships with Python 3.9 and exposes it as `python3` rather than `python`. Check your version with `python3 --version`. If it is below 3.10, install a supported version with [pyenv](https://github.com/pyenv/pyenv), [uv](https://docs.astral.sh/uv/), or `brew install python@3.12` before continuing.
</Note>

## How Mem0 uses SambaNova

Mem0 makes three different kinds of calls, and each one resolves to a provider independently. Knowing which is which explains why you need two keys.

| Role             | What it does                                                                              | Provider used in this guide     |
| :--------------- | :---------------------------------------------------------------------------------------- | :------------------------------ |
| Chat completions | Generates the reply your user sees                                                        | SambaNova `gpt-oss-120b`        |
| Memory LLM       | Extracts facts from a conversation and decides whether to add, update, or delete a memory | SambaNova `gpt-oss-120b`        |
| Embedder         | Converts memories and queries into vectors for similarity search                          | OpenAI `text-embedding-3-small` |

<Warning>
  If you leave the `llm` block out of your Mem0 config, Mem0 does not fall back to SambaNova. It defaults to `provider: "openai"` with the model `gpt-4.1-nano-2025-04-14` and reads `OPENAI_API_KEY`, so your memory extraction silently runs on OpenAI instead. Set the `llm` block explicitly, as shown below, to keep that work on SambaNova.
</Warning>

## Quickstart

This quickstart is self-contained. It stores a memory, recalls it on a later turn, and feeds it back into the prompt, without cloning anything.

### Install Mem0

```bash theme={}
pip install "mem0ai==1.0.1"
```

Pin the version. Mem0's configuration schema changes between major versions, and the config below is written against the 1.x schema. `mem0ai` installs the `openai` client as a dependency, so you do not need to install it separately.

### Set your API keys

```bash theme={}
export SAMBANOVA_API_KEY="your-sambanova-api-key"
export OPENAI_API_KEY="your-openai-api-key"
```

### Store and recall a memory

Save the following as `quickstart.py` and run it with `python quickstart.py`.

```python theme={}
import os

from mem0 import Memory
from openai import OpenAI

SAMBANOVA_BASE_URL = "https://api.sambanova.ai/v1"
CHAT_MODEL = "gpt-oss-120b"
USER_ID = "alice"

# Both lookups raise KeyError immediately if the variable is unset, so a missing
# key fails before the first request instead of surfacing as a 401 later.
sambanova_api_key = os.environ["SAMBANOVA_API_KEY"]
openai_api_key = os.environ["OPENAI_API_KEY"]

config = {
    "llm": {
        "provider": "openai",
        "config": {
            "model": CHAT_MODEL,
            "openai_base_url": SAMBANOVA_BASE_URL,
            "api_key": sambanova_api_key,
        },
    },
    "embedder": {
        "provider": "openai",
        "config": {
            "model": "text-embedding-3-small",
            "embedding_dims": 1536,
            "api_key": openai_api_key,
        },
    },
    "vector_store": {
        "provider": "qdrant",
        "config": {
            "collection_name": "sambanova_mem0",
            "embedding_model_dims": 1536,
            "path": "./.qdrant",
            "on_disk": True,
        },
    },
}

memory = Memory.from_config(config)
client = OpenAI(api_key=sambanova_api_key, base_url=SAMBANOVA_BASE_URL)


def chat(message: str) -> str:
    # 1. Recall memories relevant to this message.
    recalled = memory.search(query=message, user_id=USER_ID, limit=3).get("results", [])
    context = "\n".join(f"- {item['memory']}" for item in recalled) or "No relevant memories yet."
    print(f"[recalled]\n{context}\n")

    # 2. Use them in the prompt.
    response = client.chat.completions.create(
        model=CHAT_MODEL,
        messages=[
            {
                "role": "system",
                "content": f"You are a helpful assistant. Use these memories only if relevant:\n{context}",
            },
            {"role": "user", "content": message},
        ],
    )
    reply = response.choices[0].message.content

    # 3. Store this turn so later turns can recall it.
    memory.add(
        [
            {"role": "user", "content": message},
            {"role": "assistant", "content": reply},
        ],
        user_id=USER_ID,
    )
    return reply


print(chat("I'm allergic to peanuts, and I love Thai food."))
print(chat("What should I order for dinner?"))
```

Two details in that config matter more than they look:

* `openai_base_url` is the key Mem0 reads to redirect the OpenAI-compatible client. Spelling it `base_url` raises `TypeError: __init__() got an unexpected keyword argument 'base_url'`.
* The `embedder` block deliberately has no `openai_base_url`, so it targets `https://api.openai.com/v1`. Do not set an `OPENAI_BASE_URL` environment variable, because Mem0's embedder reads it and would send embedding requests to SambaNova, which does not serve them on SambaCloud.

### Confirm it worked

The first turn has nothing to recall. The second turn is the proof: the memory extracted from turn one comes back and reaches the prompt.

```text theme={}
[recalled]
No relevant memories yet.

<reply to your first message>

[recalled]
- Is allergic to peanuts
- Loves Thai food

<dinner suggestion that avoids peanuts>
```

The exact wording of the extracted memories and of both replies varies between runs. What confirms the integration works is that the second `[recalled]` block is not empty.

To list everything stored for a user, append the following to `quickstart.py`, which reuses the `memory` object and `USER_ID` defined above:

```python theme={}
for item in memory.get_all(user_id=USER_ID)["results"]:
    print(item["id"], "|", item["memory"])
```

<Note>
  `on_disk` is set to `True` on purpose. Mem0's local Qdrant store defaults to `on_disk: False`, and in that mode it deletes the store directory every time you construct a `Memory` object, so memories do not survive a restart. Setting `on_disk: True` keeps them across runs.
</Note>

## Run the example application

The [sambanova/integrations](https://github.com/sambanova/integrations/tree/main/mem0) repository has a longer example that wraps the same add and search loop in an interactive REPL.

### Clone the repository

```bash theme={}
git clone https://github.com/sambanova/integrations.git
cd integrations/mem0
```

### Create a virtual environment

```bash theme={}
python3 -m venv .venv
source .venv/bin/activate
```

Confirm the environment uses a supported version before installing:

```bash theme={}
python --version
```

### Install dependencies

```bash theme={}
pip install -r requirements.txt
```

### Set environment variables

Create a `.env` file in the project directory with both keys:

```bash theme={}
SAMBANOVA_API_KEY=your-sambanova-api-key
OPENAI_API_KEY=your-openai-api-key
```

Alternatively, export the variables directly in your terminal:

```bash theme={}
export SAMBANOVA_API_KEY="your-sambanova-api-key"
export OPENAI_API_KEY="your-openai-api-key"
```

### Run the script

This script initializes a Mem0 memory client connected to SambaNova and starts an interactive chat loop, so you can see memory persist across turns by asking follow-up questions. Type `exit` to quit.

<Warning>
  The example sets its embedding model to `E5-Mistral-7B-Instruct`, which was [removed from SambaCloud on April 6, 2026](/docs/en/models/deprecations) and is [available on SambaStack only](/docs/en/features/embeddings). With a SambaCloud key, every memory write and lookup fails. Substituting a different SambaNova embedding model does not help, because SambaCloud does not currently serve an embeddings endpoint.

  To run the example against SambaCloud, edit `main.py` to use a non-SambaNova embedder. Replace the `embedder` block with the OpenAI embedder from the [Quickstart](#store-and-recall-a-memory) above and set `embedding_model_dims` to `1536` to match `text-embedding-3-small`; the example currently sets it to `4096` for E5-Mistral. Otherwise, point the example at a SambaStack deployment that serves the embeddings endpoint.

  The example also omits the `llm` block, so its memory extraction runs on OpenAI rather than SambaNova. Add the `llm` block from the Quickstart to keep it on SambaNova.
</Warning>

```bash theme={}
python main.py
```

The script prints a banner and then waits for input:

```text theme={}
Chat with AI memory system (type 'exit' to quit)

You:
```

The prompt appears before any model call is made, so reaching it confirms only that the dependencies imported. Configuration and key problems surface on your first message, not at startup.

The full source code and additional examples are available in the [Mem0 integration example](https://github.com/sambanova/integrations/tree/main/mem0) on GitHub.

## Troubleshooting

<AccordionGroup>
  <Accordion title="command not found: python">
    macOS does not provide a `python` executable, only `python3`. Use `python3 -m venv .venv` to create the environment. After you activate it with `source .venv/bin/activate`, `python` works as expected inside the environment.
  </Accordion>

  <Accordion title="pip install fails on the langchain requirement">
    The pinned `langchain` and `langchain-sambanova` releases require Python 3.10 or later. Check the interpreter inside your activated environment with `python --version`, and rebuild the environment with a supported interpreter if it reports 3.9.
  </Accordion>

  <Accordion title="The chat loop starts, then fails on the first message">
    The prompt appears before any model call happens, so an unusable configuration is not visible until you send your first message. That turn makes three calls in order: a memory search against the embeddings endpoint, a chat completion, then a memory write that uses both again. The embeddings call is first, so it is the one that usually fails first. Read the error text to see which endpoint rejected the request.
  </Accordion>

  <Accordion title="AuthenticationError: Error code: 401">
    One of the two keys is unset or wrong. Read the message body to tell which: a 401 that points you at `platform.openai.com` came from the embedder, so check `OPENAI_API_KEY`. Any other 401 came from the chat or memory LLM call, so check `SAMBANOVA_API_KEY` and copy it again from your [SambaCloud portal](https://cloud.sambanova.ai/apis).

    Because the first thing each turn does is a memory search, the embedder is the first call to fail, so an OpenAI 401 can appear even when your SambaNova key is fine. The example's `main.py` also calls `os.environ.setdefault`, so it substitutes a placeholder rather than failing at startup and the error only surfaces on the first message. The Quickstart reads keys with `os.environ[...]` instead, which fails immediately when a key is missing.
  </Accordion>

  <Accordion title="Memory writes and lookups fail against the embeddings endpoint">
    SambaNova embeddings are [available on SambaStack only](/docs/en/features/embeddings), and `E5-Mistral-7B-Instruct` was [removed from SambaCloud on April 6, 2026](/docs/en/models/deprecations). A SambaCloud key cannot reach that endpoint. Configure a non-SambaNova embedder as shown in the Quickstart, or use a SambaStack deployment.
  </Accordion>

  <Accordion title="OPENAI_API_KEY errors even though you are using SambaNova">
    Expected. Mem0 needs an embedder, and SambaCloud does not serve embeddings, so the embedder runs on OpenAI and reads `OPENAI_API_KEY`. Both keys must be set. If you also left the `llm` block out of your config, Mem0's memory LLM defaults to OpenAI as well and reads the same key.
  </Accordion>

  <Accordion title="TypeError: __init__() got an unexpected keyword argument 'base_url'">
    Mem0's OpenAI-compatible provider config uses `openai_base_url`, not `base_url`. Rename the key inside the `llm` config block.
  </Accordion>

  <Accordion title="Requests go to OpenAI even though openai_base_url points at SambaNova">
    Check that the `llm` block is present and that `provider` is `openai` with `openai_base_url` set to `https://api.sambanova.ai/v1`. Mem0 rejects `provider: "sambanova"` with `Unsupported LLM provider`, because it reaches SambaNova through the OpenAI-compatible client rather than a dedicated provider.
  </Accordion>

  <Accordion title="Memories are empty every time you restart">
    Mem0's local Qdrant store defaults to `on_disk: False`, and in that mode it deletes the store directory when you construct a `Memory` object. Set `on_disk` to `True` in the `vector_store` config to persist memories across runs.
  </Accordion>

  <Accordion title="Recall returns nothing on the second turn">
    A memory write runs the conversation through the memory LLM to extract facts, so a turn that contains no durable fact produces no memory. Confirm what was stored with `memory.get_all(user_id=USER_ID)`. Also check that `search` and `add` use the same `user_id`, since memories are scoped per user.
  </Accordion>

  <Accordion title="Vector dimensions do not match after changing embedders">
    `embedding_model_dims` in the `vector_store` config must match the embedding model's output size: `1536` for `text-embedding-3-small`, `4096` for `E5-Mistral-7B-Instruct`. After changing embedders, delete the local store directory so the collection is recreated at the new size.
  </Accordion>
</AccordionGroup>

## Mem0 documentation

For more information about Mem0, see the [official Mem0 documentation](https://docs.mem0.ai/introduction).
