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:Filter Operators
Filter expressions support comparison, string-matching, and logical operators.Comparison Operators
EQ(key, value)- Equality: field equals valueNEQ(key, value)- Inequality: field does not equal valueGT(key, value)- Greater than:field > valueGTE(key, value)- Greater than or equal:field >= valueLT(key, value)- Less than:field < valueLTE(key, value)- Less than or equal:field <= valueIN(key, [values])- Inclusion: field in list of values
String Matching Operators
CONTAINS(key, value)- field contains the substring (case-insensitive)STARTSWITH(key, value)- field starts with the given prefix
Logical Operators
AND(*filters)- All conditions must be trueOR(*filters)- At least one condition must be trueNOT(filter)- Negate a condition
Nesting limit: Filter expressions can be nested up to 10 levels deep. Deeper expressions are rejected during deserialization, fall into the error-handling path, and the request proceeds without filters (with a warning logged).
Python operator overloads
Filter expressions support Python’s bitwise operators as shorthand forAND, OR, and NOT:
Serialization Format
Filter expression objects use a dictionary format with an"op" key that distinguishes them from regular dict filters. Field names differ per operator: IN takes values (plural); NOT takes condition (singular); AND/OR take conditions (plural); everything else takes value.
Comparison and string operators
All comparison operators (EQ, NEQ, GT, GTE, LT, LTE) and string operators (CONTAINS, STARTSWITH) share the same shape:
IN takes values (plural)
"value" instead of "values" is rejected by the deserializer.
AND and OR take conditions (plural)
NOT takes condition (singular)
Round-trip example
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 theknowledge_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: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: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