Skip to main content
Query all your Laminar data directly with SQL. Find patterns, debug issues, and answer questions the dashboard doesn’t anticipate.

What You Can Query

TableContains
spansIndividual spans (LLM/tool/custom/eval spans) with events and tags
tracesTrace-level aggregates derived from spans
signal_eventsSignal-triggered events and payloads
signal_runsSignal execution runs and statuses
logsStreaming logs (for example, sandbox command output)
dataset_datapointsDataset datapoints (latest version per datapoint)
dataset_datapoint_versionsDataset datapoints (all versions/history)
evaluation_datapointsEvaluation datapoints with scores, executor output, dataset links, and associated trace data
Only SELECT queries are allowed.

Getting Started

Open the SQL Editor from the sidebar. Write a query:
SELECT name, input, output, start_time
FROM spans
WHERE start_time > now() - INTERVAL 3 DAY
Results appear in a table or raw JSON view. Export results to a dataset or labeling queue for further use. You can also query via API at /v1/sql/query—authenticate with your project API key and pass { "query": "..." }.

Writing Queries

Laminar uses ClickHouse, a columnar analytics database. The basics work like standard SQL (SELECT, FROM, WHERE, GROUP BY, ORDER BY, LIMIT), with a few differences.

Always filter by time

Spans are ordered by start_time. Adding a time filter dramatically speeds up queries and prevents memory issues:
-- Slow: scans everything
SELECT * FROM spans WHERE trace_id = 'abc-123'

-- Fast: scans only relevant time range
SELECT * FROM spans 
WHERE trace_id = 'abc-123'
  AND start_time > now() - INTERVAL 1 DAY

Avoid joins

ClickHouse isn’t optimized for joins. Instead, run two queries and combine results in your application:
-- First: find the spans you care about
SELECT trace_id, name, input, output
FROM spans
WHERE span_type = 'LLM' AND start_time > now() - INTERVAL 1 DAY

-- Second: get trace-level data for those trace_ids
SELECT id, duration
FROM traces
WHERE id IN ('id1', 'id2', 'id3')

Working with dates

Truncate timestamps for grouping with toStartOfInterval:
-- Spans per day over the last month
SELECT
    toStartOfInterval(start_time, INTERVAL 1 DAY) AS day,
    count(*) AS span_count
FROM spans
WHERE start_time > now() - INTERVAL 1 MONTH
GROUP BY day
ORDER BY day
Works with any interval: INTERVAL 15 MINUTE, INTERVAL 1 HOUR, etc. Shortcuts exist for common intervals: toStartOfDay(value), toStartOfHour(value), toStartOfWeek(value).

Working with JSON

Many columns (like attributes) store JSON as strings. Use simpleJSONExtract* functions for fast extraction:
-- Extract token counts from LLM spans
SELECT
    name,
    simpleJSONExtractInt(attributes, 'gen_ai.usage.input_tokens') AS input_tokens,
    simpleJSONExtractInt(attributes, 'gen_ai.usage.output_tokens') AS output_tokens
FROM spans
WHERE span_type = 'LLM' AND start_time > now() - INTERVAL 1 DAY
Check if a key exists with simpleJSONHas:
SELECT count(*)
FROM spans
WHERE simpleJSONHas(attributes, 'gen_ai.request.structured_output_schema')
For complex operations (array indexing, nested paths), use JSONExtract* functions—more flexible but slower.

Working with tuples

Some columns store data as arrays of named tuples. The most common examples are:
  • spans.events: Array(Tuple(timestamp Int64, name String, attributes String))
  • evaluation_datapoints.trace_spans: Array(Tuple(name String, duration Float64, type String))
Use tupleElement(tuple, 'fieldName') to extract fields from tuples:
-- Extract event names from spans
SELECT 
    span_id,
    arrayMap(e -> tupleElement(e, 'name'), events) as event_names
