Skip to main content
This example shows how to trace an agent with a knowledge base in AgentOS. The traces capture knowledge searches, model calls, and all agent operations.
1

Create a Python file

touch agent_with_knowledge_tracing.py
2

Add the following code to your Python file

agent_with_knowledge_tracing.py
from textwrap import dedent

from agno.agent import Agent
from agno.db.postgres import PostgresDb
from agno.db.sqlite import SqliteDb
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.openai import OpenAIChat
from agno.os import AgentOS
from agno.vectordb.pgvector import PgVector, SearchType

# ************* Database Setup *************
db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"
db = PostgresDb(db_url, id="agno_assist_db")
db_sqlite = SqliteDb(db_file="tmp/traces.db")
# *******************************


# ************* Description & Instructions *************
description = dedent(
    """\
    You are AgnoAssist, an advanced AI Agent specialized in the Agno framework.
    Your goal is to help developers understand and effectively use Agno and the AgentOS by providing
    explanations, working code examples, and optional audio explanations for complex concepts."""
)

instructions = dedent(
    """\
    Your mission is to provide comprehensive support for Agno developers. Follow these steps to ensure the best possible response:

    1. **Analyze the request**
        - Analyze the request to determine if it requires a knowledge search, creating an Agent, or both.
        - If you need to search the knowledge base, identify 1-3 key search terms related to Agno concepts.
        - If you need to create an Agent, search the knowledge base for relevant concepts and use the example code as a guide.
        - When the user asks for an Agent, they mean an Agno Agent.
        - All concepts are related to Agno, so you can search the knowledge base for relevant information

    After Analysis, always start the iterative search process. No need to wait for approval from the user.

    2. **Iterative Search Process**:
        - Use the `search_knowledge_base` tool to search for related concepts, code examples and implementation details
        - Continue searching until you have found all the information you need or you have exhausted all the search terms

    After the iterative search process, determine if you need to create an Agent.
    If you do, ask the user if they want you to create an Agent for them.

    3. **Code Creation**
        - Create complete, working code examples that users can run. For example:
        ```python
        from agno.agent import Agent
        from agno.tools.duckduckgo import DuckDuckGoTools

        agent = Agent(tools=[DuckDuckGoTools()])

        # Perform a web search and capture the response
        response = agent.run("What's happening in France?")
  • You must remember to use agent.run() and NOT agent.print_response()
  • Remember to:
    • Build the complete agent implementation
    • Include all necessary imports and setup
    • Add comprehensive comments explaining the implementation
    • Ensure all dependencies are listed
    • Include error handling and best practices
    • Add type hints and documentation
  1. Explain important concepts using audio
    • When explaining complex concepts or important features, ask the user if they’d like to hear an audio explanation
    • Use the ElevenLabs text_to_speech tool to create clear, professional audio content
    • The voice is pre-selected, so you don’t need to specify the voice.
    • Keep audio explanations concise (60-90 seconds)
    • Make your explanation really engaging with:
      • Brief concept overview and avoid jargon
      • Talk about the concept in a way that is easy to understand
      • Use practical examples and real-world scenarios
      • Include common pitfalls to avoid
  2. Explain concepts with images
    • You have access to the extremely powerful DALL-E 3 model.
    • Use the create_image tool to create extremely vivid images of your explanation.
    • Don’t provide the URL of the image in the response. Only describe what image was generated.
Key topics to cover:
  • Agent levels and capabilities
  • Knowledge base and memory management
  • Tool integration
  • Model support and configuration
  • Best practices and common patterns""" )

*******************************

knowledge = Knowledge( vector_db=PgVector( db_url=db_url, table_name=“agno_assist_knowledge”, search_type=SearchType.hybrid, embedder=OpenAIEmbedder(id=“text-embedding-3-small”), ), contents_db=db, )

Setup our Agno Agent

agno_assist = Agent( name=“Agno Assist”, id=“agno-assist”, model=OpenAIChat(id=“gpt-5-mini”), description=description, instructions=instructions, db=db_sqlite, enable_user_memories=True, knowledge=knowledge, search_knowledge=True, add_history_to_context=True, add_datetime_to_context=True, markdown=True, )agent_os = AgentOS( description=“Example app with Agno Docs Agent with knowledge and tracing”, agents=[agno_assist], tracing=True, )app = agent_os.get_app()if name == “main”: knowledge.add_content(name=“Agno Docs”, url=“https://docs.agno.com/llms-full.txt”) """Run your AgentOS.You can see test your AgentOS at: http://localhost:7777/docs"""

Don’t use reload=True here, this can cause issues with the lifespan

agent_os.serve(app=“agent_with_knowledge_tracing:app”)
</Step>

<Snippet file="create-venv-step.mdx" />

<Step title="Install libraries">
```bash
pip install -U openai agno pgvector psycopg opentelemetry-api opentelemetry-sdk openinference-instrumentation-agno
3

Export your API keys

export OPENAI_API_KEY="your_openai_api_key_here"
4

Start PostgreSQL with pgvector

Make sure you have PostgreSQL running with pgvector extension. You can use Docker:
docker run -d \
  --name pgvector \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e POSTGRES_DB=ai \
  -p 5532:5432 \
  pgvector/pgvector:pg16
5

Run AgentOS

python agent_with_knowledge_tracing.py
Your AgentOS will be available at http://localhost:7777. View traces in the AgentOS dashboard.