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 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), range queries (GT/LT/GTE/LTE), inequality (NEQ), or substring/prefix matching (CONTAINS/STARTSWITH)

Filter Operators

Filter expressions support comparison, string-matching, and logical operators.

Comparison Operators

  • EQ(key, value) - Equality: field equals value
  • NEQ(key, value) - Inequality: field does not equal value
  • GT(key, value) - Greater than: field > value
  • GTE(key, value) - Greater than or equal: field >= value
  • LT(key, value) - Less than: field < value
  • LTE(key, value) - Less than or equal: field <= value
  • IN(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 true
  • OR(*filters) - At least one condition must be true
  • NOT(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 for AND, 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)

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