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

# Find the slowest LLM spans with SQL

> Rank LLM spans by duration in one trace or across a project with a SQL query, and spot slow spans in the trace timeline.

> [!NOTE]
>
> **Applies to:**
>
> * Plan - Any
> * Deployment - Any
> *
> * Use case - Identify which LLM calls in a trace or project take the longest

## Summary

To rank LLM spans by how long they took, compute each span's duration as `metrics.end - metrics.start` in a SQL query and sort by it. Run the query on the [** Query**](https://www.braintrust.dev/app/~/sql) page. To inspect a single trace visually, open it and use the **Timeline** view, which scales span bars by duration.

## What is happening

In the default **Traces** row type of the logs table, each row is a trace represented by its root span. Fields that exist only on child spans, such as the details of an individual LLM call, appear empty in that view.

Each span records its start and end times in `metrics.start` and `metrics.end` as Unix timestamps in seconds. LLM spans have `span_attributes.type` set to `llm`. Subtracting the two timestamps gives the span's duration in seconds, which you can sort on in SQL.

## Fix or suggestion

### Option 1: Rank the LLM spans in one trace

Replace the project ID and the trace's root span ID, then run the query on the [** Query**](https://www.braintrust.dev/app/~/sql) page or with [`bt sql`](https://docs.braintrust.dev/reference/cli/sql):

```sql theme={null}
SELECT
  id,
  span_attributes.name AS span_name,
  metadata.model AS model,
  metrics.end - metrics.start AS duration_seconds
FROM project_logs('<PROJECT_ID>')
WHERE root_span_id = '<ROOT_SPAN_ID>'
  AND span_attributes.type = 'llm'
  AND metrics.end IS NOT NULL
ORDER BY duration_seconds DESC
LIMIT 20
```

The `root_span_id` filter scopes the query to a single trace and maps directly to the index, so the query stays fast.

### Option 2: Rank LLM spans across a project

To find the slowest LLM calls across all traces, replace the `root_span_id` filter with a time range and return `root_span_id` so you can open each trace:

```sql theme={null}
SELECT
  root_span_id,
  id,
  span_attributes.name AS span_name,
  metadata.model AS model,
  metrics.end - metrics.start AS duration_seconds
FROM project_logs('<PROJECT_ID>')
WHERE created > now() - interval 1 day
  AND span_attributes.type = 'llm'
  AND metrics.end IS NOT NULL
ORDER BY duration_seconds DESC
LIMIT 20
```

Sorting on a computed value requires the engine to read every matching span before it applies `LIMIT`. Keep the time range as short as your investigation allows. See [Sort on indexed columns](https://docs.braintrust.dev/reference/sql/best-practices.md#sort-on-indexed-columns).

### Option 3: Inspect one trace in the timeline

1. Go to [** Logs**](https://www.braintrust.dev/app/~/logs) and open the trace.
2. Select  **Timeline** in the outline. Bar width represents each span's duration by default.
3. To show only LLM spans, select  and then ** Filter by span type**.

See [View as a timeline](https://docs.braintrust.dev/observe/examine-traces.md#view-as-a-timeline) for the other scaling options.

To list every span in the logs table instead of one row per trace, open the row type selector in the toolbar and choose  **Spans**. See [Browse logs](https://docs.braintrust.dev/observe/view-logs.md#browse-logs).

## How to confirm it worked

* The query results list LLM spans with the largest `duration_seconds` values first.
* In the trace timeline, the spans at the top of the query results have the widest bars.

## Notes

* To track LLM latency over time instead of finding individual slow spans, chart `avg(metrics.end - metrics.start)` or `percentile(metrics.end - metrics.start, 0.95)` grouped by model. See [Example queries](https://docs.braintrust.dev/observe/dashboards/sql-charts.md#example-queries).
* The `metrics.end IS NOT NULL` filter excludes spans that have no recorded end time.


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