Repository navigation
fix(bedrock): count cacheWriteInputTokens in turn tokens and cost - #478
Open
pcbeingused333 wants to merge 3 commits into
Open
pcbeingused333 wants to merge 3 commits into
pcbeingused333 wants to merge 3 commits into
Conversation
ChatBedrock() sends cache points by default for anthropic.* and amazon.nova* models, but converse_tokens() ignored the cacheWriteInputTokens field of the Converse usage, so cache writes were never counted. Fold them into the input count like ChatAnthropic() does: 1.25x (2x with cache="1h") on Claude models, the regular input rate otherwise. Co-authored-by: Claude Sonnet 5.5 <noreply@anthropic.com>
cpsievert
reviewed
Oct 21, 2026
| cache_write = usage.get("cacheWriteInputTokens", 0) | ||
| return ( | ||
| usage["inputTokens"], | ||
| usage["inputTokens"] + int(cache_write * cache_write_mult), |
Collaborator
There was a problem hiding this comment.
Do other providers need to incorporate cache write tokens?
Contributor
Author
There was a problem hiding this comment.
Good question. Anthropic already folds cache writes in. Checking the rest: OpenAI does need it, since GPT-5.6 and later bill Responses cache writes at 1.25x (2.5x for the 12h lifetime) and report them as input_tokens_details.cache_write_tokens, which is included in input_tokens. That is in a separate PR, #480, so this one stays Bedrock-only. Google reports no write count, Ollama is local, and the OpenAI-compatible Chat Completions providers (OpenRouter etc.) expose the field but the write rate depends on the upstream model, so I left those out.
# Conflicts: # CHANGELOG.md
This branch has not been deployed
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.
ChatBedrock()appends a Converse cache point by default foranthropic.*andamazon.nova*models, butconverse_tokens()read onlyinputTokens,outputTokensandcacheReadInputTokens. ThecacheWriteInputTokensfield of the Converseusagewas dropped, so turns that wrote to the cache reported fewer input tokens, and a lower cost, than they used.ChatAnthropic()already folds cache creation into the input count (1.25x for the 5 minute cache, 2x forcache="1h").This change does the same for the Converse provider:
anthropic.*, with or without a cross-region prefix):inputTokens + cacheWriteInputTokens * 1.25, or* 2whencache="1h".cacheWriteInputTokensfield counts as 0, as before.Tests (
tests/test_provider_bedrock_converse.py,TestResponseParsing) build Converse responses withcacheReadInputTokensandcacheWriteInputTokensand check the resulting(input, output, cached)tuple for a 5m Claude model, a 1h Claude model, a Nova model and a response without the field. Three of them fail onmain(the write tokens are ignored) and pass with the change. The bedrock and token test files pass locally; nothing was run against the real Bedrock API. A CHANGELOG entry is included.Open question: if you would rather only count the tokens and leave the per-model weighting out, the multiplier branch is a few lines and easy to drop.
This change was made with Claude Code.
🤖 Generated with Claude Code