Focus
The Focus DSL pre-filters tool results before they reach the LLM, reducing token consumption without changing agent behaviour. SQL database plugins push focus clauses directly into the query so the database does the filtering.
#Overview
Every token a tool result sends to the LLM costs money and consumes context window. Wide database tables, verbose API responses, and large JSON payloads force the LLM to process far more data than it actually needs to answer the question.
Focus lets you describe which rows and columns matter. The framework filters the data before the LLM ever sees it.
from daita import Agent
from daita.plugins import postgresql
db = postgresql(host="localhost", database="analytics", username="user", password="pw")
agent = Agent(
name="Analyst",
tools=[db],
focus="status == 'completed' | SELECT order_id, product, amount | LIMIT 100"
)
# The LLM receives only completed orders, only 3 columns, capped at 100 rows.
# For SQL plugins, this filter runs inside the database — no full table scan.
await agent.run("What was total revenue from completed orders this month?")#DSL Syntax
Focus expressions are pipe-delimited strings. All clauses are optional and can appear in any order.
[filter] | [SELECT ...] | [ORDER BY ...] | [LIMIT n] | [GROUP BY ...]#Filter
A Python boolean expression evaluated against each row.
"status == 'active'"
"amount > 100 and region == 'EU'"
"level == 'ERROR' or level == 'WARN'"
"refunded == False"
"status in ['pending', 'processing']"
"notes == None" # translates to IS NULL in SQLSupported operators: ==, !=, <, <=, >, >=, in, not in, and, or, not.
#SELECT
Project to a subset of columns.
"SELECT id, name, amount"
"SELECT order_id, status, region"#ORDER BY
"ORDER BY amount DESC"
"ORDER BY created_at" # ASC is the default#LIMIT
"LIMIT 50"#GROUP BY and Aggregates
"GROUP BY region | SELECT region, SUM(revenue) AS total"
"GROUP BY status | SELECT status, COUNT(*) AS cnt | ORDER BY cnt DESC"Supported aggregate functions: SUM, COUNT, AVG, MIN, MAX.
#Combined example
focus = "status == 'completed' | SELECT order_id, product, amount | ORDER BY amount DESC | LIMIT 100"#Applying Focus
#Agent-wide — all tools
agent = Agent(
name="Analyst",
tools=[db],
focus="SELECT id, name, status | LIMIT 200",
)#Per-tool — by tool name
agent = Agent(
name="Analyst",
tools=[db, slack_plugin],
focus={
"postgres_query": "status == 'active' | SELECT id, amount | LIMIT 100",
"slack_post": "SELECT channel, text",
},
)#On the tool definition
from daita import tool
@tool(focus="level == 'ERROR' | SELECT timestamp, service, message | LIMIT 50")
def get_logs() -> list:
...#Precedence
When multiple focus sources apply to the same tool:
Agent dict focus > Agent string focus > @tool default > no focus#Applying manually
from daita import apply_focus
rows = await db.query("SELECT * FROM orders")
focused = apply_focus(rows, "status == 'completed' | SELECT id, amount | LIMIT 100")#SQL Pushdown
For SQL database plugins (PostgreSQL, MySQL, Snowflake), focus clauses are compiled into the SQL query before it executes. The database filters the data — nothing unnecessary is transferred over the wire.
# Without focus — fetches every row, every column
rows = await db.query("SELECT * FROM orders")
# With focus — database executes the narrowed query
rows = await db.query(
"SELECT * FROM orders",
focus="status == 'completed' | SELECT id, product, amount | LIMIT 100"
)
# Runs: SELECT "id", "product", "amount"
# FROM (SELECT * FROM orders) _focus_q
# WHERE "status" = 'completed'
# LIMIT 100#Using focus through an agent
Pass a focus string in Agent(focus=...) and the framework injects it automatically:
agent = Agent(
name="Analyst",
tools=[postgresql(...)],
focus={"postgres_query": "status == 'completed' | SELECT id, product, amount | LIMIT 100"},
)Or instruct the LLM to pass it explicitly as a tool argument — the postgres_query, mysql_query, and snowflake_query tools all expose a focus parameter in their schemas.
#What gets pushed down
| Clause | Pushed to SQL | Notes |
|---|---|---|
Filter (==, !=, <, >, and, or, not) | Yes | |
Filter with == None | Yes | Emits IS NULL |
Filter with dot notation (order.status) | No | Falls back to Python |
SELECT | Yes | Column names must be simple identifiers |
ORDER BY | Yes | |
LIMIT | Yes | |
GROUP BY + aggregates | Yes | Standard SUM/COUNT/AVG/MIN/MAX |
Anything that cannot be expressed in SQL is silently handled by the Python evaluator after the query returns.
#Token reduction
Integration tests on typical wide tables show ~90% reduction in tokens sent to the LLM when focus pushdown is active.
| Scenario | Without focus | With focus | Reduction |
|---|---|---|---|
| Orders table (15 columns) | ~572 tokens | ~49 tokens | −91% |
| Sales table (10 columns) | ~413 tokens | ~39 tokens | −91% |
#Supported Backends
| Data type | Backend | How focus is applied |
|---|---|---|
list[dict] / dict | Python evaluator | AST-walk filter, Python projection/sort/limit |
pandas.DataFrame | Pandas native | df.query(), column selection, sort_values, head |
| PostgreSQL / MySQL / Snowflake | SQL compiler | Focus compiled into subquery SQL before execution |
The correct backend is selected automatically based on the data type.
#Error Handling
Malformed DSL raises FocusDSLError at parse time:
from daita import apply_focus
from daita.core.exceptions import FocusDSLError
try:
apply_focus(data, "price >>>")
except FocusDSLError as e:
print(e) # Invalid filter expression 'price >>>': invalid syntaxFocus failures on a live tool call log a warning and return the unfiltered result rather than failing the agent run.
#Next Steps
- Agent — Full agent configuration reference
- Tools — Defining and registering tools
- PostgreSQL Plugin — SQL pushdown in action
- MySQL Plugin — MySQL focus pushdown
- Snowflake Plugin — Snowflake focus pushdown