← Back to Pull requests

fix(bedrock): count cacheWriteInputTokens in turn tokens and cost #478

Open
unknown opened this pull request with 199 lines diff across 2 files
View on GitHub
unknown unknown commented on Oct 21, 2026 12:28 UTC
Author

ChatBedrock() appends a Converse cache point by default for anthropic. and amazon.nova models, but converse_tokens() read only inputTokens, outputTokens and cacheReadInputTokens. The cacheWriteInputTokens field of the Converse usage was 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 for cache="1h").

This change does the same for the Converse provider:

  • Claude models (anthropic., with or without a cross-region prefix): inputTokens + cacheWriteInputTokens 1.25, or * 2 when cache="1h".

  • Other models: cache writes are counted at the regular input rate (multiplier 1).

  • A missing cacheWriteInputTokens field counts as 0, as before.
  • Tests (tests/test_provider_bedrock_converse.py, TestResponseParsing) build Converse responses with cacheReadInputTokens and cacheWriteInputTokens and 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 on main (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

    Diff: 199 lines (+149 / -50) across 2 files Reviewer: pcbeingused333 Review duration: 51977m
    All Vouch review confidence checks passed
    Residual risk 0.39 = Change Risk (0.71) × (1 − Review Confidence (0.45)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.71 · 199 lines diff across 2 files
    0.71
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Shallow Review Depth shallow_review_depth
    0.344
    Diff Size diff_size
    0.317
    Sensitive Paths sensitive_paths
    0.071
    Ai Authored ai_authored
    0.050
    Real PR Conversation Timeline
    1 comments
    "Review active on #478"
    general weight: 0.35
    Assignees
    Unassigned
    Labels
    risk: medium confidence: 44.7% reviewed sensitive_module
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 71.3% (HIGH) · Higher = larger blast radius
    71.3%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 39.5% · Moderate risk
    39.5%