> ## 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 Redis storage

The `redis` backend uses Redis as the vector store. Memory text is embedded by an embedding model, written to Redis with a per-user index (the actual index name is `veadk-ltm/{index}/{user_id}`), and retrieved by cosine similarity.

## When to use

* You already run Redis with the **RediSearch** module enabled;
* You want Redis as a low-latency vector-search backend, with persistence and multi-instance sharing.

## Dependencies

```bash lines theme={null}
pip install "veadk-python[extensions]"
```

Redis must support RediSearch (vector search).

## Usage

Before running, provide Redis with RediSearch, an account allowed to create and access indexes, and the Redis and embedding variables below. Durability depends on Redis RDB/AOF and backup settings; VeADK does not enable them

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

ltm = LongTermMemory(backend="redis", index="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 connection config explicitly via `backend_config`:

```python lines theme={null}
import os

from veadk.memory.long_term_memory import LongTermMemory
from veadk.configs.database_configs import RedisConfig

ltm = LongTermMemory(
    backend="redis",
    index="ltm_demo",
    backend_config={
        "index": "ltm_demo",
        "redis_config": RedisConfig(
            host="localhost",
            port=6379,
            password=os.environ["DATABASE_REDIS_PASSWORD"],
            db=0,
        ),
    },
)
```

## Parameters

### LongTermMemory parameters

| Parameter | Type | Default | Description |
| - | - | - | - |
| `backend` | `str \| BaseLongTermMemoryBackend` | `"opensearch"` | Set to `redis` for this page, or pass a configured backend instance |
| `backend_config` | `dict` | `{}` | Backend settings; a supplied backend instance takes precedence |
| `index` | `str` | `""` | Without backend\_config, resolves from index, app\_name, then default\_app; supply a nonempty index with a configuration dictionary |
| `app_name` | `str` | `""` | Fallback for index; the actual user comes from the saved Session or search arguments |
| `top_k` | `int` | `5` | Number of retrieved chunks; use a positive integer |
| `user_id` | `str` | `""` | Deprecated; does not select the runtime user |

### Constructor parameters

`backend_config` supports the following settings:

| Parameter | Type | Default | Description |
| :- | :- | :- | :- |
| `index` | `str` | No default; provided by `LongTermMemory` | Memory index name. The Redis backend imposes no extra naming constraints. |
| `redis_config` | `RedisConfig` | Read automatically from `DATABASE_REDIS_*` env vars | Redis connection config. |
| `embedding_config` | `EmbeddingModelConfig` | Read automatically from `MODEL_EMBEDDING_*` env vars | Embedding model config. |

### Redis connection config

`redis_config` is a `RedisConfig` with env prefix `DATABASE_REDIS_`:

| Field | Env var | Type | Default | Description |
| :- | :- | :- | :- | :- |
| `host` | `DATABASE_REDIS_HOST` | `str` | `""` | Redis host. |
| `port` | `DATABASE_REDIS_PORT` | `int` | `6379` | Port. |
| `username` | `DATABASE_REDIS_USERNAME` | `str \| None` | `None` | The field exists, but Redis long-term memory connections do not forward username; this field does not select an ACL user |
| `password` | `DATABASE_REDIS_PASSWORD` | `str` | `""` | Password. |
| `db` | `DATABASE_REDIS_DB` | `int` | `0` | Database number. |
| `secret_token` | `DATABASE_REDIS_SECRET_TOKEN` | `str` | `""` | STS temporary-credential token; not yet enabled. |

### Embedding config

`embedding_config` is an `EmbeddingModelConfig` with env prefix `MODEL_EMBEDDING_`:

| Field | Env var | Type | Default | Description |
| :- | :- | :- | :- | :- |
| `name` | `MODEL_EMBEDDING_NAME` | `str` | `doubao-embedding-vision-250615` | Embedding model name. |
| `dim` | `MODEL_EMBEDDING_DIM` | `int` | `2048` | Embedding vector dimension; used to build the vector field. Distance metric is cosine. |
| `api_base` | `MODEL_EMBEDDING_API_BASE` | `str` | `https://ark.cn-beijing.volces.com/api/v3/` | API base of the embedding service. |
| `api_key` | `MODEL_EMBEDDING_API_KEY` | `str` | Falls back to `MODEL_AGENT_API_KEY`, then an auto-fetched Ark token | Key for accessing the embedding service. |

## Environment variables

```bash lines theme={null}
# Redis connection
export DATABASE_REDIS_HOST="localhost"
export DATABASE_REDIS_PORT=6379
export DATABASE_REDIS_PASSWORD="your-password"
export DATABASE_REDIS_DB=0

# Embedding model
export MODEL_EMBEDDING_NAME="doubao-embedding-vision-250615"
export MODEL_EMBEDDING_DIM=2048
export MODEL_EMBEDDING_API_KEY="your-ark-api-key"
```

<Note>
  The vector dimension comes from the embedding config (`MODEL_EMBEDDING_DIM`). The index uses the flat algorithm with a cosine distance metric, and an index is created per user (`veadk-ltm/{index}/{user_id}`).
</Note>

## Verify writes and retrieval

After configuring the dependencies and credentials on this page, run this standalone example. It saves user text and searches for that user directly without calling a conversation model

```python lines theme={null}
import asyncio

from google.adk.events import Event
from google.adk.sessions import Session
from google.genai import types
from veadk.memory.long_term_memory import LongTermMemory

async def main():
    memory = LongTermMemory(backend="redis", index="ltm_demo")
    session = Session(
        id="memory_check", app_name="ltm_demo", user_id="user_42",
        events=[Event(author="user", content=types.Content(
            role="user", parts=[types.Part(text="My preferred language is Chinese")]
        ))],
    )
    await memory.add_session_to_memory(session)
    result = await memory.search_memory(
        app_name="ltm_demo", user_id="user_42", query="preferred language"
    )
    for entry in result.memories:
        print(entry.content)

asyncio.run(main())
```

Results should contain the saved language preference. Managed services may extract memories asynchronously, so a completed write does not guarantee immediate retrieval. An empty result can also indicate permission, network, or service failure; check error logs and service records. The save method does not return a success Boolean