FROM spans
WHERE length(events) > 0
  AND start_time > now() - INTERVAL 1 DAY
LIMIT 10
Use arrayExists to filter arrays by tuple field values:
-- Find spans with cache_hit events
SELECT span_id, name, events
FROM spans
WHERE arrayExists(e -> tupleElement(e, 'name') = 'cache_hit', events)
  AND start_time > now() - INTERVAL 1 DAY
Use ARRAY JOIN to unnest tuple arrays into separate rows:
-- Get one row per span in each evaluation datapoint
SELECT 
    edp.id as datapoint_id,
    tupleElement(span, 'name') as span_name,
    tupleElement(span, 'duration') as span_duration,
    tupleElement(span, 'type') as span_type
FROM evaluation_datapoints edp
ARRAY JOIN edp.trace_spans as span
WHERE edp.evaluation_id = 'your-evaluation-id'
LIMIT 100
Combine arrayMap and tupleElement for transformations:
-- Convert event timestamps from nanoseconds to DateTime
SELECT 
    span_id,
    arrayMap(e -> toDateTime64(tupleElement(e, 'timestamp') / 1e9, 9, 'UTC'), events) as event_times
FROM spans
WHERE length(events) > 0
  AND start_time > now() - INTERVAL 1 DAY
LIMIT 10

Data types

TypeUsed for
UUIDMost ID columns (trace_id, span_id, id)
DateTime64(9, 'UTC')Timestamps (always UTC)
StringText, JSON stored as strings, and enum-like columns (span_type, trace_type, status)
LowCardinality(String)Low-cardinality enums
Float64Floating point numbers
Int64Counts, token numbers
UInt8Small numeric enums (for example, logs.severity_number)
UInt32Flags and bitmasks (for example, logs.flags)
UInt64Indexes and counters (evaluation_datapoints.index)
BoolFlags like has_browser_session
Array(String)Tag lists and other string arrays
Array(Tuple(...))Complex nested arrays (for example, events)
Cast with CAST(value AS Type) or toDateTime64('2025-01-01 00:00:00', 9, 'UTC').

Table Schemas

These are the logical tables exposed in the SQL Editor. The schemas below reflect the columns available for queries.

spans

ColumnTypeExample value
span_idUUID"00000000-0000-0000-1234-426614174000"
nameString"openai.chat"
span_typeString"LLM"
start_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
end_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
durationFloat641.23
input_costFloat640.5667
output_costFloat640.123
total_costFloat640.6897
input_tokensInt64150
output_tokensInt64100
total_tokensInt64250
request_modelString"gpt-4.1-mini"
response_modelString"gpt-4.1-mini-2025-04-14"
modelString"gpt-4.1-mini-2025-04-14"
trace_idUUID"12345678-90ab-4def-8234-426614174000"
providerString"openai"
pathString"workflow.process.step1.openai.chat"
inputString"[{\"role\": \"user\", \"content\": \"Hello\"}]"
outputString"[{\"role\": \"assistant\", \"content\": \"Hi\"}]"
statusString"success"
parent_span_idUUID"00000000-0000-0000-a456-abcd5667ef09"
attributesString"{\"gen_ai.system\": \"openai\", \"gen_ai.model\": \"gpt-4o\"}"
tagsArray(String)["needs-review", "tool-call"]
eventsArray(Tuple(timestamp Int64, name String, attributes String))[Tuple(1735689600000000000, 'cache_hit', '{}'), ...]

Path

Laminar span path is stored as an array of span names in span attributes. However, in SQL queries, it is stored as a string with items joined by a dot. For example, if the span path is ["outer", "inner"], the path column will be "outer.inner". If needed, you can still access the array value by reading attributes with simpleJSONExtractRaw(attributes, 'lmnr.span.path').

Parent span ID

If the current span is the top span of the trace, the parent_span_id will be a 0 UUID, i.e. "00000000-0000-0000-0000-000000000000".

Span type

