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

# Multi-Tier Memory

> MemoryRouter backends with runtime switching — in-memory, Redis, MongoDB, and SQL database storage

# Multi-Tier Memory System

**Runtime-switchable memory backends** — start in memory, switch to Redis,
MongoDB, or SQL database storage when the application needs persistence.

***

## Quick Start

The default `in_memory` backend is enough for local development and tests:

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
import asyncio
from omnicoreagent import OmniCoreAgent, MemoryRouter

async def main():
    agent = OmniCoreAgent(
        name="memory_agent",
        system_instruction="Remember useful details inside the active session.",
        model_config={"provider": "openai", "model": "gpt-4o"},
        memory_router=MemoryRouter("in_memory"),
    )

    await agent.run("My project is called Atlas.", session_id="user_123")
    result = await agent.run("What is my project called?", session_id="user_123")
    print(result["response"])
    await agent.cleanup()

asyncio.run(main())
```

Install the backend extra before using a durable store:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
pip install "omnicoreagent[redis]"
pip install "omnicoreagent[mongodb]"
pip install "omnicoreagent[postgres]"
```

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from omnicoreagent import OmniCoreAgent, MemoryRouter

# Start with Redis
agent = OmniCoreAgent(
    name="my_agent",
    system_instruction="Remember useful details inside the active session.",
    memory_router=MemoryRouter("redis"),
    model_config={"provider": "openai", "model": "gpt-4o"}
)

# Switch at runtime — no restart needed!
await agent.switch_memory_store("mongodb")     # Switch to MongoDB
await agent.switch_memory_store("sql")         # Switch to SQL database storage
await agent.switch_memory_store("in_memory")   # Switch to in-memory
await agent.switch_memory_store("redis")       # Back to Redis
```

***

## Available Backends

| Backend     | Use Case                    | Environment Variable |
| ----------- | --------------------------- | -------------------- |
| `in_memory` | Fast development            | —                    |
| `redis`     | Production persistence      | `REDIS_URL`          |
| `sql`       | SQLAlchemy database storage | `DATABASE_URL`       |
| `mongodb`   | Document storage            | `MONGODB_URI`        |

***

## Conversation Summarization

OmniCoreAgent includes **automatic conversation summarization** to manage long conversation histories efficiently. When enabled, older messages are condensed into summaries, keeping context while reducing token usage.

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
from omnicoreagent import OmniCoreAgent, MemoryRouter

memory_router = MemoryRouter("redis")

memory_config = {
    "mode": "sliding_window",  # or "token_budget"
    "value": 10,  # Keep last 10 messages, or max tokens in token_budget mode
    "summary": {
        "enabled": True,
        "retention_policy": "keep"  # Options: "keep" or "delete"
    }
}

agent = OmniCoreAgent(
    name="summarizing_agent",
    memory_router=memory_router,
    model_config={"provider": "openai", "model": "gpt-4o"},
    agent_config={"memory_config": memory_config},
)
```

### Summarization Modes

| Mode             | Description                                | Best For                |
| ---------------- | ------------------------------------------ | ----------------------- |
| `sliding_window` | Keep last N messages, summarize older ones | Predictable memory size |
| `token_budget`   | Keep messages within token limit           | Cost optimization       |

### Retention Policies

| Policy   | Behavior                                           |
| -------- | -------------------------------------------------- |
| `keep`   | Mark summarized messages as inactive (recoverable) |
| `delete` | Permanently remove summarized messages             |

### How It Works

1. When conversation exceeds configured limit → summarization triggers
2. Older messages are sent to LLM for summary generation
3. Summary replaces older messages in active context
4. Original messages are retained (with `"keep"`) or deleted per policy

<Tip>
  Enable summarization for long-running conversations (support bots, research assistants) to maintain context while controlling costs. Use `sliding_window` for predictable behavior, `token_budget` for strict cost control.
</Tip>
