Skip to content

fix: stabilize token/cache accounting across providers and routed Roo metadata#11448

Merged
hannesrudolph merged 4 commits intomainfrom
fix/token-counting-and-cache
Feb 12, 2026
Merged

fix: stabilize token/cache accounting across providers and routed Roo metadata#11448
hannesrudolph merged 4 commits intomainfrom
fix/token-counting-and-cache

Conversation

@hannesrudolph
Copy link
Collaborator

@hannesrudolph hannesrudolph commented Feb 12, 2026

Summary

  • add provider-computed total token fields and use them in Task aggregation to avoid protocol misclassification double-counting
  • fix AI SDK v6 cache/reasoning extraction paths across providers (usage.cachedInputTokens, inputTokenDetails, outputTokenDetails)
  • fix Roo routed-metadata cache extraction (Roo -> Anthropic -> Gateway -> AI SDK usage fallback chain)

What changed

  • usage model: added/used total input/output token semantics at provider boundary
  • provider updates: normalized cache/reasoning token extraction for AI SDK v6
  • Roo handler: robust cache source priority for routed responses
  • tests: expanded provider test coverage including Roo routed metadata cases

Validation

  • branch test/lint runs completed during implementation
  • targeted Roo provider suite passing (44/44)

Notes

  • excludes progress.txt from commits

Each provider now computes totalInputTokens/totalOutputTokens directly
in their usage chunks, since each provider knows its own protocol
semantics. Task.ts uses these provider-computed values instead of
re-deriving them through calculateApiCostAnthropic/calculateApiCostOpenAI
which made incorrect assumptions about whether inputTokens includes
cache tokens.

Root cause: For Bedrock, the AI SDK normalizes inputTokens to include
cache tokens (OpenAI convention), but getApiProtocol('bedrock') returned
'anthropic', causing calculateApiCostAnthropic() to add cache tokens
a second time — doubling the displayed input token count.
AI SDK v6 exposes cachedInputTokens at usage.cachedInputTokens (top-level)
and usage.inputTokenDetails.cacheReadTokens (structured), not at the
legacy usage.details.cachedInputTokens path. Similarly for reasoning tokens.

Updates 18 providers with proper v6 fallback chains and adds 30 new tests
covering the v6 cache and reasoning token extraction paths.

Providers with providerMetadata (anthropic, bedrock, minimax) retain it
as highest priority, with v6 paths as secondary fallbacks.
@dosubot dosubot bot added size:XXL This PR changes 1000+ lines, ignoring generated files. bug Something isn't working labels Feb 12, 2026
Copy link
Collaborator Author

@hannesrudolph hannesrudolph left a comment

Choose a reason for hiding this comment

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

Architectural direction is sound. Moving total token computation to the provider boundary eliminates the protocol misclassification double-counting problem, and the AI SDK v6 field path normalization is thorough with good fallback chains.

P1 (1 issue): cacheWriteTokens appears in the cacheRead fallback chain in roo.ts. Looks like a copy-paste error that would misreport write tokens as reads.

P2 (3 issues): Mixed indentation in roo.spec.ts from incomplete restructuring. totalCost and telemetry cost can now be undefined where they previously had calculated fallbacks. Worth confirming this is intentional.

P3 (2 observations): Local types inside createMessage could be extracted to module scope. The expanded usage type signature is now duplicated across ~15 provider files and could benefit from a shared type.

Test coverage is solid (44/44 Roo suite, new v6 field path tests for Bedrock, Gemini, OpenAI Native, OpenAI).

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 12, 2026
@hannesrudolph hannesrudolph merged commit 3965cd9 into main Feb 12, 2026
11 checks passed
@hannesrudolph hannesrudolph deleted the fix/token-counting-and-cache branch February 12, 2026 23:10
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Feb 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working lgtm This PR has been approved by a maintainer size:XXL This PR changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants