fix(cost): honor an explicit zero cache-creation cost in batch pricing - #36723
Open
hsusul wants to merge 1 commit into
Open
fix(cost): honor an explicit zero cache-creation cost in batch pricing#36723hsusul wants to merge 1 commit into
hsusul wants to merge 1 commit into
Conversation
batch_cost_calculator resolved the cache-write rate with
`model_info.get("cache_creation_input_token_cost") or input_cost_per_token`,
so a model that prices cache writes at an explicit 0.0 was billed at the
plain input rate because `or` short-circuits on a falsy 0.0. The sibling
cache-read path already reads its rate through calculate_cost_component,
which treats a present 0.0 as a real price. Distinguish a missing key from a
present zero so the two cache buckets price consistently.
Contributor
Greptile SummaryThis PR corrects batch cache-write pricing so an explicit zero rate remains free while a missing rate still falls back to the standard input price
Confidence Score: 5/5The PR appears safe to merge, with the corrected pricing distinction covered by focused regression tests The changed calculation preserves the existing missing-rate fallback while correctly honoring zero as a valid configured price, and the tests exercise both branches directly
|
| Filename | Overview |
|---|---|
| litellm/cost_calculator.py | Correctly distinguishes an explicit zero cache-creation rate from an absent rate in batch pricing |
| tests/test_litellm/test_cost_calculator.py | Adds network-independent regression coverage for explicit-zero and missing-rate behavior; remaining changes are formatting-only |
Reviews (1): Last reviewed commit: "fix(cost): honor an explicit zero cache-..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
0.0is billed at the plain input rate in batch cost, not at 0batch_cost_calculatorresolved the cache-write rate withmodel_info.get("cache_creation_input_token_cost") or input_cost_per_token, and theorshort-circuits on a falsy0.0, so a real zero rate looks like a missing key and falls through to the input rateHow it solves it:
0.0) and only fall back toinput_cost_per_tokenwhen the key is absent (None)calculate_cost_componentand treats a present0.0as a real priceUser Flow
A user runs a batch job on a model whose config sets
cache_creation_input_token_cost: 0whileinput_cost_per_tokenis nonzero, and the request writes prompt-cache tokens. Before this change every cache-write token is billed atinput_cost_per_token / 2; after it, those tokens are billed at0Relevant issues
Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)Screenshots / Proof of Fix
batch_cost_calculatoris pure given itsusageandmodel_info, so the before/after is visible without a network call. A model with free cache writes and a nonzero input rate, writing 1000 cache tokens:Before:
0.0005(1000 cache-write tokens billed atinput_cost_per_token / 2). After:0.0End-to-end QA a maintainer can run against a live proxy: register a model whose config carries
cache_creation_input_token_cost: 0, submit a batch job that writes prompt-cache tokens, then confirm at http://localhost:4000/ui/?page=logs that the batch's prompt cost charges those cache-write tokens at 0 rather than the input rateType
🐛 Bug Fix
Changes
batch_cost_calculatorinlitellm/cost_calculator.pynow distinguishes a missingcache_creation_input_token_costfrom a present0.0. This mirrors the same distinction the flat (non-batch) Dashscope path already makes, and the fix that #30749 applied to tiered pricingCaveats (if any)
A missing
cache_creation_input_token_coststill falls back toinput_cost_per_token, so nothing changes for the common providers that do not declare a separate cache-write rateQA runbook
Covered by two regression tests in
tests/test_litellm/test_cost_calculator.py: an explicit0.0cache-creation cost bills at 0, while a missing key still falls back to the input rate. The zero-honoring test fails on the current code and passes after the fixFinal Attestation