fix(openai): count cache-write tokens at the cache-write rate in turn tokens and cost #480
All Vouch review confidence checks passed
Residual risk 0.40 = Change Risk (0.72) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.72 · 216 lines diff across 2 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.322
Sensitive Paths
sensitive_paths
0.072
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #480"
general
weight: 0.35
pcbeingused333
ChatOpenAI()computed a turn's input tokens asinput_tokens - cached_tokens, so tokens written to OpenAI's prompt cache were counted, and priced, as ordinary input. GPT-5.6 and later bill cache writes above the input rate: 125% for the default 30-minute lifetime and 250% for the 12-hour one, and caching is on by default, so the cost of a turn with a long prompt was understated.input_tokensalready includes the written tokens (input_tokens = ordinary + cached_tokens + cache_write_tokens), sovalue_tokens()now adds the premium on top: 25% of thettl_30m_tokensand 150% of thettl_12h_tokensreported incache_write_tokens_details, the same way the Anthropic and Bedrock providers fold the write rate into the input count. The premium is only added when that per-lifetime breakdown is present: the guide ties it to the models that takeprompt_cache_options, which are the ones billed for writes, so a response that reportscache_write_tokenswithout the breakdown (models with no write charge) keeps today's count.ChatAzureOpenAI()inherits it. Responses without cache writes are unchanged.Three tests build the usage object by hand: 30-minute writes, 12-hour writes, and no writes (including writes reported without the breakdown). The first two fail on
main. The OpenAI-compatible providers built on Chat Completions (OpenRouter, Portkey, Databricks, ...) are left as they are, since the write rate there depends on the upstream model.This change was made with Claude Code (AI-assisted). The tests use hand-built usage objects; no request was made to the OpenAI API.
🤖 Generated with Claude Code