Skip to content

fix(parser): preserve Claude reasoning usage without double billing - #590

Open
Kooooojun wants to merge 1 commit into
xiufengsun:mainfrom
Kooooojun:fix/claude-reasoning-usage
Open

fix(parser): preserve Claude reasoning usage without double billing#590
Kooooojun wants to merge 1 commit into
xiufengsun:mainfrom
Kooooojun:fix/claude-reasoning-usage

Conversation

@Kooooojun

@Kooooojun Kooooojun commented Sep 7, 2026

Copy link
Copy Markdown

Summary

Preserve reported reasoning usage from Claude Code / Anthropic-compatible session records instead of always storing zero.

  • Read output_tokens_details.thinking_tokens, falling back to reasoning_tokens when absent (not when explicitly zero).
  • Clamp to a non-negative integer within the inclusive output count.
  • Split reasoning out of queue output_tokens: Claude rows price reasoning separately, so simply adding the detail would double bill it. Keep total tokens and cost unchanged.

Validation

  • Regression reproduced before patch: reported 163 became 0.
  • 17 focused tests pass: new regression, Claude categorizer, WSL dual-install tests.
  • New numeric-only fixture covers ten detail variants, token conservation, cost conservation and a second incremental parse with no queue changes.
  • validate:guardrails, validate:versions, git diff --check pass.
  • Full npm test is not green locally: Copilot SQLite-sidecar and sync-store tests failed (unmodified areas); full results will be appended when the run finishes. Initial run before installing dependencies also failed on missing packages; that is not claimed as validation.
  • Self-reviewed diff for missing versus explicit zero, input/cache conservation, and billing invariants. Maintainer review is still required.

Scope / limitations

No conversation content or credentials included. No release, version bump, deployment, or local service changes; release coordination belongs to the maintainer.

This fixes newly parsed queue usage, not historical backfill, session discovery, or context-category estimation. Existing logs with the detail omitted cannot reconstruct it. Upstream proxies must preserve the field first: router-for-me/CLIProxyAPI#5587.

Summary by CodeRabbit

  • Bug Fixes
    • Claude usage reporting now separately accounts for reasoning tokens while preventing double billing.
    • Output token counts and cost calculations are more accurate when reasoning details are provided.
    • Incremental usage parsing remains consistent across repeated processing.

@coderabbitai

coderabbitai Bot commented Sep 7, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Team

Run ID: 9be0e9a9-5d5e-4e45-931e-5f688694b0df

📥 Commits

Reviewing files that changed from the base of the PR and between 3367ddd and 04141cb.

📒 Files selected for processing (2)
  • src/lib/rollout.js
  • test/claude-reasoning-usage.test.js

Included review availability: Your plan provides up to 4 included reviews per hour; 3 remain after this review.


📝 Walkthrough

Walkthrough

Claude usage normalization now separates reasoning tokens from output tokens, caps reasoning at total output, and preserves total token and cost calculations. Tests cover varied usage details and confirm idempotent incremental parsing.

Changes

Claude reasoning usage

Layer / File(s) Summary
Reasoning token normalization and validation
src/lib/rollout.js, test/claude-reasoning-usage.test.js
normalizeClaudeUsage reads Claude reasoning details, caps the value, subtracts it from output_tokens, and reports reasoning_output_tokens. Tests validate supported and invalid inputs, token totals, cost calculations, and repeated parsing.

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

Merge Risk: ⚪ Minimal · up to 04141

Claude session parsing now preserves reasoning-token usage while keeping total token counts and cost stable. The covered parsing and billing behaviors are ready to merge.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main change: preserving Claude reasoning usage while preventing double billing in the parser.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff. Analyzed 0 functions across 1…
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@Kooooojun

Copy link
Copy Markdown
Author

Full-suite baseline comparison completed with the same installed dependencies: unmodified base 3367ddd has 2,632 passed / 14 failed / 7 skipped (2,653 total); patched run has 2,633 passed / 14 failed / 7 skipped (2,654 total). The sets of failing test names match exactly; no newly failing tests observed. Final focused run (including 10 numeric fixture variants) passes all 17 tests. Self-review checked token/cost conservation, explicit-zero precedence, malformed values, and incremental idempotency. Maintainer review is pending; no service was changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant