Skip to content

Track agent health: numbers across runs and error classes

View as Markdown

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:

Terminal window
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:

{
"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.

GET /api/stats needs the capability stats, present when the database aggregates: both first-party backends do, through obsdb.Aggregator. Without it, capabilities_off says why.

ParameterMeans
byagent, model, tool, error_type or step (required)
sincethe window’s start: RFC 3339, or a duration back from until (90m, 24h, 7d); default 24h
untilthe window’s end, RFC 3339; default now
agentone agent’s runs, exact
tag.<k>=<v>, public_id, experiment_id, statusas /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
playgroundfalse (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:

FieldCounts
keythe agent, model, tool, error type or step
runs, failedthe group’s runs, and those that failed
failure_ratefailed / runs for agent and error_type; failed_calls / calls for model, tool and step; null with nothing to divide
calls, failed_callsmodel calls, or tool calls for by=tool
input_tokens, output_tokensthe model calls’ own usage, never a run’s rolled-up total
the cost blockeach 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)
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_ratemodel 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
denialsrefused tool calls
series24 buckets, oldest first, each {runs, failed, calls, failed_calls, input_tokens, output_tokens}; bucket_ms is the window cut in 24
delegatedby=agent alone: {runs, input_tokens, output_tokens, cost block}, what the agent’s subagents spent for it
runs_queryagent 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.

  • 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). ClickHouse groups otel_traces natively.

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 firstThe 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
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.

A failure’s error text carries numbers and ids that differ run to run. 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:

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:

{"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). obsdb.ErrorClasses and ErrorClassTexts do the same folding in Go.

  • 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). (Source: studio/stats.go:76-79)

Symbols used: obsdb.Aggregator · obsdb.ErrorClass · obsdb.ErrorClasses · obsdb.ErrorClassTexts