Here are the values of the span_type column and their meanings:
DEFAULT
LLM
EXECUTOR
EVALUATOR
EVALUATION
TOOL
HUMAN_EVALUATOR
CACHED
UNKNOWN

Status

Status is normalized to "success" or "error".

Input and output

The input and output columns are stored as either raw strings or stringified JSON. The best way to parse them is to try to parse them as JSON, and if it fails, use the raw string. You can also use isValidJSON function right in the query to test for this. input and output columns are also indexed on content, so you can use them in WHERE conditions. Use ILIKE instead of LIKE, because the index is case-insensitive.

Attributes

The attributes column is stored as a string in JSON format. That is, you can safely JSON.parse / json.loads them. In addition, you can use JSON* and simpleJSON* functions on them right in the queries. Attributes are guaranteed to be a valid JSON object.

Model

The model column is set to the response model if present, otherwise it is set to the request model.

Total tokens and total cost

Usually, total_tokens = input_tokens + output_tokens and total_cost = input_cost + output_cost. However, you can manually report these values using the relevant attributes. In this case, totals may not be equal to the sum of the input and output tokens and costs.

Tags

The tags column contains an array of string tags attached to spans. You can filter by tags using array functions:
-- Find spans with a specific tag
SELECT name, tags
FROM spans
WHERE has(tags, 'needs-review')
  AND start_time > now() - INTERVAL 1 DAY

Events

The events column contains an array of event tuples. Each event has:
  • timestamp (Int64): Unix nanoseconds
  • name (String): Event name
  • attributes (String): JSON attributes
To work with events, you can extract timestamps, filter by event name, or unnest events using arrayJoin:
-- Convert event timestamps to DateTime
SELECT 
    span_id,
    arrayMap(t -> toDateTime64(t / 1e9, 9, 'UTC'), tupleElement(events, 'timestamp')) as event_timestamps
FROM spans
WHERE length(events) > 0
  AND start_time > now() - INTERVAL 1 DAY
-- Find spans with specific event names
SELECT span_id, name, events
FROM spans
WHERE arrayExists(e -> tupleElement(e, 'name') = 'cache_hit', events)
  AND start_time > now() - INTERVAL 1 DAY
-- Unnest events to get one row per event
SELECT 
    span_id,
    name as span_name,
    toDateTime64(tupleElement(event, 'timestamp') / 1e9, 9, 'UTC') as event_time,
    tupleElement(event, 'name') as event_name,
    tupleElement(event, 'attributes') as event_attributes
FROM spans
ARRAY JOIN events as event
WHERE start_time > now() - INTERVAL 1 DAY
LIMIT 100

traces

ColumnTypeExample value
idUUID"01234567-1234-4def-8234-426614174000"
start_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
end_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
input_tokensInt64150
output_tokensInt64100
total_tokensInt64250
input_costFloat640.5667
output_costFloat640.123
total_costFloat640.6897
durationFloat641.23
metadataString"{\"key\": \"value\"}"
session_idString"session_123"
user_idString"user_123"
statusString"success"
top_span_idUUID"00000000-0000-0000-1234-426614174000"
top_span_nameString"run"
top_span_typeString"DEFAULT"
trace_typeString"DEFAULT"
tagsArray(String)["needs-review", "production"]
has_browser_sessionBooltrue
id is the trace ID; join to spans with spans.trace_id = traces.id.

Trace type

Here are the values of the trace_type column and their meanings:
DEFAULT
EVALUATION
PLAYGROUND

Duration

The duration is in seconds, and is calculated as end_time - start_time.

Status

Status is set to error if any span in the trace has status error, otherwise it is success.

Metadata

Metadata is stored as a string in JSON format. That is, you can safely JSON.parse / json.loads it. In addition, you can use JSON* and simpleJSON* functions on it right in the queries. Metadata is guaranteed to be a valid JSON object.

signal_events

