---
title: 'Track agent health: numbers across runs and error classes'
description: Read runs, failure rates, tokens, cost, latency, time to first token, retries and denials per agent, model, tool, error type or step from GET /api/stats and Studio's Agents page, and group failures into error classes to find the ones that are the same failure.
url: https://weftgo.dev/docs/observe/studio/stats/
type: how-to
sources:
  - 'weft/studio/stats.go:16-104 (GET /api/stats: the parameters, the defaults, by=model and by=step keys, the window, every wire field, delegated, runs_query, nearest-rank percentiles, content-off safe, groups_cut); statsBody'
  - 'weft/obsdb/stats.go:26 (Aggregator), 73-79 (StatsBuckets 24, MaxStatsBuckets 366, MaxStatsGroups 200), 119 (CheckStats), 401-430 (ErrorClassCount, ErrorClass: N, <id>, quoted spans kept), 543 (ErrorClasses), 570 (ErrorClassTexts)'
  - 'weft/obsdb/example_test.go:467-473 (ExampleErrorClass: the two outputs)'
  - 'weft/studio/testdata/api/stats-agent.golden.json (one by=agent group: researcher, 5 runs, 3 failed, unpriced, runs_query)'
  - 'weft/studio/search.go:37-39 (error_class filter, capability stats), 80-99 (searchFacets: error_class, error_class_capped; errorClassCount)'
  - 'weft/studio/testdata/api/search-usage-limit.golden.json (TestUsageLimitOneFacetOneMatrix: seven usage-limit failures, one class of 7)'
  - weft/studio/web/src/lib/stats.ts:16-57 (STATS_WINDOWS 24h and 7d, TYPICAL_MIN_RUNS 5, STATS_WORDS), 90 (groupMetrics), 170 (statsRunsSearch), 227-270 (typicalChips)
  - weft/studio/web/src/components/studio/error-class-facet.tsx (FACET_WORDS, the facet, Experiment on these N →)
  - weft/studio/web/src/routes/runs.index.tsx:182-184 (error_class needs capability stats), 811-821 (the facet, the matrix links)
  - weft/studio/web/src/components/studio/spark.tsx (the sparkline and the bar list)
  - 'W11-tag3, v0.20.2: weft/studio/web/src/components/studio/spark.tsx:108-125 (BarList: one grid, each row a subgrid, the value column the widest value''s); weft/CHANGELOG.md:17 (0.20.2)'
  - weft/studio/README.md (One failure class, one matrix, a pass predicate (U24.2))
  - 'weft/CHANGELOG.md:113-116 (0.19.0: obsdb Aggregator, ErrorClass, GET /api/stats, facets.error_class), 127-128 (the Agents page''s numbers, vs typical chips; the error-class facet), 168 (schema 0025: span_stats)'
---

# Track agent health: numbers across runs and error classes

`GET /api/stats` answers numbers across runs: runs, failures, tokens, cost, latency, time to first token, retries and denials, grouped by agent, model, tool, error type or step, with a 24-bucket series over the window. Studio's Agents page draws it per agent card, and the runs list groups failures into **error classes**: error texts with their numbers and ids normalised, so seven usage-limit failures at different steps read as one class of 7. An agent's numbers link to the runs they count.

Ask for the last day's numbers per agent, against `weft studio` with its token in `$WEFT_STUDIO_TOKEN`:

```sh
curl -s -H "Authorization: Bearer $WEFT_STUDIO_TOKEN" \
  'http://127.0.0.1:7331/api/stats?by=agent&since=24h' | jq '.groups[] | del(.series)'
```

One group, from weft's own golden `testdata/api/stats-agent.golden.json`:

```json
{
  "key": "researcher",
  "runs": 5, "failed": 3, "failure_rate": 0.6,
  "calls": 11, "failed_calls": 0,
  "input_tokens": 176, "output_tokens": 58,
  "cost_usd": null, "cost_known": false, "cost_badge": "unpriced",
  "cost_reason": "1 of 1 models unpriced: no price for zai/glm-5.3-flash",
  "cache": "not_reported", "unpriced_models": ["zai/glm-5.3-flash"],
  "duration_ms": {"p50": 3000, "p95": 5000, "n": 5},
  "ttft_ms": {"p50": 600, "p95": 1100, "n": 11},
  "retried": 0, "retry_rate": 0, "denials": 0,
  "delegated": {"runs": 0, "input_tokens": 0, "output_tokens": 0, "cost_usd": 0, "cost_known": true, "cache": "exact"},
  "runs_query": "agent=researcher&all=1&playground=false&since=2020-01-01T00%3A00%3A00Z&until=2020-01-02T00%3A00%3A00Z"
}
```

`runs_query` is the `/api/search` query whose total is the group's runs: the link a card's numbers open.

## The stats API

