Agentic Filtering: Agent-Driven Search Refinement

With agentic filtering enabled, the agent inspects available metadata keys in the knowledge base and dynamically builds filters from the user query.

agentic_filtering.py
"""
Agentic Filtering: Agent-Driven Search Refinement
===================================================
With agentic filtering enabled, the agent inspects available metadata keys
in the knowledge base and dynamically builds filters from the user query.

This is powerful for multi-topic knowledge bases where the user's intent
determines which subset of data to search.

Steps:
1. Load documents with metadata tags
2. Enable agentic filtering on the agent
3. The agent automatically builds filters from user queries

See also: 04_filtering.py for static (predefined) filters.
"""

import asyncio

from agno.agent import Agent
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.knowledge.knowledge import Knowledge
from agno.models.openai import OpenAIResponses
from agno.vectordb.qdrant import Qdrant
from agno.vectordb.search import SearchType

# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------

qdrant_url = "http://localhost:6333"

knowledge = Knowledge(
    vector_db=Qdrant(
        collection="agentic_filtering_demo",
        url=qdrant_url,
        search_type=SearchType.hybrid,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
)

# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------

# Enable agentic filtering: the agent inspects metadata keys and dynamically
# builds filters based on the user's query.
agent = Agent(
    model=OpenAIResponses(id="gpt-5.2"),
    knowledge=knowledge,
    search_knowledge=True,
    enable_agentic_knowledge_filters=True,
    markdown=True,
)

# ---------------------------------------------------------------------------
# Run Demo
# ---------------------------------------------------------------------------

if __name__ == "__main__":

    async def main():
        # Load documents with rich metadata
        await knowledge.ainsert(
            name="Thai Recipes",
            url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
            metadata={"cuisine": "thai", "category": "recipes"},
        )
        await knowledge.ainsert(
            name="CV",
            path="cookbook/07_knowledge/testing_resources/cv_1.pdf",
            metadata={"category": "resume", "department": "engineering"},
        )

        print("\n" + "=" * 60)
        print("Agentic filtering: agent builds filters from query")
        print("=" * 60 + "\n")

        # The agent will automatically filter by cuisine=thai
        agent.print_response("What Thai recipes do you have?", stream=True)

        print("\n" + "=" * 60)
        print("Different query triggers different filters")
        print("=" * 60 + "\n")

        # The agent will automatically filter by category=resume
        agent.print_response("What engineering candidates do you have?", stream=True)

    asyncio.run(main())

The model chooses whether to search and which metadata filters to request; the comments describe intended choices, not guaranteed tool calls. Use trusted application-supplied identity and filters for access control, rather than relying on the model to choose them.

Run the Example

Set up your virtual environment

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

Install dependencies

uv pip install -U agno beautifulsoup4 fastembed openai pypdf qdrant-client

Export your OpenAI API key

export OPENAI_API_KEY="your_openai_api_key_here"

Run Qdrant

docker run -d --name qdrant -p 6333:6333 qdrant/qdrant:latest

Download the sample CV

From the directory where you will run Python, download the file used by the example:

mkdir -p cookbook/07_knowledge/testing_resources
curl -fL https://raw.githubusercontent.com/agno-agi/agno/d703c34f3abf3c41275d3fb2da6e0518a8881f24/cookbook/07_knowledge/testing_resources/cv_1.pdf -o cookbook/07_knowledge/testing_resources/cv_1.pdf

Run the example

Save the code above as agentic_filtering.py, then run:

python agentic_filtering.py

Full source: cookbook/07_knowledge/02_building_blocks/05_agentic_filtering.py