Fix Bedrock Converse input_tokens double-subtracting cache tokens#832
Merged
crmne merged 3 commits intoJul 3, 2026
Merged
Conversation
AWS Bedrock reports inputTokens as already non-cached; cacheReadInputTokens and cacheWriteInputTokens are separate buckets, not folded in. Subtracting them (as inclusive providers like OpenAI/Gemini require) understated input and floored it to zero on cache-heavy turns. Pass inputTokens through, as the Anthropic provider already does for the same Claude models. Updates the converse chat spec: the prior test encoded the subtraction on a payload that can't occur (inputTokens already excludes cache per AWS), plus a regression for the floor-to-zero case.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #832 +/- ##
==========================================
- Coverage 82.18% 82.17% -0.02%
==========================================
Files 170 170
Lines 8029 8027 -2
Branches 1347 1347
==========================================
- Hits 6599 6596 -3
- Misses 890 891 +1
Partials 540 540 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #828.
Problem
Converse::Chat#input_tokenssubtractscacheReadInputTokensandcacheWriteInputTokensfrominputTokens. But AWS Bedrock'sinputTokensis already the non-cached count. The cache buckets are reported separately, not folded in. Per the AWS prompt-caching docs:So the subtraction removes tokens that were never there, understating input on every cache hit and flooring it to
0whenever the cached prefix exceeds the fresh input (the common multi-turn case). Any cost or usage tracking onmessage.input_tokensunder-reports.Why pass-through is correct
The
Anthropicprovider already handles the same Claude models correctly:anthropic/chat.rbpassesinput_tokensthrough raw and reads the cache buckets separately. Bedrock serves those same models with the same semantics, so this just brings Converse into line. The same fix landed in LiteLLM (#15292) and was reported in LangSmith (#1858).Change
input_tokensreturnsusage['inputTokens']unchanged.cached_tokensandcache_creation_tokensare still parsed separately, so nothing is lost. Streaming delegates to this method, so both paths are covered.inputTokensalready excludes cache), so its fixture is corrected to a realistic one, and a regression is added for the floor-to-zero case.Verified:
bundle exec rspec spec/ruby_llm/protocols/converse/plusspec/ruby_llm/providers/bedrock_spec.rbgreen (39 examples), rubocop clean.