Skip to content

fix(transcript): sum usage.iterations to count fallback attempts#170

Merged
k-berger merged 2 commits into
mainfrom
fix_sum_iterations
Jul 15, 2026
Merged

fix(transcript): sum usage.iterations to count fallback attempts#170
k-berger merged 2 commits into
mainfrom
fix_sum_iterations

Conversation

@k-berger

Copy link
Copy Markdown
Contributor

Problem

Token usage reported by the plugin can undercount what Claude Code itself reports (e.g. via /usage). One confirmed cause: when a request is retried on a fallback model (observed in a real transcript as a Fable attempt followed by an Opus fallback under a single requestId), the transcript entry's top-level message.usage only mirrors the final iteration. Every billed attempt is listed in usage.iterations[].

Real example: top-level showed input=5607, output=698, while the summed iterations are input=11214, output=700 — a 50% input undercount for that call. Cache tokens are similarly undercounted.

Fix

ReadTurnUsage now decodes usage.iterations and sums the token fields across iterations when there is more than one. With zero or one iteration (the normal case — the single iteration duplicates the top-level fields), behavior is byte-identical to before. The streaming keep-last-per-requestId dedup is unchanged; streamed duplicates repeat the same iterations array, so each API call is still counted once.

Testing

  • New unit tests: fallback summing (modeled on the real payload), single-iteration parity, streaming dedup of fallback entries, empty-iterations fallback to top-level.
  • Spot-checked against the real fallback transcript (now reports 11214/700) and a fresh single-call conversation (unchanged: 12587/979).
  • go test ./... and go vet clean.

When a request is retried on a fallback model, the transcript's top-level
usage only mirrors the final iteration; every billed attempt is listed in
usage.iterations. Sum the iterations when there is more than one so token
counts (and cost estimates) include the failed attempt as well. Single- or
no-iteration usage objects are unaffected.
@k-berger k-berger requested a review from a team as a code owner July 14, 2026 14:42
@k-berger k-berger merged commit 77c9130 into main Jul 15, 2026
7 checks passed
@k-berger k-berger deleted the fix_sum_iterations branch July 15, 2026 11:11
@github-actions github-actions Bot locked and limited conversation to collaborators Jul 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants