Skip to main content
Ready to build your first intelligent agent? This guide will walk you through creating a knowledge-powered agent that can answer questions about your documents in just a few minutes.

What You’ll Build

By the end of this tutorial, you’ll have an agent that can:
  • Read and understand your documents or website content
  • Answer specific questions based on that information
  • Provide sources for its responses
  • Search intelligently without you having to specify what to look for

Prerequisites

1

Install Agno

pip install agno
2

Set up your API key

export OPENAI_API_KEY="your-api-key-here"
This tutorial uses OpenAI, but Agno supports many other models.

Step 1: Set Up Your Knowledge Base

First, let’s create a knowledge base with a vector database to store your information:
knowledge_agent.py
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.pgvector import PgVector
from agno.models.openai import OpenAIChat

# Create a knowledge base with PgVector
knowledge = Knowledge(
    vector_db=PgVector(
        table_name="knowledge_documents",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai"
    ),
)

# Create an agent with knowledge
agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    knowledge=knowledge,
    # Enable automatic knowledge search
    search_knowledge=True,
    instructions=[
        "Always search your knowledge base before answering questions",
        "Include source references in your responses when possible"
    ]
)
For a quick start without setting up PostgreSQL, use LanceDB which stores data locally:
from agno.vectordb.lancedb import LanceDb

# Local vector storage - no database setup required
knowledge = Knowledge(
    vector_db=LanceDb(
        table_name="knowledge_documents",
        uri="tmp/lancedb"  # Local directory for storage
    ),
)

Step 2: Add Your Content

Now let’s add some knowledge to your agent. You can add content from various sources:
  • From Local Files
  • From URLs
  • From Text
# Add a specific file
knowledge.add_content(
    path="path/to/your/document.pdf"
)

# Add an entire directory
knowledge.add_content(
    path="path/to/documents/"
)

Step 3: Chat with Your Agent

That’s it! Your agent is now ready to answer questions based on your content:
# Test your knowledge-powered agent
if __name__ == "__main__":
    # Your agent will automatically search its knowledge to answer
    agent.print_response(
        "What is the company policy on remote work?",
        stream=True
    )

Complete Example

Here’s the full working example:
knowledge_agent.py
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.lancedb import LanceDb
from agno.models.openai import OpenAIChat

# Set up knowledge base
knowledge = Knowledge(
    vector_db=LanceDb(
        table_name="my_documents",
        uri="tmp/lancedb"
    ),
)

# Add your content
knowledge.add_content(
    text_content="""
    Agno Knowledge System

    Knowledge allows AI agents to access and search through
    domain-specific information at runtime. This enables
    dynamic few-shot learning and agentic RAG capabilities.

    Key features:
    - Automatic content chunking
    - Vector similarity search
    - Multiple data source support
    - Intelligent retrieval
    """
)

# Create your agent
agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    knowledge=knowledge,
    search_knowledge=True,
    instructions=["Always search knowledge before answering"]
)

# Chat with your agent
if __name__ == "__main__":
    agent.print_response("What is Agno Knowledge?", stream=True)
Run it:
python knowledge_agent.py

What Just Happened?

When you ran the code, here’s what occurred behind the scenes:
  1. Content Processing: Your text was chunked into smaller pieces and converted to vector embeddings
  2. Intelligent Search: The agent analyzed your question and searched for relevant information
  3. Contextual Response: The agent combined the retrieved knowledge with your question to provide an accurate answer
  4. Source Attribution: The response is based on your specific content, not generic training data

Next Steps: Explore Advanced Features

Troubleshooting

Make sure you set search_knowledge=True when creating your agent and consider adding explicit instructions to search the knowledge base.
For local development, try LanceDB instead of PostgreSQL. For production, ensure your database connection string is correct.
Your content might need better chunking. Try different chunking strategies or smaller chunk sizes for more precise retrieval.

Ready for Core Concepts?

Dive deeper into understanding knowledge bases and how they power intelligent agents
I