> ## 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 & Knowledge

> Add knowledge bases and retrieval-augmented generation to your agents

Learn how to enhance your agents with knowledge bases and retrieval-augmented generation (RAG).

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

agent = Agent(
    name="Knowledge Assistant",
    instructions="Answer from the knowledge base.",
    knowledge=KnowledgeConfig(sources=["./docs"]),
)

agent.start("Find the section about session persistence.")
```

The user attaches a knowledge source, asks a question, and the agent grounds its reply in retrieved passages.

```mermaid theme={"theme":{"light":"vitesse-light","dark":"vitesse-dark"}}
graph LR
    subgraph "RAG"
        U[📋 Question] --> A[🔍 Retrieve]
        A --> O[✅ Grounded Answer]
    end

    classDef input fill:#6366F1,stroke:#7C90A0,color:#fff
    classDef process fill:#189AB4,stroke:#7C90A0,color:#fff
    classDef output fill:#10B981,stroke:#7C90A0,color:#fff

    class U input
    class A process
    class O output
```

## Quick Start

<Steps>
  <Step title="Simple Usage">
    Point the agent at a folder of documents and it answers from them.

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

    agent = Agent(
        name="Knowledge Assistant",
        instructions="Answer from the knowledge base.",
        knowledge=KnowledgeConfig(sources=["./docs"]),
    )
    agent.start("Find the section about session persistence.")
    ```
  </Step>

  <Step title="With Configuration">
    Tune how many passages come back and how similar they must be.

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

    agent = Agent(
        name="Knowledge Assistant",
        instructions="Cite retrieved passages.",
        knowledge=KnowledgeConfig(sources=["./docs"], top_k=5, similarity_threshold=0.7),
    )
    agent.start("Summarise the security section.")
    ```
  </Step>
</Steps>

***

## How It Works

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

    User->>Agent: Ask a question
    Agent->>Knowledge: Retrieve top-k passages
    Knowledge-->>Agent: Matching chunks
    Agent-->>User: Answer grounded in sources
```

***

## Related

<CardGroup cols={2}>
  <Card title="Knowledge Base Setup" icon="database" href="/docs/guides/rag/knowledge-base">
    Create and configure knowledge bases
  </Card>

  <Card title="Chunking Strategies" icon="scissors" href="/docs/guides/rag/chunking">
    Optimize document chunking
  </Card>

  <Card title="Retrieval Methods" icon="magnifying-glass" href="/docs/guides/rag/retrieval">
    Configure retrieval strategies
  </Card>
</CardGroup>

***

## Best Practices

<AccordionGroup>
  <Accordion title="Start with a folder of clean sources">
    `KnowledgeConfig(sources=["./docs"])` indexes a directory directly. Remove boilerplate and duplicates first so retrieval returns signal, not noise.
  </Accordion>

  <Accordion title="Raise the similarity threshold to cut noise">
    A higher `similarity_threshold` filters weak matches. Start at `0.7` and adjust based on whether answers miss context or cite irrelevant passages.
  </Accordion>

  <Accordion title="Keep top_k small for focused answers">
    Fewer, stronger passages usually beat many weak ones. Increase `top_k` only when answers lack coverage.
  </Accordion>
</AccordionGroup>
