Skip to main content
When using the AgentOS API, you can apply filters to precisely control which knowledge base documents your agents search, without changing your agent code. Filter expressions serialize to JSON and are automatically reconstructed server-side for powerful, programmatic filtering.

Two Approaches to Filtering

Agno supports two ways to filter knowledge through the API:
  • Use dictionary filters for simple “field = value” lookups
  • Use filter expressions when you need OR/NOT logic or ranges

1. Dictionary Filters (Simple)

Best for straightforward equality matching. Send a JSON object with key-value pairs:

2. Filter Expressions (Advanced)

Best for complex filtering with full logical control. Send structured filter objects:
When to use which:
  • Use dict filters for simple queries like filtering by category or status
  • Use filter expressions when you need OR logic, exclusions (NOT), or range queries (GT/LT)

Filter Operators

Filter expressions support a range of comparison and logical operators:

Comparison Operators

  • EQ(key, value) - Equality: field equals value
  • GT(key, value) - Greater than: field > value
  • LT(key, value) - Less than: field < value
  • IN(key, [values]) - Inclusion: field in list of values

Logical Operators

  • AND(*filters) - All conditions must be true
  • OR(*filters) - At least one condition must be true
  • NOT(filter) - Negate a condition

Serialization Format

Filter expression objects use a dictionary format with an "op" key that distinguishes them from regular dict filters:
The presence of the "op" key tells the API to deserialize the filter as a filter expression. Regular dict filters (without "op") continue to work for backward compatibility.

Using Filters Through the API

In all examples below, you pass the serialized JSON string via the knowledge_filters field when creating a run.

Dictionary Filters (Simple Approach)

For basic filtering, send a JSON object with key-value pairs. All conditions are combined with AND logic:
More Dict Filter Examples:

Filter Expressions (Advanced Approach)

For complex filtering with logical operators and comparisons:

Multiple Filter Expressions

Send multiple filter expressions as a JSON array:

Error Handling

Invalid Filter Structure

When filters have errors, they’re gracefully ignored with warnings:
When filters fail to parse, the search proceeds without filters rather than throwing an error. Always verify your filter JSON is valid and check server logs if results seem unfiltered.

Client-Side Validation

Add validation before sending requests:

Next Steps

Advanced Filtering Guide

Learn about filter expressions and metadata design in detail

Agent OS API

Explore the full Agent OS API reference

Knowledge Bases

Understand knowledge base architecture and setup

Search & Retrieval

Optimize your search strategies

Filter Expressions Example

See working examples of filter expressions via API