agentic_rag_with_reasoning.py
"""
Agentic Rag With Reasoning
=============================
Demonstrates agentic RAG with reranking and explicit reasoning tools.
"""
import asyncio
from agno.agent import Agent
from agno.knowledge.embedder.cohere import CohereEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.knowledge.reranker.cohere import CohereReranker
from agno.models.openai import OpenAIResponses
from agno.tools.reasoning import ReasoningTools
from agno.vectordb.lancedb import LanceDb, SearchType
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
knowledge = Knowledge(
# Use LanceDB as the vector database, store embeddings in the `agno_docs` table
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
embedder=CohereEmbedder(id="embed-v4.0"),
reranker=CohereReranker(model="rerank-v3.5"),
),
)
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
agent = Agent(
model=OpenAIResponses(id="gpt-5.2"),
# Agentic RAG is enabled by default when `knowledge` is provided to the Agent.
knowledge=knowledge,
# search_knowledge=True gives the Agent the ability to search on demand
# search_knowledge is True by default
search_knowledge=True,
tools=[ReasoningTools(add_instructions=True)],
instructions=[
"Include sources in your response.",
"Always search your knowledge before answering the question.",
],
markdown=True,
)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
if __name__ == "__main__":
asyncio.run(
knowledge.ainsert_many(urls=["https://docs.agno.com/agents/overview.md"])
)
agent.print_response(
"What are Agents?",
stream=True,
show_full_reasoning=True,
)
Run the Example
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U agno cohere lancedb openai pyarrow
3
Export your API keys
export LANCEDB_API_KEY="your_lancedb_api_key_here"
export OPENAI_API_KEY="your_openai_api_key_here"
$Env:LANCEDB_API_KEY="your_lancedb_api_key_here"
$Env:OPENAI_API_KEY="your_openai_api_key_here"
4
Run the example
Save the code above as
agentic_rag_with_reasoning.py, then run:python agentic_rag_with_reasoning.py