← Back to Pull requests

fix(openai): count cache-write tokens at the cache-write rate in turn tokens and cost #480

Open
unknown opened this pull request with 216 lines diff across 2 files
View on GitHub
unknown unknown commented on Oct 21, 2026 15:35 UTC
Author

ChatOpenAI() computed a turn's input tokens as input_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_tokens already includes the written tokens (input_tokens = ordinary + cached_tokens + cache_write_tokens), so value_tokens() now adds the premium on top: 25% of the ttl_30m_tokens and 150% of the ttl_12h_tokens reported in cache_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 take prompt_cache_options, which are the ones billed for writes, so a response that reports cache_write_tokens without 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

Diff: 216 lines (+162 / -54) across 2 files Reviewer: pcbeingused333 Review duration: 51791m
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
0.72
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.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
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.8% (HIGH) · Higher = larger blast radius
71.8%
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.7% · Moderate risk
39.7%