Skip to content

feat: capture the token usage Providers already report (0.15.0) - #16

Merged
chivopic merged 1 commit into
mainfrom
claude/progress-check-pqu8i6
Sep 7, 2026
Merged

chivopic merged 1 commit into
mainfrom
claude/progress-check-pqu8i6

Conversation

@chivopic

@chivopic chivopic commented Sep 7, 2026

Copy link
Copy Markdown
Owner

Stage 12. Stage 11 made "can this agent do the job" answerable, but the report had no cost in it — pass rate, turns, wall time. A prompt change that lifts the pass rate from 70% to 75% while tripling the tokens is usually a bad trade, and it looked like a pure win. Providers return usage on every call and chivgent was throwing it away.

What it does

Usage flows from the Provider response through to the run: per turn on message_end, per run on agent_end and the run result, per attempt in the eval report, and cumulatively behind /session.

task                  pass  turns  tokens  tools                       p50
find-auth-logic       4/5   2.0    3.4k    list_files,search_text,...  6.1s
rename-symbol         3/5   5.4    12.1k   read_file,edit_file         9.8s

overall  7/10 (70%)  58.3k tokens

Compaction's own summarising call counts too: it trades a call now against smaller inputs later, and that trade cannot be judged with half of it hidden.

Tokens only, never money

Converting to a currency needs a model-to-price table, and that table goes stale silently when a Provider changes its rates — producing a figure that looks precise and is wrong. That is worse than no figure, because nobody questions a number with a decimal point in it. Token counts are what the Provider itself reported and do not expire.

A missing figure stays missing

usage is undefined rather than zero when a Provider reports nothing, and a total containing such a call is flagged incomplete. Zeroes would be indistinguishable from a genuinely free call and would quietly understate every total containing them.

Compatibility note

Streamed Chat Completions only report usage when asked, so stream_options: { include_usage: true } is now sent. A self-hosted OpenAI-compatible endpoint predating that field may reject the request; --no-stream is the documented fallback. This is the only change here that can affect an existing user.

A bug only running it could find

Every turn added the context manager's usage, which is undefined when no compaction was needed — so every real CLI run was flagged incomplete, since the CLI always has a context manager. The unit test asserting complete: true used an agent with no context manager, so it never reached that line. Caught by watching agent_end.usage.complete in a live end-to-end run. Not spending anything and not reporting anything are different, and only the second is a gap; the test that would have caught it is now there.

Verification

  • npm run release:check passes: type-check, 379 tests, build, npm pack --dry-run.
  • The compaction-cost test was verified in reverse: removing that accumulation makes it fail.
  • End to end against a stub that reports usage: non-streaming and streaming both carry it through, including cachedInputTokens, into the eval table and the JSON report.

🤖 Generated with Claude Code

https://claude.ai/code/session_01FFMvrN7qqBmVMNX26XtRDy


Generated by Claude Code

Stage 11 made "can this agent do the job" answerable, but the report had
no cost in it: pass rate, turns, wall time. A prompt change that lifts
the pass rate from 70% to 75% while tripling the tokens is usually a bad
trade, and it looked like a pure win. Providers return usage on every
call and chivgent was throwing it away.

Usage now flows from the Provider response to the run: per turn on
message_end, per run on agent_end and the run result, per attempt in the
eval report, and cumulatively behind /session. Compaction's own
summarising call counts too, because it trades a call now against
smaller inputs later and that trade cannot be judged with half of it
hidden.

Tokens only, never money. Converting to a currency needs a
model-to-price table, and that table goes stale silently when a Provider
changes its rates, producing a figure that looks precise and is wrong.
That is worse than no figure, because nobody questions a number with a
decimal point in it.

A missing figure stays missing: usage is undefined rather than zero when
a Provider reports nothing, and a total that contains such a call is
flagged incomplete. Streamed Chat Completions only report usage when
asked, so stream_options.include_usage is sent; an endpoint predating
that field may reject it, and --no-stream is the documented fallback.

Running it end to end found a bug the unit tests could not: every turn
added the context manager's usage, which is undefined when no compaction
was needed, so every real CLI run was flagged incomplete. Not spending
anything and not reporting anything are different, and only the second
is a gap. The test that would have caught it — a context manager present
but never triggered — is now there.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01FFMvrN7qqBmVMNX26XtRDy
Copilot AI lite review requested due to automatic review settings September 7, 2026 03:10

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chivopic
chivopic merged commit 1b70216 into main Sep 7, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants