Agent with Knowledge Tracing

Trace agents with knowledge bases in AgentOS.

Trace an agent with a knowledge base in AgentOS. The traces capture knowledge searches, model calls, and all agent operations.

Create a 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 OpenAIResponses
from agno.os import AgentOS
from agno.vectordb.pgvector import PgVector, SearchType

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 = 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 and working code examples."""
)

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
        - 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

    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=OpenAIResponses(id="gpt-5.2"),
    description=description,
    instructions=instructions,
    db=db_sqlite,
    update_memory_on_run=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.insert(name="Agno Docs", url="https://docs.agno.com/llms-full.txt")
    """Run your AgentOS.

    You can 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")

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U openai "agno[os]" beautifulsoup4 pgvector "psycopg[binary]" opentelemetry-api opentelemetry-sdk openinference-instrumentation-agno

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Start PostgreSQL with pgvector

Make sure you have PostgreSQL running with the 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

Run AgentOS

python agent_with_knowledge_tracing.py

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