`GET /api/stats` needs the capability `stats`, present when the database aggregates: both first-party backends do, through [`obsdb.Aggregator`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#Aggregator). Without it, `capabilities_off` says why.

| Parameter | Means |
|---|---|
| `by` | `agent`, `model`, `tool`, `error_type` or `step` (required) |
| `since` | the window's start: RFC 3339, or a duration back from `until` (`90m`, `24h`, `7d`); default `24h` |
| `until` | the window's end, RFC 3339; default now |
| `agent` | one agent's runs, exact |
| `tag.<k>=<v>`, `public_id`, `experiment_id`, `status` | as `/api/runs` reads them |
| `parent` | `*` (the default): every run, a subagent's child runs each counted under its own agent; empty: the top-level runs; a run id: its children |
| `playground` | `false` (the default): the app's own runs; `true`: playground runs alone; `all`: both. With `experiment_id` and no `playground`, the default is `all` |

Two defaults differ from `/api/runs`'. `parent` counts child runs, and `playground` leaves replays out, so health numbers never mix scripted or playground replays into the app's own. A group's `runs_query` carries the effective filter, so its link lands on the same runs. An unknown `by`, `playground` or `status`, or a bad `since` or `until`, is a 400; so is a window outside the years 1678 to 2262. A panel token's numbers are its own public id's alone.

The answer is `{by, since, until, buckets, bucket_ms, groups, groups_cut}`. A run is in the window when it started there, and its spans count with it. Each group carries:

| Field | Counts |
|---|---|
| `key` | the agent, model, tool, error type or step |
| `runs`, `failed` | the group's runs, and those that failed |
| `failure_rate` | `failed` / `runs` for `agent` and `error_type`; `failed_calls` / `calls` for `model`, `tool` and `step`; `null` with nothing to divide |
| `calls`, `failed_calls` | model calls, or tool calls for `by=tool` |
| `input_tokens`, `output_tokens` | the model calls' own usage, never a run's rolled-up total |
| the cost block | each model's usage priced at read time: unknown, never $0, when a model with usage has no price; for `by=tool` the block is there with `cost_usd` `null` and the badge `unknown`: tool calls bill no model usage ([See what a run cost](https://weftgo.dev/docs/observe/studio/cost/)) |
| `duration_ms` | `{p50, p95, n}`: run durations for `agent` and `error_type`, call durations otherwise; `null` when `n` is 0 |
| `ttft_ms` | `{p50, p95, n}` over the model calls' time to first token |
| `retried`, `retry_rate` | model calls that took more than one attempt, and that over calls; `retry_rate` is `null` for `by=tool` (no model calls) or with no calls |
| `denials` | refused tool calls |
| `series` | 24 buckets, oldest first, each `{runs, failed, calls, failed_calls, input_tokens, output_tokens}`; `bucket_ms` is the window cut in 24 |
| `delegated` | `by=agent` alone: `{runs, input_tokens, output_tokens, cost block}`, what the agent's subagents spent for it |
| `runs_query` | `agent` and `error_type` groups with a key only: the `/api/search` query of the group's runs. `model`, `tool` and `step` groups carry none |

At most 200 groups are returned; `groups_cut` says how many more there were.

## What the numbers count

- **A child run counts under its own agent.** An agent's own figures never include its subagents' calls; `delegated` holds those, the model calls of the Subagent runs below its runs whose agent is another. One call can sit under several agents' `delegated` (boss → helper → boss), so never sum `delegated` across groups.
- **A derived run counts as a run.** A run built from stock OTel spans has no calls of its own here, so a group's runs match the runs list.
- **`by=model` keys on the model name alone.** Two providers' models of one name are one group; its cost is still priced per provider and model.
- **`by=step` keys on each run's own step index.** A replay numbers its steps from 0 at its `from_step`, so a replay from step 3 counts under step 0. The default `playground=false` leaves replays out.
- **Percentiles are nearest-rank** and exact on both backends.
- **No content is read.** Every number is a span attribute or a run column, so the numbers hold with content off.

The SQLite backend answers from a `span_stats` table written at ingest beside every stored `invoke_agent`, `chat` and `execute_tool` span (migration 0023). Opening a file older than that backfills it in batches, and `Open` logs one INFO line with the span count before and one with the duration after: a large file's first open waits for it ([Choose the observability database](https://weftgo.dev/docs/production/storage-backends/#migrations)). ClickHouse groups `otel_traces` natively.

## The Agents page numbers

Studio's Agents page reads `GET /api/stats` over the last 24 hours or 7 days, the window riding the URL (`?window=7d`). Each agent card gets a numbers strip:

![The Agents page's numbers over the last 24 hours: runs, failure rate and tokens per agent as bar charts, one row per agent, most runs first](https://weftgo.dev/docs/_astro/studio-agents-stats.light.pAZF0YpS.webp)

*Runs, failure rate and tokens per agent over the last 24 hours.*

- runs, failed %, duration p50 · p95, first token p50, tokens, and cost, or its badge ("unpriced", never $0);
- retried %, denials, and sparklines over the window;
- the subagents' bill apart, as *delegated*, its hover saying it is never summed across agents.

One chart per metric compares the agents, every bar ending at one place whatever the width of its figure, and each tool card gets its calls, failed %, p50 and p95. Agents seen in runs that no `weft.json` or connected runtime registers are listed dimmed. Each agent card's numbers link to the runs they count through its group's `runs_query`; a tool's numbers have no runs link, since `by=tool` groups carry no `runs_query`. A registered agent always gets its own numbers: when the 200-group cap leaves one out, the page reads it alone over the same window, and says "numbers not read" with why while that read is pending or failed, never "no runs".

On a run's header, **vs typical** chips compare the run with its agent's runs over the last 7 days: its duration against their p50, its tokens and cost against their mean per run. They need at least 5 runs and say so otherwise; an unpriced side draws no cost chip. A playground or scripted run's chips say they compare it with the app's runs.

## Error classes

A failure's error text carries numbers and ids that differ run to run. [`obsdb.ErrorClass`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClass) normalises it. A number becomes `N` (with its unit kept, `after Ns`). A UUID, a hex id of 8 or more characters with a digit, or a word one of whose `_`- or `-`-separated parts is 8 or more letters and digits mixed (`call_8f3a9b2c11`, `toolu_01ABCdefGH`) becomes `<id>`. Any other word keeps its digits (a model name like `glm-5.3-flash`), and a quoted span stays as written. From its package example:

```text
weft: run failed at step 2: weft: run exceeded its usage limit (input 30/25, output 15/0)
→ weft: run failed at step N: weft: run exceeded its usage limit (input N/N, output N/N)

tool "lookup_42" retried 3 times, past MaxModelRetries(2)
→ tool "lookup_42" retried N times, past MaxModelRetries(N)
```

`/api/search` answers `facets.error_class`, the match's failures grouped by class, most runs first, each `{class, count}`. The facet is counted over the match without its own `error_class` filter, so it lists the classes a pick could switch to; `error_class_capped` is true when the match held more distinct error texts than the server reads. `error_class=<class>` filters `/api/search` and `/api/runs` to that class, exactly; it needs the `stats` capability. Weft's golden for seven usage-limit failures at different steps and one loop failure:

```json
{"error_class": [
  {"class": "weft: run failed at step N: weft: run exceeded its usage limit (input N/N, output N/N)", "count": 7},
  {"class": "weft: run failed at step N: weft: the model repeated the same tool calls too many times: N identical steps in a row", "count": 1}
]}
```

On the runs list, a list of failed runs (or one class) reads `/api/search` and draws the facet as picks with their counts, under "error class". A pick is the `error_class` chip. With one class picked, the list offers **Experiment on these N →** per agent: a matrix over those runs, each continuing from its own failing step ([A matrix from a failure class](https://weftgo.dev/docs/observe/playground/experiments/#a-matrix-from-a-failure-class)). [`obsdb.ErrorClasses`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClasses) and [`ErrorClassTexts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClassTexts) do the same folding in Go.

## Best practices & gotchas

- **Keep replays out of health numbers.** The default `playground=false` already does; pass `playground=all` only when you mean to count experiments. (Source: `studio/stats.go:39-48`)
- **Never sum `delegated` across agents.** One subagent call can sit under several agents' figures. Sum the groups' own figures for a total. (Source: `studio/stats.go:89-95`; `lib/stats.ts` `STATS_WORDS`)
- **Read `by=step` on the app's own runs.** A replay's step 0 is its source's `from_step`, so mixing replays in shifts the steps. (Source: `studio/stats.go:54-61`)
- **Treat an unpriced group as unknown, not free.** Its cost is `null` with a reason; supply a price to read it ([Supply your own table](https://weftgo.dev/docs/observe/studio/cost/#supply-your-own-table)). (Source: `studio/stats.go:76-79`)

## See also

- [Find runs: the runs list, search and filters](https://weftgo.dev/docs/observe/studio/runs/)
- [Run experiments with the option lab](https://weftgo.dev/docs/observe/playground/experiments/)
- [See what a run cost](https://weftgo.dev/docs/observe/studio/cost/)
- [Studio HTTP API](https://weftgo.dev/docs/reference/studio-api/)
- [obsdb: reading the observability database from Go](https://weftgo.dev/docs/reference/obsdb/)

Symbols used: [`obsdb.Aggregator`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#Aggregator) · [`obsdb.ErrorClass`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClass) · [`obsdb.ErrorClasses`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClasses) · [`obsdb.ErrorClassTexts`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/obsdb#ErrorClassTexts)
