- Filter and search for relevant logs and experiments. Use
WHEREclauses to filter individual records andHAVINGclauses to filter aggregated results after grouping. - Create consistent, reusable queries for monitoring.
- Build automated reporting and analysis pipelines.
- Write complex queries to analyze model performance.
Self-hosted deployments: SQL syntax support requires data plane version v1.1.29 or later.
Syntax styles
Braintrust supports two syntax styles: standard SQL syntax, and the legacy BTQL syntax with pipe-delimited clauses. SQL syntax is recommended for all new queries. The parser automatically detects whether your query is SQL or BTQL:- SQL queries start with
SELECT,WITH, etc. followed by whitespace - BTQL queries use clause syntax like
select:,filter:, etc.
SQL syntax specifies the shape with a named parameter (e.g.,
FROM experiment('id', shape => 'traces')), while BTQL uses a trailing token (e.g., from: experiment('id') traces). Table aliases on top-level table functions (e.g., FROM project_logs('id') AS t) are reserved for future use. Aliases on subquery sources (e.g., FROM (...) AS sub) are required.Full-text search: Use the
MATCH infix operator for full-text search:WHERE input MATCH 'search term'→filter: input MATCH 'search term'- Multiple columns require OR:
WHERE input MATCH 'x' OR output MATCH 'x'→filter: input MATCH 'x' OR output MATCH 'x'
Run SQL queries
Run SQL from the SQL sandbox, thebt sql CLI, or the API.
SQL sandbox
To test SQL with autocomplete, validation, and a table of results, use the SQL sandbox in your project. In the sandbox, you can use Loop to generate and optimize queries from natural language: Example queries:- “Find the most common errors in logs over the last week”
- “What are the highest scoring rows in my experiment”
- “Show me error distribution over time”
- “List all traces where latency exceeded 60 seconds”
- “Update the query to show error distribution over time”
- “Add a filter to only show errors from specific models”
- “Group by user instead”
- Syntax errors
- Schema validation issues
- Field name corrections
If a
project_logs() query is missing a range filter on created, _xact_id, _pagination_key, or a specific root_span_id/id, the sandbox proactively warns you so you don’t have to wait for a timeout to discover the issue.bt CLI
Run SQL from your terminal withbt sql. It opens an interactive editor, accepts an inline query, or reads from stdin for scripting.
API
Access SQL programmatically with the Braintrust API:query(required): your SQL query string.fmt: response format (jsonorparquet, defaults tojson).lint_mode: root-level lint handling mode (defaultorstrict, defaults todefault). Instrictmode, lint warnings fail the query.tz_offset: timezone offset in minutes for time-based operations.audit_log: include audit log data.version: an_xact_idstring to query data as it existed at a specific point in time (useful for recovering deleted rows). Supported forexperimentanddatasetsources; not supported forproject_logs.
For correct day boundaries, set
tz_offset to match your timezone. For example, use 480 for US Pacific Standard Time.Next steps
- Learn the query structure: clauses, data sources, data shapes, and field access.
- Look up syntax in Functions and operators.
- Browse copy-and-run patterns in Example queries.
- Write correct, fast queries with SQL best practices.