Skip to content

fix(llm): propagate token usage through review/scan/stream paths#323

Merged
ajianaz merged 1 commit into
developfrom
fix/token-usage-tracking-and-hardcode-cleanup
Jun 21, 2026
Merged

fix(llm): propagate token usage through review/scan/stream paths#323
ajianaz merged 1 commit into
developfrom
fix/token-usage-tracking-and-hardcode-cleanup

Conversation

@ajianaz

@ajianaz ajianaz commented Jun 21, 2026

Copy link
Copy Markdown
Collaborator

Summary

Fixes 3 bugs where tokens_used was always None or partial, plus hardcode cleanup.

Bugs Fixed

# Bug Severity
1 tokens_used always None — parse functions hardcoded Ok((..., None)), discarding API Usage 🔴 Critical
2 cora review --stream ignored usage field in SSE chunks 🟠 Major
3 cora scan multi-batch overwrote (not accumulated) token counts 🟡 Minor

Changes

  • chat_completion() returns (String, Option<Usage>) tuple
  • chat_completion_stream() sends stream_options: {include_usage: true} + parses usage from final SSE chunk
  • parse_review_response / parse_scan_response accept Option<&Usage> and convert via usage_to_token_usage()
  • Scan multi-batch token accumulation fixed
  • Magic numbers (20, 60_000) extracted to named constants
  • Usage struct accepts camelCase aliases + handles total-only providers

Test Plan

  • All 570 unit tests pass (+6 new regression tests)
  • All 16 CLI tests pass
  • All 6 config tests pass
  • cargo clippy --all-targets — zero warnings
  • Pre-commit cora review passed clean

Closes #322

Three bugs caused tokens_used to always be None or partial:

1. parse_review_response/parse_scan_response hardcoded Ok((..., None)),
   discarding the Usage object from the API response. Fixed by threading
   Option<&Usage> through both functions and converting via
   usage_to_token_usage().

2. chat_completion_stream only collected delta.content, ignoring the
   usage field in the final SSE chunk. Now sends stream_options.include_usage
   and parses usage from top-level or choices[0].delta.usage.

3. scan multi-batch total_tokens was overwritten per batch instead of
   accumulated. Fixed with proper accumulation of input/output/cost.

Also:
- Extracted magic numbers (20, 60_000) to named constants
- Usage struct now accepts camelCase aliases (promptTokens etc)
- chat_completion returns (String, Option<Usage>) tuple
- Added 4 regression tests for token usage threading
- Bumped version 0.6.1 → 0.6.2

All 590 tests pass (568 unit + 16 CLI + 6 config).
@ajianaz ajianaz force-pushed the fix/token-usage-tracking-and-hardcode-cleanup branch from 9a29e6f to 24999b9 Compare June 21, 2026 09:58
@ajianaz ajianaz merged commit 99cf877 into develop Jun 21, 2026
10 checks passed
@ajianaz ajianaz deleted the fix/token-usage-tracking-and-hardcode-cleanup branch June 21, 2026 10:04
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.

1 participant