ColumnTypeExample value
idUUID"01234567-89ab-4def-8234-426614174000"
signal_idUUID"11111111-2222-4333-9444-555555555555"
trace_idUUID"01234567-1234-4def-a234-426614174000"
run_idUUID"22222222-3333-4444-b555-666666666666"
nameString"error.clustered"
payloadString"{\"error_type\": \"timeout\", \"count\": 3}"
timestampDateTime64(9, 'UTC')"2021-01-01 00:00:00"

Payload

The payload column is stored as a string in JSON format. That is, you can safely JSON.parse / json.loads it. In addition, you can use JSON* and simpleJSON* functions on it right in the queries. Payload is guaranteed to be a valid JSON object.

signal_runs

ColumnTypeExample value
signal_idUUID"11111111-2222-4333-8444-555555555555"
job_idUUID"33333333-4444-4555-9666-777777777777"
trigger_idUUID"44444444-5555-4666-a777-888888888888"
run_idUUID"22222222-3333-4444-b555-666666666666"
trace_idUUID"01234567-1234-4def-8234-426614174000"
statusString"COMPLETED"
event_idUUID"55555555-6666-4777-8888-999999999999"
updated_atDateTime64(9, 'UTC')"2021-01-01 00:00:00"

Status

status is one of "PENDING", "COMPLETED", "FAILED", or "UNKNOWN".

logs

ColumnTypeExample value
log_idUUID"01234567-89ab-4def-8234-426614174000"
timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
observed_timeDateTime64(9, 'UTC')"2021-01-01 00:00:01"
severity_numberUInt89
severity_textString"INFO"
bodyString"Processing iteration 1 of 5"
attributesString"{\"stream\": \"stdout\", \"sandbox_id\": \"sbx_123\"}"
trace_idUUID"01234567-1234-4def-9234-426614174000"
span_idUUID"00000000-0000-0000-1234-426614174000"
flagsUInt320
event_nameString"process.stdout"

Attributes

The attributes column is stored as a string in JSON format. That is, you can safely JSON.parse / json.loads it. In addition, you can use JSON* and simpleJSON* functions on it right in the queries. Attributes are guaranteed to be a valid JSON object.

evaluation_datapoints

ColumnTypeExample value
idUUID"01234567-89ab-4def-8234-426614174000"
evaluation_idUUID"98765432-1098-4654-9210-987654321098"
dataString"{\"key\": \"value\"}"
targetString"{\"key\": \"value\"}"
metadataString"{\"key\": \"value\"}"
executor_outputString"{\"key\": \"value\"}"
indexUInt640
trace_idUUID"01234567-1234-4def-a234-426614174000"
group_idString"group_a"
scoresString"{\"score1\": 0.85}"
created_atDateTime64(9, 'UTC')"2021-01-01 00:00:00"
dataset_idUUID"00000000-0000-0000-0000-000000000000"
dataset_datapoint_idUUID"00000000-0000-0000-0000-000000000000"
dataset_datapoint_created_atDateTime64(9, 'UTC')"1970-01-01 00:00:00"
durationFloat641.23
input_costFloat640.5667
output_costFloat640.123
total_costFloat640.6897
start_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
end_timeDateTime64(9, 'UTC')"2021-01-01 00:00:00"
input_tokensInt64150
output_tokensInt64100
total_tokensInt64250
trace_statusString"success"
trace_metadataString"{\"key\": \"value\"}"
trace_tagsArray(String)["production", "experiment-a"]
trace_spansArray(Tuple(name String, duration Float64, type String))[Tuple('openai.chat', 1.23, 'LLM'), ...]
data, target, metadata, executor_output, scores, and trace_metadata are JSON stored as strings. scores is a JSON object of string keys to numeric values. When the datapoint is not sourced from a dataset, dataset_id and dataset_datapoint_id are a nil UUID (all zeroes) and dataset_datapoint_created_at is the Unix epoch. The trace-related columns (duration, input_cost, output_cost, total_cost, start_time, end_time, input_tokens, output_tokens, total_tokens, trace_status, trace_metadata, trace_tags, trace_spans) are joined from the associated trace. If no trace exists for the datapoint, these columns will be nil/empty/zero.

