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

# RAG Scoping & Filtering

> Filter retrieval with user_id, agent_id, and run_id for multi-tenant isolation

Scope knowledge retrieval per user, agent, or session so tenants and conversations stay isolated.

```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
from praisonaiagents import Agent, MemoryConfig

agent = Agent(
    name="SupportBot",
    instructions="Answer from the customer's knowledge only.",
    knowledge=["./support_docs/"],
    memory=MemoryConfig(user_id="customer_42"),
)

agent.start("What was the status of my refund request?")
```

The user asks in a scoped session; retrieval filters by tenant and user so answers stay isolated.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    Q[Query + scopes] --> S[Vector search]
    S --> F[Apply filters]
    F --> R[Scoped results]

    classDef query fill:#8B0000,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class Q query
    class S,F process
    class R output
```

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant RagScopingFiltering

    User->>Agent: Request
    Agent->>RagScopingFiltering: Process
    RagScopingFiltering-->>Agent: Result
    Agent-->>User: Response
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Set `user_id` via memory config — the agent passes it to every knowledge search:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent, MemoryConfig

    agent = Agent(
        name="PersonalAssistant",
        instructions="You are a personal assistant.",
        knowledge=["./documents/"],
        memory=MemoryConfig(user_id="alice"),
    )

    agent.start("Find my recent notes about the project")
    ```
  </Step>

  <Step title="With Configuration">
    Combine scopes for multi-tenant SaaS — pass `run_id` on retrieve for session-specific context:

    ```python theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
    from praisonaiagents import Agent, MemoryConfig

    session_id = "session_2026_05_30"

    agent = Agent(
        name="SupportBot",
        instructions="Answer customer questions using their session history.",
        knowledge=["./support_docs/"],
        memory=MemoryConfig(user_id="customer_42"),
    )

    agent.retrieve("Refund status?", run_id=session_id)
    ```
  </Step>
</Steps>

***

## How It Works

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
sequenceDiagram
    participant User
    participant Agent
    participant Feature as RAG Scoping & Filtering

    User->>Agent: Request
    Agent->>Feature: Process request
    Feature-->>Agent: Result
    Agent-->>User: Response
```

The agent forwards `user_id` and `agent_id` (auto-generated) to the knowledge backend. Vector stores combine filters with `$and` when multiple scopes are set.

| Scope      | Source                                    | Purpose               |
| ---------- | ----------------------------------------- | --------------------- |
| `user_id`  | `MemoryConfig(user_id=…)`                 | Per-user isolation    |
| `agent_id` | `agent.agent_id` (auto)                   | Per-agent isolation   |
| `run_id`   | `retrieve(…, run_id=…)` or store metadata | Per-session isolation |

***

## Configuration Options

| Option     | Type  | Default     | Description                      |
| ---------- | ----- | ----------- | -------------------------------- |
| `user_id`  | `str` | `"praison"` | User scope on `MemoryConfig`     |
| `agent_id` | `str` | auto UUID   | Agent scope (read-only property) |
| `run_id`   | `str` | `None`      | Session scope on retrieve/store  |

Backends: **ChromaDB** supports all scopes; **mem0** requires at least one scope identifier.

***

## Best Practices

<AccordionGroup>
  <Accordion title="Set user_id for tenant isolation">
    Use `memory=MemoryConfig(user_id=customer_id)` in SaaS apps — each customer's retrieval stays separate.
  </Accordion>

  <Accordion title="Pass run_id for ephemeral sessions">
    Scope temporary conversation context with `agent.retrieve(query, run_id=session_id)`.
  </Accordion>

  <Accordion title="Pick the backend for your scoping needs">
    mem0 enforces scope at ingest; ChromaDB filters at query time with combined `$and` clauses.
  </Accordion>

  <Accordion title="Use consistent identifier naming">
    Prefer readable IDs like `customer_alice_123` over opaque tokens for easier debugging.
  </Accordion>
</AccordionGroup>

***

## Related

<CardGroup cols={2}>
  <Card title="Knowledge Backends" icon="database" href="/docs/features/knowledge-backends">
    Vector store backends and scoping support
  </Card>

  <Card title="Knowledge" icon="book" href="/docs/features/knowledge">
    Configure sources and retrieval
  </Card>
</CardGroup>
