# Agentic RAG With Lightrag (/examples/integrations/rag/agentic-rag-with-lightrag)



Load a PDF, a Wikipedia topic, and a URL into a LightRAG-backed knowledge base and query it with an agent.

```python title="agentic_rag_with_lightrag.py"
"""
Agentic Rag With Lightrag
=============================

Demonstrates an agentic RAG flow backed by LightRAG (relocated integration example).
"""

import asyncio
from os import getenv

from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reader.wikipedia_reader import WikipediaReader
from agno.vectordb.lightrag import LightRag

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
vector_db = LightRag(api_key=getenv("LIGHTRAG_API_KEY"))

knowledge = Knowledge(
    name="My LightRag Knowledge Base",
    description="Knowledge base using a LightRag vector database",
    vector_db=vector_db,
)

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
    knowledge=knowledge,
    search_knowledge=True,
    read_chat_history=False,
)

# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
    asyncio.run(
        knowledge.ainsert(
            name="Recipes",
            path="cookbook/07_knowledge/testing_resources/cv_1.pdf",
            metadata={"doc_type": "recipe_book"},
        )
    )

    asyncio.run(
        knowledge.ainsert(
            name="Recipes",
            topics=["Manchester United"],
            reader=WikipediaReader(),
        )
    )

    asyncio.run(
        knowledge.ainsert(
            name="Recipes",
            url="https://en.wikipedia.org/wiki/Manchester_United_F.C.",
        )
    )

    asyncio.run(
        agent.aprint_response("What skills does Jordan Mitchell have?", markdown=True)
    )

    asyncio.run(
        agent.aprint_response(
            "In what year did Manchester United change their name?", markdown=True
        )
    )
```

The local file is a CV despite the retained `Recipes` label and metadata. Give the three inserts distinct names such as `Candidate CV`, `Manchester United topic`, and `Manchester United page` when adapting the recipe, so their labels identify the source content.

## Run the Example [#run-the-example]

<Steps>
    <Step title="Set up your virtual environment">
      <CodeBlockTabs defaultValue="Mac">
        <CodeBlockTabsList>
          <CodeBlockTabsTrigger value="Mac">
            Mac
          </CodeBlockTabsTrigger>

          <CodeBlockTabsTrigger value="Windows">
            Windows
          </CodeBlockTabsTrigger>
        </CodeBlockTabsList>

        <CodeBlockTab value="Mac">
          ```bash
          uv venv --python 3.12
          source .venv/bin/activate
          ```
        </CodeBlockTab>

        <CodeBlockTab value="Windows">
          ```bash
          uv venv --python 3.12
          .venv\Scripts\activate
          ```
        </CodeBlockTab>
      </CodeBlockTabs>
    </Step>

  <Step title="Install dependencies">
    ```bash
    uv pip install -U agno beautifulsoup4 openai pypdf wikipedia
    ```
  </Step>

  <Step title="Export your OpenAI API key">
    <CodeBlockTabs defaultValue="Mac/Linux">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="Mac/Linux">
          Mac/Linux
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="Windows">
          Windows
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="Mac/Linux">
        ```bash
        export OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>

      <CodeBlockTab value="Windows">
        ```bash
        $Env:OPENAI_API_KEY="your_openai_api_key_here"
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Step>

  <Step title="Clone Agno">
    Clone the pinned Agno source and run the remaining commands from its root:

    ```bash
    git clone https://github.com/agno-agi/agno.git
    cd agno
    git checkout d703c34f3abf3c41275d3fb2da6e0518a8881f24
    ```
  </Step>

  <Step title="Start LightRAG">
    Follow the [LightRAG server setup](/examples/knowledge/advanced/graph-rag), configure its model and embedding providers, and start it at `http://localhost:9621` in a separate terminal. Set `LIGHTRAG_API_KEY` in the Python terminal only if the server requires authentication.
  </Step>

  <Step title="Run the example">
    Run the example from the repository root:

    ```bash
    python cookbook/07_knowledge/05_integrations/rag/agentic_rag_with_lightrag.py
    ```
  </Step>
</Steps>

Full source: [cookbook/07\_knowledge/05\_integrations/rag/agentic\_rag\_with\_lightrag.py](https://github.com/agno-agi/agno/blob/d703c34f3abf3c41275d3fb2da6e0518a8881f24/cookbook/07_knowledge/05_integrations/rag/agentic_rag_with_lightrag.py)