dataset_datapoints

ColumnTypeExample value
idUUID"019c6634-0cb4-7f9d-8192-f36604488483"
created_atDateTime64(9, 'UTC')"2021-01-01 00:00:00"
dataset_idUUID"11111111-2222-4333-a444-555555555555"
dataString"{\"query\": \"What is 2+2?\"}"
targetString"{\"answer\": \"4\"}"
metadataString"{\"source\": \"prod\"}"
data, target, and metadata are JSON stored as strings.

dataset_datapoint_versions

Same schema as dataset_datapoints, but includes all versions and history for each datapoint.

Example Queries

Cost breakdown by model:
SELECT
    model,
    sum(total_cost) AS total_cost,
    count(*) AS call_count
FROM spans
WHERE span_type = 'LLM' AND start_time > now() - INTERVAL 7 DAY
GROUP BY model
ORDER BY total_cost DESC
Slowest operations:
SELECT name, avg(end_time - start_time) AS avg_duration_ms
FROM spans
WHERE start_time > now() - INTERVAL 1 DAY
GROUP BY name
ORDER BY avg_duration_ms DESC
LIMIT 10
Error rate by span type:
SELECT
    name,
    countIf(status = 'error') AS errors,
    count(*) AS total,
    round(errors / total * 100, 2) AS error_rate
FROM spans
WHERE start_time > now() - INTERVAL 1 DAY
GROUP BY name
HAVING total > 10
ORDER BY error_rate DESC
Find spans with specific tags:
SELECT 
    span_id, 
    name, 
    tags, 
    start_time
FROM spans
WHERE has(tags, 'needs-review')
  AND start_time > now() - INTERVAL 7 DAY
ORDER BY start_time DESC
LIMIT 100
Count events by type:
SELECT 
    tupleElement(event, 'name') as event_name,
    count(*) as event_count
FROM spans
ARRAY JOIN events as event
WHERE start_time > now() - INTERVAL 1 DAY
GROUP BY event_name
ORDER BY event_count DESC
Find cache hit rate from events:
SELECT 
    countIf(arrayExists(e -> tupleElement(e, 'name') = 'cache_hit', events)) as cache_hits,
    count(*) as total_spans,
    round(cache_hits / total_spans * 100, 2) as cache_hit_rate
FROM spans
WHERE span_type = 'LLM'
  AND start_time > now() - INTERVAL 1 DAY
Average cost per evaluation datapoint:
SELECT 
    evaluation_id,
    avg(total_cost) as avg_cost,
    avg(duration) as avg_duration_seconds,
    count(*) as datapoint_count
FROM evaluation_datapoints
WHERE evaluation_id = {evaluation_id:UUID}
GROUP BY evaluation_id
Find evaluation datapoints with specific span types:
SELECT 
    id,
    group_id,
    scores,
    trace_spans
FROM evaluation_datapoints
WHERE arrayExists(s -> tupleElement(s, 'type') = 'LLM', trace_spans)
  AND evaluation_id = {evaluation_id:UUID}
LIMIT 100
Count span types across evaluation datapoints:
SELECT 
    tupleElement(span, 'type') as span_type,
    count(*) as span_count,
    avg(tupleElement(span, 'duration')) as avg_duration
FROM evaluation_datapoints
ARRAY JOIN trace_spans as span
WHERE evaluation_id = {evaluation_id:UUID}
GROUP BY span_type
ORDER BY span_count DESC

Exporting Results

Select results and click “Export to Dataset.” Map columns to dataset fields (data, target, metadata). Use this to build evaluation datasets from query results.

Full Reference

For complete ClickHouse SQL syntax, see the ClickHouse documentation.