claude_cli: done event reports last request usage only; add usage_total for the whole run #190

Open
opened 2026-11-14 13:02:43 +00:00 by hermes · 0 comments
Collaborator

In claude_cli mode the done event (NDJSON stream/done and the final done line) reports the usage of the LAST API request only, not of the whole run. Consumers cannot get the real token cost of a run.

Facts

  • crates/hx-ai/src/claude_cli.rs: on the result line, StreamEvent::Done { usage: usage(last.as_ref().unwrap_or(&v["usage"])) } where last is the message.usage of the last assistant message. The source comment says it: ponytail: billing/stats see the last request only; split Done if exact sums matter. The reason is deliberate: the last request's usage is the real context size for compaction (result sums all requests).
  • The claude stream-json result line has the summed usage of all requests of the run (usage / modelUsage).
  • Observed with orch (hx 0.1.13, claude_cli): a 12-run bench reports 39363 tokens per run (cache 99 %), the size of one request (a single-request session measured 39623 with hx stats), although the runs do several tool turns. Per-run cost and a max_tokens budget are therefore meaningless in this mode; orch can only compare runs of the same mode with each other.

Wanted

Keep usage as is (context size) and add the run total next to it, so nothing that relies on the current meaning breaks:

  1. StreamEvent::Done (or a new event just before it) carries usage_total taken from the result line: input_tokens, output_tokens, cache_read_tokens, cache_write_tokens, summed over all requests of the claude run. For providers that make one request per hx turn usage_total is the sum of that turn's requests (equal to usage when there is one request).
  2. NDJSON: the final done line gets usage_total for the whole hx run (sum over its turns and subagents, like Agent::usage()), usage stays the existing field. stream/done events carry usage_total of their own request group.
  3. hx stats prints the claude_cli totals separately (it shows requests 1 today).

Done when

  • A test with a fake claude whose result line sums two assistant usages: done.usage is the last request, done.usage_total the sum. Direct-provider tests are unchanged.
  • NDJSON documentation in the README lists usage_total.

Consumer: johannes/orch (telemetry, max_tokens budget; will use usage_total when present and fall back to usage).

In `claude_cli` mode the `done` event (NDJSON `stream/done` and the final `done` line) reports the usage of the LAST API request only, not of the whole run. Consumers cannot get the real token cost of a run. ## Facts - `crates/hx-ai/src/claude_cli.rs`: on the `result` line, `StreamEvent::Done { usage: usage(last.as_ref().unwrap_or(&v["usage"])) }` where `last` is the `message.usage` of the last `assistant` message. The source comment says it: `ponytail: billing/stats see the last request only; split Done if exact sums matter`. The reason is deliberate: the last request's usage is the real context size for compaction (`result` sums all requests). - The claude stream-json `result` line has the summed usage of all requests of the run (`usage` / `modelUsage`). - Observed with orch (hx 0.1.13, claude_cli): a 12-run bench reports 39363 tokens per run (cache 99 %), the size of one request (a single-request session measured 39623 with `hx stats`), although the runs do several tool turns. Per-run cost and a `max_tokens` budget are therefore meaningless in this mode; orch can only compare runs of the same mode with each other. ## Wanted Keep `usage` as is (context size) and add the run total next to it, so nothing that relies on the current meaning breaks: 1. `StreamEvent::Done` (or a new event just before it) carries `usage_total` taken from the `result` line: `input_tokens`, `output_tokens`, `cache_read_tokens`, `cache_write_tokens`, summed over all requests of the claude run. For providers that make one request per hx turn `usage_total` is the sum of that turn's requests (equal to `usage` when there is one request). 2. NDJSON: the final `done` line gets `usage_total` for the whole hx run (sum over its turns and subagents, like `Agent::usage()`), `usage` stays the existing field. `stream/done` events carry `usage_total` of their own request group. 3. `hx stats` prints the claude_cli totals separately (it shows `requests 1` today). ## Done when - A test with a fake claude whose `result` line sums two `assistant` usages: `done.usage` is the last request, `done.usage_total` the sum. Direct-provider tests are unchanged. - NDJSON documentation in the README lists `usage_total`. Consumer: johannes/orch (telemetry, `max_tokens` budget; will use `usage_total` when present and fall back to `usage`).
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: johannes/hx#190