Skip to content

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

Open
pcbeingused33 wants to merge 8 commits into
posit-dev:mainfrom
pcbeingused33:fix/openai-cache-write-tokens
Open

pcbeingused33 wants to merge 8 commits into
posit-dev:mainfrom
pcbeingused33:fix/openai-cache-write-tokens

Conversation

@pcbeingused33

Copy link
Copy Markdown
Contributor

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

pcbeingused33 and others added 2 commits October 21, 2026 11:31
… tokens and cost

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
…e lifetime breakdown

Models that do not bill cache writes (before GPT-5.6) are not charged above the
input rate, and the per-lifetime breakdown is only reported for the models that
are, so the premium is gated on it; the 30-minute count comes from the breakdown.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Comment thread chatlas/_provider_openai.py Resolved
pcbeingused33 and others added 5 commits October 21, 2026 11:44
…a fields

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
openai 3.45 typed cache_write_tokens_details on InputTokensDetails, which
moves it out of model_extra, so the extra-field lookup silently added no
premium on current SDKs (and CI failed). Read the attribute instead: the
typed model on 3.45+, the extra-field dict (exposed as an attribute via
pydantic's extra='allow') on older versions.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the prompt-cache write accounting changes.

No open issues 🎉

What changed in this PR

Corrects prompt-cache write accounting for ChatOpenAI() and ChatAzureOpenAI() so turn input-token totals and cost estimates include the applicable write premium.

Changes:

  • Adds cache-write premiums based on the reported 30-minute and 12-hour lifetime breakdown.
  • Handles both typed SDK objects and plain dictionaries from older SDKs, preserving existing accounting when no breakdown is reported.
  • Adds regression tests and documents the correction in the changelog.
File Description
tests/​test_provider_openai.py Tests write premiums, unchanged fallback behavior, and older SDK dictionary handling.
chatlas/​_provider_openai.py Extracts cache-write counts by lifetime and applies the premium to input tokens.
CHANGELOG.md Documents corrected OpenAI and Azure OpenAI cache-write accounting.

Review effort: Balanced

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants