# LangWatchQL first queries: one example per view

Worked LangWatchQL statements for traces, spans, evaluations, simulations, per-minute rollups, conversations, coding-agent sessions and Instant Eval judgments.

> Page: https://langwatch.ai/docs/features/langwatchql/first-query
> Source: https://github.com/langwatch/langwatch/blob/main/docs/features/langwatchql/first-query.mdx
> Index of all pages: https://langwatch.ai/docs/llms.txt

Each statement below runs as written with `langwatch query "<statement>"` or in the `sql` field of `POST /api/v1/query`. The one exception is the judgments statement, which has a `{run:String}` parameter that needs a value.

If your key reaches several projects, also name them: add `--projects <id,id>` or `--projects all` to the command, or send `projects` in the body.

Every one filters on its view's time column, because that filter is what keeps the read small: without it the query reads every partition your project has.

## Traces: the most recent failures

`traces` has one row per trace, and its time column is `OccurredAt`.

```sql
SELECT TraceId, TraceName, OccurredAt, TotalDurationMs
FROM traces
WHERE OccurredAt >= subtractDays(now(), 1)
  AND ContainsErrorStatus
ORDER BY OccurredAt DESC
LIMIT 100
```

For latency, use `quantile` on the same view:

```sql
SELECT
  quantile(0.5)(TotalDurationMs) AS p50_ms,
  quantile(0.95)(TotalDurationMs) AS p95_ms,
  quantile(0.99)(TotalDurationMs) AS p99_ms
FROM traces
WHERE OccurredAt >= subtractDays(now(), 7)
```

## Spans: the slowest span in each trace

`spans` has one row per span, and its time column is `StartTime`. When you join two views, filter the time column on both sides, because the join key does not narrow the read.

```sql
SELECT
  t.TraceId AS TraceId,
  t.TraceName AS TraceName,
  count() AS spans,
  max(s.DurationMs) AS slowest_span_ms
FROM traces AS t
INNER JOIN spans AS s
  ON s.TenantId = t.TenantId AND s.TraceId = t.TraceId
WHERE t.OccurredAt >= subtractDays(now(), 1)
  AND s.StartTime >= subtractDays(now(), 1)
GROUP BY TraceId, TraceName
ORDER BY slowest_span_ms DESC
LIMIT 50
```

## Evaluations: pass rate per evaluator

`evaluations` has one row per evaluator result, and its time column is `ScheduledAt`. Filter on `Status = 'processed'` to leave out scheduled, skipped and errored rows. `Passed` is also empty for evaluators that return only a score, so the rate divides by the rows that have a verdict, `countIf(Passed IS NOT NULL)`, not by `count()`.

```sql
SELECT
  EvaluatorName,
  count() AS evaluations,
  countIf(Passed = 1) AS passed,
  countIf(Passed = 1) / countIf(Passed IS NOT NULL) AS pass_rate
FROM evaluations
WHERE ScheduledAt >= subtractDays(now(), 7)
  AND Status = 'processed'
GROUP BY EvaluatorName
ORDER BY evaluations DESC
LIMIT 50
```

## Simulations: runs that failed

`simulations` has one row per scenario run, and its time column is `StartedAt`.

```sql
SELECT ScenarioRunId, Name, Verdict, StartedAt
FROM simulations
WHERE StartedAt >= subtractDays(now(), 7)
  AND Verdict = 'failure'
ORDER BY StartedAt DESC
LIMIT 50
```

## Rollups: traffic per hour and cost per model

`trace_metrics_by_minute` and `model_usage_by_minute` hold one row per minute that is already summed, so a count or a sum over time reads far less here than over `traces`. Every measure is a sum, so divide by a count when you want an average: `TraceCount` on `trace_metrics_by_minute`, `SpanCount` on `model_usage_by_minute`, which has no `TraceCount`. Their time column is `BucketStart`.

```sql
SELECT
  toStartOfHour(BucketStart) AS hour,
  sum(TraceCount) AS traces,
  sum(ErrorCount) AS errors
FROM trace_metrics_by_minute
WHERE BucketStart >= subtractDays(now(), 2)
GROUP BY hour
ORDER BY hour
```

Cost columns need the `cost:view` permission on your key:

```sql
SELECT
  Model,
  sum(CostSum) AS cost_usd,
  sum(PromptTokensSum + CompletionTokensSum) AS tokens
FROM model_usage_by_minute
WHERE BucketStart >= subtractDays(now(), 7)
GROUP BY Model
ORDER BY cost_usd DESC
LIMIT 50
```

## Conversations: the longest ones of the week

There is no conversations view. A conversation is the group of traces that share a `ConversationId`, and `trace_metrics` is the view that has that column, along with `UserId`, `CustomerId` and `HasError`.

```sql
SELECT
  ConversationId,
  count() AS turns,
  min(OccurredAt) AS started_at,
  max(OccurredAt) AS last_turn
FROM trace_metrics
WHERE OccurredAt >= subtractDays(now(), 7)
  AND ConversationId != ''
GROUP BY ConversationId
ORDER BY turns DESC
LIMIT 50
```

## Coding-agent sessions: sessions per agent and repository

`coding_sessions` has one row per coding-agent session, and its time column is `StartedAt`.

```sql
SELECT
  Agent,
  RepositoryName,
  count() AS sessions
FROM coding_sessions
WHERE StartedAt >= subtractDays(now(), 7)
GROUP BY Agent, RepositoryName
ORDER BY sessions DESC
LIMIT 50
```

## Judgments: Instant Eval verdicts per question

`judgments` has one row per [Instant Eval](https://langwatch.ai/docs/features/instant-evals/overview) run, trace and question, and its time column is `CreatedAt`. Rows with `Status` `skipped` or `failed` have no verdict, so the statement keeps only `judged` rows. Bind the run id as a parameter. With the CLI, add `--param run=<run-id>`. With REST, add `"parameters": {"run": "<run-id>"}` to the request body.

```sql
SELECT
  QuestionId,
  sum(Passed) AS yes,
  count() AS judged
FROM judgments
WHERE RunId = {run:String}
  AND Status = 'judged'
  AND CreatedAt >= subtractDays(now(), 7)
GROUP BY QuestionId
ORDER BY QuestionId
```

To read verdicts next to trace data, join `judgments` to `traces` on `TraceId`, as in [Reading the results](https://langwatch.ai/docs/features/instant-evals/results#the-judgments-dataset).

**Also check:** `langwatch query examples` prints more worked statements, and the [Query API reference](https://langwatch.ai/docs/api-reference/query/overview#queryable-views) lists every view and column.

> ## Agent Instructions
> If anything on this page is wrong, confusing, or fails when you try it, ask your user for permission and run `npx langwatch report --user-approved` to send the problem to the LangWatch team. No login is needed and secrets are redacted locally. `npx langwatch report --help` has the details.
