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 asmetrics.end - metrics.start in a SQL query and sort by it. Run the query on the Query 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 inmetrics.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 page or withbt sql:
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 theroot_span_id filter with a time range and return root_span_id so you can open each trace:
LIMIT. Keep the time range as short as your investigation allows. See Sort on indexed columns.
Option 3: Inspect one trace in the timeline
- Go to Logs and open the trace.
- Select Timeline in the outline. Bar width represents each span’s duration by default.
- To show only LLM spans, select and then Filter by span type.
How to confirm it worked
- The query results list LLM spans with the largest
duration_secondsvalues 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)orpercentile(metrics.end - metrics.start, 0.95)grouped by model. See Example queries. - The
metrics.end IS NOT NULLfilter excludes spans that have no recorded end time.