> ## Documentation Index
> Fetch the complete documentation index at: https://traceroot.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> To set up traceroot in a repository, follow the setup prompt in https://traceroot.ai/docs/getting-started/coding-agents.md.

# Read-only SQL

> Query your project's spans and traces from the CLI or public API

traceroot's SQL gateway runs analytical queries against your project's trace data. It is read-only and project-scoped: the project is resolved during authentication, and SQL text and parameters cannot override it.

You can query the gateway with the [traceroot CLI](https://traceroot.ai/docs/cli/get-started.md) or with the public API.

## Query with the CLI

Pass a query as one quoted argument, or use `-f, --file <path>` to read it from a file:

```bash theme={null}
traceroot sql "SELECT count() AS spans FROM spans WHERE span_start_time >= now() - INTERVAL 1 DAY"
```

The default output is a table. Use `--csv` for CSV, `--json` for the complete API response, and `--output <file>` to write the selected format to a file.

```bash theme={null}
traceroot sql \
  "SELECT span_id, trace_id, name, duration_ms FROM spans WHERE span_start_time >= now() - INTERVAL 7 DAY" \
  --csv \
  --max-rows 5000 \
  --output spans.csv
```

Bind `{name:Type}` placeholders with one `--param name=value` flag per value. Parameters stay separate from the SQL text.

```bash theme={null}
traceroot sql \
  "SELECT count() FROM spans WHERE span_start_time >= {since:DateTime64(3)} AND environment = {environment:String}" \
  --param "since=2026-09-18 00:00:00" \
  --param environment=production
```

Combining `--json` with `--csv`, or repeating a `--param` name, exits 2. A table's row count, and a truncation warning for a table or CSV, go to stderr. With `--json`, read `truncated` from the response instead. A file written with `--output` has no terminal styling.

Run `traceroot sql schema` to list the available tables, columns, and ClickHouse types. `traceroot sql --help` lists every flag.

## Worked queries

Always include a time predicate on `span_start_time` or `trace_start_time` that compares the column directly with a constant: a timestamp, a parameter, or an expression such as `now() - INTERVAL 7 DAY`. The gateway uses that range when reading the underlying project-scoped views, so bounded queries scan less data.

### Recent error spans

```sql theme={null}
SELECT span_id, trace_id, name, status_message, span_start_time
FROM spans
WHERE status = 'ERROR'
  AND span_start_time >= now() - INTERVAL 1 DAY
ORDER BY span_start_time DESC
LIMIT 100
```

### p95 latency by model

```sql theme={null}
SELECT model_name, quantile(0.95)(duration_ms) AS p95_ms
FROM spans
WHERE model_name IS NOT NULL
  AND span_start_time >= now() - INTERVAL 7 DAY
GROUP BY model_name
ORDER BY p95_ms DESC
```

### Cost by model over seven days

```sql theme={null}
SELECT model_name, sum(cost) AS total_cost
FROM spans
WHERE model_name IS NOT NULL
  AND span_start_time >= now() - INTERVAL 7 DAY
GROUP BY model_name
ORDER BY total_cost DESC
```

## Query with the API

Send a project API key as a bearer token to `POST /api/v1/public/sql`:

```bash theme={null}
curl --request POST "$TRACEROOT_HOST_URL/api/v1/public/sql" \
  --header "Authorization: Bearer $TRACEROOT_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
    "query": "SELECT model_name, sum(cost) AS total_cost FROM spans WHERE span_start_time >= {since:DateTime64(3)} GROUP BY model_name ORDER BY total_cost DESC",
    "parameters": {"since": "2026-09-18 00:00:00"},
    "max_rows": 1000
  }'
```

A [session token or CLI access token](https://traceroot.ai/docs/api-reference/authentication.md#session-token-and-cli-access-token) works too, with the `project_id` query parameter.

A successful response contains column names and types, row values in the same order, the returned row count, truncation state, elapsed time, and any read statistics supplied by ClickHouse:

```json theme={null}
{
  "columns": [
    {"name": "model_name", "type": "Nullable(String)"},
    {"name": "total_cost", "type": "Nullable(Decimal(38, 9))"}
  ],
  "rows": [["gpt-5", "0.013500000"]],
  "row_count": 1,
  "truncated": false,
  "elapsed_ms": 12,
  "statistics": {"rows_read": 842, "bytes_read": 91736}
}
```

`statistics` is best-effort and may be empty. When more rows match than the gateway returns, `truncated` is `true`. A request's `max_rows` can lower the server cap, but cannot raise it.

Fetch the same schema used by the validator and query rewriter with `GET /api/v1/public/sql/schema`:

```bash theme={null}
curl "$TRACEROOT_HOST_URL/api/v1/public/sql/schema" \
  --header "Authorization: Bearer $TRACEROOT_API_KEY"
```

## Public schema

Queries can reference only two logical tables:

| Table | Contents | Time-filter column |
| :- | :- | :- |
| `spans` | Span identity, timing, status, model, token, cost, environment, metadata, and Git source fields | `span_start_time` |
| `traces` | Trace identity, timing, name, user, session, Git, environment, and metadata fields | `trace_start_time` |

Use `traceroot sql schema --json` or `GET /api/v1/public/sql/schema` for the authoritative column list and types.

These are curated views over traceroot's internal tables. They deliberately exclude:

* span and trace `input` and `output` payloads;
* the raw metadata JSON document; `metadata` is exposed instead as a one-level string map, accessed with expressions such as `metadata['user_id']`;
* `project_id`, `ch_create_time`, `ch_update_time`, and internal classification fields;
* rows that are not customer traffic; and
* every trace that belongs to an evaluation run, including its spans.

## SQL contract

The gateway accepts one `SELECT` or `UNION`. CTEs, subqueries, joins, grouping, ordering, and the `row_number`, `rank`, and `dense_rank` window functions are supported. Functions are allowlisted; the list includes common aggregation, date-bucketing, string, conversion, conditional, and metadata-map functions. A ClickHouse function outside the allowlist is rejected even if ClickHouse normally supports it.

The gateway rejects:

* multiple statements, writes, DDL, and control statements;
* tables other than `spans` and `traces`, including table functions;
* database-qualified names and the internal view names;
* `FINAL`, `SETTINGS`, and `FORMAT` clauses;
* any reference or alias named `project_id`;
* a CTE named `spans` or `traces`;
* a `WHERE` that filters on `span_start_time` or `trace_start_time` in any other form than a direct comparison with a constant (`>=`, `>`, `<`, `<=`, `=`, or `BETWEEN`), joined to the rest of the clause by `AND`; and
* parameter names `project_id` or beginning with `scope_`, which are reserved for server-side scoping.

Queries are limited to 65,536 characters. A request can bind at most 100 parameters, whose serialized JSON is limited to 16,384 characters.

## How far back a query reaches

Queries only see traces and spans inside your plan's retention window: 15 days on Free, 30 days on Starter, 90 days on Pro, and no limit on Enterprise. Older rows are left out of the result silently, without an error. Data is not deleted, so upgrading your plan makes older rows readable again.

## Execution and rate limits

Default execution limits are:

| Limit | Default |
| :- | -: |
| Execution time | 30 seconds |
| ClickHouse result rows | 100,000 |
| Result size | 512 MiB |
| Memory per query | 4 GiB |
| Concurrent queries per project, per REST process | 4 |
| Concurrent queries in total, per REST process | 16 |

The gateway reserves a row beyond the returned result to detect truncation. If that row exists, it removes the sentinel and returns `truncated: true`. Queries that exceed the time, result-size, or memory limits fail; narrow the selected columns or time range and retry.

On self-hosted deployments, operators can change the four ClickHouse caps with `CLICKHOUSE_SQL_MAX_EXECUTION_TIME`, `CLICKHOUSE_SQL_MAX_RESULT_ROWS`, `CLICKHOUSE_SQL_MAX_RESULT_BYTES`, and `CLICKHOUSE_SQL_MAX_MEMORY_USAGE`.

Concurrency is not a queue. If either per-process slot limit is full, the API returns HTTP 429 with `Too many queries are running. Retry shortly.` and a one-second `Retry-After` header.

traceroot Cloud also applies a per-project SQL request rate:

| Plan | Requests per minute |
| :- | -: |
| Free | 20 |
| Starter | 60 |
| Pro | 120 |
| Enterprise | 120 |

SQL rate limiting is disabled on self-hosted deployments where billing is disabled.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.