# When the client asks if the agent got worse, answer with DuckDB

> A GROUP BY over one JSONL trace file shows which tool slipped between weeks, but finding out why still means reading the traces.

Original: https://fdetimes.net/en/guides/duckdb-agent-trace-analysis-jsonl/

"Error analysis is the most important activity in evals," Hamel Husain writes. The way he starts sounds simple: open the traces and write down every problem you see. Counting comes later, and in SQL it is just a `GROUP BY`.

Why should an FDE care? On Monday morning the client will not ask for an eval score. They will ask: "Did the agent get worse this week, and where?" Usually all you have is a JSONL trace file exported from their system, with no dashboard built yet.

This guide shows how to answer that question with DuckDB, on a laptop. By the end you will have a table comparing latency and error rate for each tool across two weeks, plus a categorized list of failures drawn from the traces you read yourself.

## What do you need before the first query?

Install DuckDB following the official instructions. The CLI or Python both work, since every step below is plain SQL. You also need a trace file in which each line is one span in JSON.

To keep things easy to follow, the guide uses a hypothetical, simplified schema. Each line looks like this:

```json
{"ts":"2026-11-18T09:12:03Z","trace_id":"t-812","operation":"execute_tool","duration_ms":4210,"error_type":null,"input_tokens":5120,"attributes":{"tool":{"name":"search_orders"}}}
```

A client's real logs will name fields according to the exporter they use. OpenTelemetry's GenAI semantic conventions have their own rules for tool-execution spans, with the operation name `execute_tool`, and specify `error.type` when an operation ends in an error. The spec is still in Development status, so check the latest version before naming your columns.

## Step 1: load the file and make the counts match

In DuckDB, `read_ndjson` is an alias for `read_json` with `format` preset to `newline_delimited`. For logs, declaring the format explicitly means you know exactly what you are reading.

```sql
CREATE TABLE spans AS
SELECT * FROM read_ndjson('agent_traces.jsonl');

SELECT count(*) FROM spans;
DESCRIBE spans;
```

**Check:** count the file's lines with `wc -l` and compare with `count(*)`. If the two numbers differ, ask the client what lines the file contains before trusting any figure that follows.

**Common mistake:** the client sends one JSON file holding a single large array rather than one object per line. The `format` parameter of `read_json` accepts `auto`, `unstructured`, `newline_delimited` or `array`, so in this case use `read_json` with `format = 'array'`.

## Step 2: extract the tool name and split the two weeks

The tool name sits deep inside `attributes`. The `->>` operator extracts a JSON field as text. The example below assumes the `attributes` column was read as JSON, so look at the `DESCRIBE` output from step 1 before running it.

```sql
CREATE VIEW s AS
SELECT
  CASE WHEN ts >= TIMESTAMP '2026-11-16'
       THEN 'this_week' ELSE 'last_week' END AS week,
  trace_id,
  attributes->>'$.tool.name' AS tool,
  duration_ms, error_type, input_tokens
FROM spans
WHERE operation = 'execute_tool'
  AND ts >= TIMESTAMP '2026-11-09';
```

The week boundary is hard-coded with `CASE` for readability. On a real engagement, agree the cutoff date with the client, especially if they shipped a new prompt midweek. If `DESCRIBE` shows `ts` as `VARCHAR`, cast it with `CAST(ts AS TIMESTAMP)` before comparing.

**Check:** run `SELECT tool, count(*) FROM s GROUP BY tool`. A large `NULL` group means the JSON path is wrong for some spans. Also run `SELECT week, count(*) FROM s GROUP BY week` to make sure each week has a sensible number of spans.

## Step 3: p95 per tool shows where the agent is slow

Averages easily hide problems, so compare percentiles. `quantile_cont` computes interpolated quantiles.

```sql
SELECT tool, week,
  count(*) AS span_count,
  quantile_cont(duration_ms, 0.5)  AS p50,
  quantile_cont(duration_ms, 0.95) AS p95
FROM s
GROUP BY tool, week
ORDER BY tool, week;
```

For very large trace sets, DuckDB also offers an approximate quantile function based on T-Digest (`approx_quantile`), which is cheaper to run.

Read the results in pairs of rows for the same tool. If p50 holds steady while p95 doubles, a small group of requests has probably run into trouble while the tool as a whole has not slowed down. That is still a hypothesis, to be tested in step 5.

## Step 4: error rates in a single query

The `FILTER` clause lets you count total spans and failed spans side by side in one statement, so error rate by tool and week takes a single run:

```sql
SELECT tool, week,
  count() AS n,
  count() FILTER (error_type IS NOT NULL) AS errors,
  round(100.0 * count() FILTER (error_type IS NOT NULL)
        / count(), 1) AS error_pct
FROM s
GROUP BY tool, week
ORDER BY error_pct DESC;

SELECT error_type, count(*) AS n
FROM s
WHERE week = 'this_week' AND tool = 'search_orders'
GROUP BY error_type ORDER BY n DESC;
```

In the OpenTelemetry spec, `error.type` describes the class of error the operation hit, so it makes a sensible group-by key. The spec only requires the field when an operation fails, though, so check that the client's exporter actually populates it before treating `NULL` as "fine".

To see how to read the output, suppose the results look like this. Last week `search_orders` had 40 errors across 2,000 spans, or 2%; this week it had 84 errors across 2,100 spans, or 4%. Traffic barely changed and the error rate doubled, so this week's `search_orders` row is the first thing to open up.

## Step 5: the numbers point to the spot; the traces give the reason

This is where many engineers stop, screenshot the table, send it to the client and guess at the cause. Anthropic Engineering has the opposite rule: do not trust eval scores until someone has dug into the details. The same goes for your table.

