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

# Use Mem0 storage

The `mem0` backend uses [Mem0](https://mem0.ai/) as long-term storage. It is a managed service where Mem0 handles memory extraction, storage, and retrieval, requiring no self-hosted vector store or local embedding — **recommended for production**.

## When to use

* Production needing a managed memory service;
* You want Mem0 to automatically extract and manage memories;
* You have a Mem0 account and API key, or can auto-fetch one via Volcengine credentials.

## Dependencies

```bash lines theme={null}
pip install mem0
```

## Usage

```python lines theme={null}
from veadk import Agent
from veadk.memory.long_term_memory import LongTermMemory

ltm = LongTermMemory(backend="mem0", app_name="ltm_demo")

agent = Agent(
    name="demo",
    instruction="Answer the user; when needed, use the `load_memory` tool to recall past conversations.",
    long_term_memory=ltm,
)
```

You can also pass config explicitly via `backend_config`:

```python lines theme={null}
from veadk.memory.long_term_memory import LongTermMemory
from veadk.configs.database_configs import Mem0Config

ltm = LongTermMemory(
    backend="mem0",
    index="ltm_demo",
    backend_config={
        "index": "ltm_demo",
        "mem0_config": Mem0Config(
            api_key="your-mem0-api-key",
            base_url="https://api.mem0.ai/v1",
        ),
    },
)
```

## Parameters

### Constructor parameters

`backend_config` supports the following settings:

| Parameter | Type | Default | Description |
| :- | :- | :- | :- |
| `index` | `str` | No default; provided by `LongTermMemory` | Memory index name. The Mem0 backend imposes no naming constraints. |
| `mem0_config` | `Mem0Config` | Read automatically from `DATABASE_MEM0_*` env vars | Mem0 client config. |

### Mem0 config

`mem0_config` is a `Mem0Config` with env prefix `DATABASE_MEM0_`:

| Field | Env var | Type | Default | Description |
| :- | :- | :- | :- | :- |
| `api_key` | `DATABASE_MEM0_API_KEY` | `str` | `""` | Mem0 API key. |
| `api_key_id` | `DATABASE_MEM0_API_KEY_ID` | `str` | `""` | Used to auto-fetch the API key; provide this or `project_id`. |
| `project_id` | `DATABASE_MEM0_PROJECT_ID` | `str` | `""` | Used to auto-fetch the API key. |
| `base_url` | `DATABASE_MEM0_BASE_URL` | `str` | `""` | Mem0 service endpoint, e.g. `https://api.mem0.ai/v1`. |

### API key resolution order

If `api_key` is provided directly, it is used as is; otherwise VeADK tries to fetch the API key via `api_key_id` or `project_id`; initialization fails if neither is set.

## Environment variables

```bash lines theme={null}
# Option 1: provide the API key directly
export DATABASE_MEM0_API_KEY="your-mem0-api-key"
export DATABASE_MEM0_BASE_URL="https://api.mem0.ai/v1"

# Option 2: auto-fetch via api_key_id or project_id
export DATABASE_MEM0_API_KEY_ID="your-api-key-id"
export DATABASE_MEM0_PROJECT_ID="your-project-id"
```

<Note>
  If `api_key` is not provided directly, VeADK tries to obtain it via `api_key_id` or `project_id`; an error is raised if neither is set.
</Note>
