fix(usage): count Claude advisor calls under the advisor model#24140

open2
main
GitHub fork: fix/24129-claude-advisor-usage
opened6d agobyFfixfon
View on GitHubTwo-way sync enabledPR last imported 3d ago

Problem

Usage ignores Claude advisor calls because their tokens appear only in message.usage.iterations. With the transcript from #24129, the Usage page shows only the executor's $0.13 and drops the advisor's $0.75.

Change

Keep the executor record unchanged and emit one usage record per valid advisor_message iteration under its own model. Separate stable dedupe keys prevent repeated content-block lines from counting advisor calls twice. Advance the scan cache to v10 and reparse older Claude entries so existing transcripts pick up advisor usage.

Scope and approval

Fixes https://github.com/pingdotgg/t3code/issues/24129

Triaged bug via the automated issue flow. GitCafe: #24129.

Reuses the existing contributor fix. Changes are limited to the Claude usage parser, cache invalidation, and focused tests. Pricing, client rendering, settings, and contracts use their existing paths.

Verification

  • Regression test fails on current main with the executor record matching and the advisor record missing.
  • Existing PR snapshot: 56 focused tests passed. Applied unchanged to current main: 57 focused tests passed across claudeUsage.test.ts, usageScanCache.test.ts, and UsageService.test.ts.
  • Server typecheck passed. Scoped lint reported one existing polling-loop warning outside this diff.
  • Independent reviews checked buffered/projected parsing, multiple advisors, repeated-line and cross-file dedupe, v9 reparsing, deleted-transcript history, and v10 cache round trips.
  • Real web app, isolated transcript home, Claude Code filter, Cost, 30 days: main shows only Opus $0.13; the existing fix shows Fable $0.75 plus unchanged Opus $0.13, total $0.88. No fresh provider call was required to exercise this transcript reader.
  • Exact red-test failure and evidence index.
  • Server-only proof matrix is empty: no render paths changed. Proof matrix and independent runtime checks, before/after runtime transcript, sanitized reporter fixture.
  • Runtime environment: Linux fs-39; Chromium, 1280 × 800 CSS pixels, DPR 2, light theme, no device emulation. Desktop and mobile were not separately launched; the change is in the shared server transcript reader.

Original implementation: Claude Sonnet 5.5 via T3 Code / Claude Code. Triage and verification: GPT-6.1 and Claude Opus 5.5 via T3 Code / Codex and Claude Code harnesses.

Checks

20 successful · 8 skipped
All checks passed
82d2a8c

Added 1 commit

  • fix(usage): count Claude advisor calls under the advisor model7d5880c
Synced from GitHub

Approvability

Verdict: Needs Human Approval

Macroscope's review found this PR requires human approval — This focused fix changes production usage metering by adding advisor-model token records and reparsing cached Claude history, which can alter displayed totals and calculated costs. The implementation is localized and tested, but the metering impact warrants human review.

You can add or adjust custom eligibility rules. Learn more.

Synced from GitHub

Review in Change Stack →

No actionable comments were generated in the recent review.

External image Recent review info
External image Run configuration
External image Commits

Reviewing files that changed from the base of the PR and between 7d5880c5c666da50dc51559cbf5b1b18f45cf19b and 82d2a8c468b28fba0935edf29220e13288b55056.

External image Files selected for processing: 3
  • apps/server/src/provider/Drivers/claudeUsage.test.ts
  • apps/server/src/usage/UsageService.test.ts
  • apps/server/src/usage/usageScanCache.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 7 remain after this review.

Walkthrough

Claude usage parsing now returns arrays of usage records and includes valid advisor iterations as separate records. The scan cache advances to version 10 and resets older Claude entries so they can be reparsed.

Changes: Claude usage parsing and cache

Layer / File(s)Summary
Parse executor and advisor usage
apps/server/src/provider/Drivers/claudeUsage.ts, apps/server/src/provider/Drivers/claudeUsage.test.ts
parseClaudeLine returns an array. It retains the executor record and adds a record for each valid advisor iteration, using that iteration’s model and token counts. Tests cover dedupe keys, parser paths, and invalid advisor entries.
Invalidate older Claude cache entries
apps/server/src/usage/usageScanCache.ts, apps/server/src/usage/usageScanCache.test.ts, apps/server/src/usage/UsageService.test.ts
The cache version and filename advance to v10. Older Claude entries are reset for reparsing, and tests cover the reset behavior and updated filename.

Priority: External image Normal

Estimated code review effort: 3 (Moderate) | ~25 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: pixpmusic, maria-rcks

Merge Risk: External image Minimal · up to 82d2a

Advisor usage is counted separately at the advisor model’s rates, and existing transcripts are rescanned without duplicating cached executor usage. No actionable merge risk was established.

Pre-merge checks: Passed 4
Check nameStatusExplanation
Title checkPassedThe title clearly and concisely describes the primary change: counting Claude advisor calls under the advisor model.
Description checkPassedThe description includes all required sections. It explains the missing advisor usage, the parser and cache changes, the linked issue, focused verification results, runtime checks, limitations, and th…
Linked Issues checkPassedThe changes satisfy issue #24129. parseClaudeLine and parseClaudeRecord keep the executor record and add one record for each valid non-synthetic advisor_message iteration. Each advisor record us…
Out of Scope Changes checkPassedThe changes stay within issue #24129. They modify the Claude usage parser, focused parser tests, scan-cache invalidation, and related cache tests. These changes directly support advisor usage accounti…
Full details: Description check

Explanation

The description includes all required sections. It explains the missing advisor usage, the parser and cache changes, the linked issue, focused verification results, runtime checks, limitations, and the models and harnesses used. It does not include explicit maintainer approval, but it provides a focused-bug scope justification and is otherwise complete.

Full details: Linked Issues check

Explanation

The changes satisfy issue #24129. parseClaudeLine and parseClaudeRecord keep the executor record and add one record for each valid non-synthetic advisor_message iteration. Each advisor record uses the iteration model and token counts, while the executor keeps top-level usage. Advisor dedupe keys differ from executor keys and remain stable across repeated content blocks. The model field remains available to the existing model-rate pricing path. Scan cache version 10 invalidates Claude entries from older versions, and focused tests cover parsing, deduplication, and cache invalidation.

Full details: Out of Scope Changes check

Explanation

The changes stay within issue #24129. They modify the Claude usage parser, focused parser tests, scan-cache invalidation, and related cache tests. These changes directly support advisor usage accounting and rescanning of existing transcripts. No unrelated UI, settings, pricing logic, or contract changes are present in the reviewed change summary.

Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

Added 1 commit

  • Merge origin/main into fix/24129-claude-advisor-usage82d2a8c