```sql
SELECT DISTINCT trace_id
FROM s
WHERE week = 'this_week'
  AND tool = 'search_orders'
  AND error_type IS NOT NULL
LIMIT 30;
```

Husain advises starting small: open the traces and simply write notes on the problems you see. Langfuse's cookbook adds a rule for each note: describe the behavior, do not diagnose. Write "agent passed the order ID in the wrong format", rather than "prompt lacks examples".

Thirty traces is a reasonable start for an afternoon. Langfuse suggests a target of about 100 traces, chosen to cover a range of cases rather than at random, and deliberately oversampling traces already flagged as problematic. Assign categories only after reading, then count:

```sql
CREATE TABLE notes (trace_id VARCHAR, note VARCHAR, category VARCHAR);
INSERT INTO notes VALUES
  ('t-812', 'agent passed the order ID in the wrong format', 'wrong_argument');

SELECT category, count(*) AS n,
  round(100.0 * count(*) /
        (SELECT count(DISTINCT trace_id) FROM notes), 1) AS pct
FROM notes GROUP BY category ORDER BY n DESC;
```

The `pct` column follows Langfuse's definition: a category's failure rate is the percentage of traces tagged with that category. That gives you a baseline to compare against once the client ships a fix and sends next week's traces.

**Key point:** A dashboard tells you where the agent broke; only reading the traces tells you why.

**Common mistake:** naming categories before reading. Do that and you will find exactly what you expected to find, and miss the new kind of failure that appeared this week.

## What does this skill look like on a client site?

The JSONL file usually comes from a bucket, or from a quick export the client sends over. DuckDB's advantage is that you have an answer the same afternoon, without waiting for anyone to build an observability pipeline.

A good answer has three parts: which tool got worse, by how much compared with last week, and which failure category accounts for the largest share of the traces you read.

If an FDE job posting mentions evals or observability, put this skill on your CV with numbers: which failure category you found, in how many traces, and how the error rate changed after the fix. Bringing this set of queries and a mock trace file to an interview is far more convincing than a line claiming experience with LLM monitoring.

## Self-check: six lines, three questions

Save these six lines as `sample.jsonl` and rerun the steps above:

```json
{"ts":"2026-11-10T08:00:00Z","trace_id":"t-1","operation":"execute_tool","duration_ms":1000,"error_type":null,"input_tokens":900,"attributes":{"tool":{"name":"search_orders"}}}
{"ts":"2026-11-11T08:00:00Z","trace_id":"t-2","operation":"execute_tool","duration_ms":1200,"error_type":null,"input_tokens":950,"attributes":{"tool":{"name":"search_orders"}}}
{"ts":"2026-11-17T08:00:00Z","trace_id":"t-3","operation":"execute_tool","duration_ms":1100,"error_type":"timeout","input_tokens":1800,"attributes":{"tool":{"name":"search_orders"}}}
{"ts":"2026-11-18T08:00:00Z","trace_id":"t-4","operation":"execute_tool","duration_ms":3900,"error_type":"timeout","input_tokens":2100,"attributes":{"tool":{"name":"search_orders"}}}
{"ts":"2026-11-18T09:00:00Z","trace_id":"t-5","operation":"execute_tool","duration_ms":800,"error_type":null,"input_tokens":400,"attributes":{"tool":{"name":"get_invoice"}}}
{"ts":"2026-11-19T08:00:00Z","trace_id":"t-6","operation":"execute_tool","duration_ms":1300,"error_type":null,"input_tokens":1000,"attributes":{"tool":{"name":"search_orders"}}}
```

Three questions: what does `count(*)` return, what is the error rate of `search_orders` each week, and what is its p50 this week? The correct answers are 6 lines; 0% last week and 66.7% this week (2 of 3 spans); and a p50 of 1,300 ms this week. If you miss one, go back to that step.

The next time a client asks whether the agent has got worse, you will not have to answer on gut feel. You will have a table of numbers, 30 lines of notes and a clear idea of what to fix first.

**Try this week:**

- Export the last 14 days of traces to JSONL and run the p95 and error-rate queries by tool, comparing this week with last week
- Take 30 failed traces from the worst tool–week group, write a one-line behavioral description for each, then compute rates by category
- Check the attribute names in your logs against the latest OTel GenAI semantic conventions, since the spec is still in Development status

## Sources

- [Loading JSON – DuckDB](https://duckdb.org/docs/2.0/data/json/loading_json.html)

- [JSON Processing Functions – DuckDB](https://duckdb.org/docs/2.0/data/json/json_functions.html)

- [Aggregate Functions – DuckDB](https://duckdb.org/docs/2.0/sql/functions/aggregates.html)

- [FILTER Clause – DuckDB](https://duckdb.org/docs/2.0/sql/query_syntax/filter.html)

- [Semantic conventions for generative client AI spans](https://raw.githubusercontent.com/open-telemetry/semantic-conventions-genai/main/docs/gen-ai/gen-ai-spans.md)

- [Q: Why is "error analysis" so important in AI evals, and how is it performed?](https://hamel.dev/blog/posts/evals-faq/why-is-error-analysis-so-important-in-llm-evals-and-how-is-it-performed.html)

- [Ep. #79, AI and Otel: Look at your Data with Hamel Husain (O11ycast, Heavybit)](https://www.heavybit.com/library/podcasts/o11ycast/ep-79-ai-and-otel-look-at-your-data-with-hamel-husain)

- [Error Analysis for LLM Applications – Langfuse cookbook](https://langfuse.com/guides/cookbook/error-analysis-llm-applications)

- [Demystifying evals for AI agents – Anthropic Engineering](https://www.anthropic.com/engineering/demystifying-evals-for-ai-agents)
