fix(session): fix token usage double-counting w/ anthropic & bedrock due to AI SDK v6 upgrade#19758
Merged
rekram1-node merged 3 commits intoanomalyco:devfrom Mar 29, 2026
Conversation
AI SDK v6 normalized inputTokens to include cached tokens for all providers including Anthropic/Bedrock. The excludesCachedTokens flag assumed the old v5 behavior where Anthropic excluded cache from inputTokens, causing cache tokens to be counted twice in total and inflating cost calculations.
417bb48 to
e6d7eb4
Compare
e-n-0
pushed a commit
to e-n-0/opencode
that referenced
this pull request
Mar 29, 2026
…due to AI SDK v6 upgrade (anomalyco#19758) Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: Aiden Cline <[email protected]>
loocor
pushed a commit
to loocor/opencode
that referenced
this pull request
Mar 30, 2026
…due to AI SDK v6 upgrade (anomalyco#19758) Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: Aiden Cline <[email protected]>
afanty2021
pushed a commit
to afanty2021/opencode
that referenced
this pull request
Mar 30, 2026
…due to AI SDK v6 upgrade (anomalyco#19758) Co-authored-by: Aiden Cline <[email protected]> Co-authored-by: Aiden Cline <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #19757
Type of change
What does this PR do?
AI SDK v6 (merged in v1.3.4 via #18433) normalized
inputTokensto include cached tokens for all providers. Previously Anthropic/Bedrock excluded cache frominputTokens, and theexcludesCachedTokensflag handled this by skipping the cache subtraction for those providers.After the v6 upgrade,
inputTokensincludes cache for Anthropic too, butexcludesCachedTokensstill skipped the subtraction — so cache tokens were counted once inadjustedInputTokensand again when added back for the total. This doubled the reportedtotaland inflatedtokens.input.The fix removes the
excludesCachedTokensprovider check and always subtracts cache frominputTokens, matching v6 semantics where all providers reportinputTokensinclusive of cache.How did you verify your code works?
input ≈ cache.read + cache.write + 1(confirming v6 includes cache in inputTokens)Checklist