Skip to content

fix: charge 12h OpenAI Responses cache writes at the 12h rate - #47800

Closed
krrish-berri-2 wants to merge 2 commits into
mainfrom
litellm_openai_12h_cache_write_cost
Closed

krrish-berri-2 wants to merge 2 commits into
mainfrom
litellm_openai_12h_cache_write_cost

Conversation

@krrish-berri-2

Copy link
Copy Markdown
Contributor

OpenAI now lets Responses API callers keep a prompt cache for 12 hours with prompt_cache_options={"ttl": "12h"}, and bills those cache writes at 2.5x the input rate instead of the 1.25x used for 30 minute writes. LiteLLM already passes the setting through, but it priced every cache write at the 30 minute rate, so spend logs and budgets showed about half the real cost of a 12h write. This PR prices 12h writes at the right rate for the gpt-5.6 and gpt-6 models

Details

TLDR

Problem this solves:

  • 12h OpenAI cache writes were billed at the 30m rate
  • Spend and budgets undercounted those writes by about 2x

How it solves it:

  • Map OpenAI's 30m and 12h write counts onto existing cache-write buckets
  • Add the 2.5x 12h write rate to gpt-5.6 and gpt-6 entries

User Flow

Before: a developer caching a long prompt for 12h sees half the real write cost in LiteLLM spend

  1. They call litellm.responses(model="openai/gpt-5.6-luna", ..., prompt_cache_options={"ttl": "12h"}) with a ~4.2k token prefix
  2. OpenAI reports cache_write_tokens_details.ttl_12h_tokens: 4188 in the usage
  3. LiteLLM logs the call at $0.00105, the price of a 30m write

After: the same call is logged at what OpenAI actually charges

  1. They make the same litellm.responses call with prompt_cache_options={"ttl": "12h"}
  2. OpenAI reports the same ttl_12h_tokens: 4188
  3. LiteLLM logs the call at $0.00210, the 2.5x write rate

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Real OpenAI calls through the LiteLLM SDK with LITELLM_LOCAL_MODEL_COST_MAP=True, using a fresh ~4.2k token prefix per run so call 1 is a cache write and call 2 is a cache read. The script sends the same prefix twice to litellm.responses(model="openai/gpt-5.6-luna", input=..., max_output_tokens=16, prompt_cache_options={"ttl": "12h"}) and prints response._hidden_params["response_cost"] and the usage, then repeats with litellm.completion

gpt-5.6-luna input is $0.0000002 per token, so the 12h write rate is $0.0000005. Expected cost for call 1 is 4188 x $0.0000005 plus 3 uncached input tokens and 5 output tokens, which is $0.0021006. The Before number matches 4188 x $0.00000025, the 30m rate

Before (159b5b2)

Responses API, 12h cache

  1. Call 1 returns cost= 0.0010536 with cache_write_tokens: 4188 and cache_write_tokens_details: {"ttl_12h_tokens": 4188, "ttl_30m_tokens": 0}
  2. Call 2 returns cost= 9.036e-05 with cached_tokens: 4188

Chat Completions, 12h cache

  1. Both calls fail with litellm.BadRequestError: OpenAIException - Invalid value: '12h'. Supported values are: '30m'.

After (5e4a2c5)

Responses API, 12h cache

  1. Call 1 returns cost= 0.0021006 with cache_write_tokens: 4188 and cache_write_tokens_details: {"ttl_30m_tokens": 0, "ttl_12h_tokens": 4188}
  2. Call 2 returns cost= 9.036e-05 with cached_tokens: 4188

Chat Completions, 12h cache

  1. Both calls fail with litellm.BadRequestError: OpenAIException - Invalid value: '12h'. Supported values are: '30m'.

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Chat Completions still rejects 12h, which is OpenAI's behavior, not ours
  • Only OpenAI gpt-5.6 and gpt-6 entries got the 12h rate
    • Azure and other hosts of these models are unchanged until their pricing is confirmed
  • 12h writes reuse the existing ephemeral_1h_input_tokens bucket and cache_creation_input_token_cost_above_1hr key used for Anthropic 1h writes

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

Link to Devin session: https://app.devin.ai/sessions/26f9a910163d4179821fe41e6714db9a
Open in Devin Desktop: https://app.devin.ai/desktop/session/26f9a910163d4179821fe41e6714db9a?variant=devin
Requested by: @krrish-berri-2

krrish-berri and others added 2 commits October 21, 2026 05:45
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

CLAassistant commented Oct 21, 2026 •

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
0 out of 2 committers have signed the CLA.

❌ devin-ai-integration[bot]
❌ krrish-berri
You have signed the CLA already but the status is still pending? Let us recheck it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 21, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-21T06:25:46.767263Z 5e4a2c5 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5e4a2c533c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +36702 to +36703
"cache_creation_input_token_cost_above_1hr": 0.000025,
"cache_creation_input_token_cost_above_1hr_above_272k_tokens": 0.00005,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Price 12-hour writes by service tier

For Fast or Flex/Batch requests, _get_token_base_cost ignores tier suffixes here, charging 12-hour writes at Standard instead of documented tier rates

AGENTS.md reference: AGENTS.md:L71-L71

Useful? React with 👍 / 👎.

Comment on lines +3431 to +3435
"cached_tokens_details": (
cached_tokens_details if isinstance(cached_tokens_details, CachedTokensDetails) else None
),
"cache_write_tokens": cache_write_tokens,
**cache_type_extra,

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve cache-write lifetime details in Responses output

When a native Responses result is rebuilt, this dictionary omits cache_write_tokens_details, so callers lose the lifetime breakdown that OpenAI returns in its documented response

Useful? React with 👍 / 👎.

Comment on lines +1384 to +1386
class CacheWriteTokensDetails(BaseLiteLLMOpenAIResponseObject):
ttl_30m_tokens: int | None = None
ttl_12h_tokens: int | None = None

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Allow 12-hour TTLs in the request types

PromptCacheOptions and PromptCacheBreakpoint still allow only "30m", so typed callers cannot express the newly supported "12h" value from the API

AGENTS.md reference: AGENTS.md:L102-L102

Useful? React with 👍 / 👎.

@codspeed

codspeed Bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 34 untouched benchmarks


Comparing litellm_openai_12h_cache_write_cost (5e4a2c5) with main (230f653)

Open in CodSpeed

@codecov

codecov Bot commented Oct 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@mateo-berri

Copy link
Copy Markdown
Contributor

Main already bills OpenAI 12 hour cache writes at their own rate since #48088, so closing this. Reopen if a model still prices them wrong

@devin-ai-integration

Copy link
Copy Markdown
Contributor

❌ Cannot revive Devin session - the session is too old. Please start a new session instead.

View session

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.

4 participants