Skip to content

fix(pricing): add cache_read_input_token_cost to 8 deepseek models - #28853

Closed
silencedoctor wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
silencedoctor:fix-deepseek-cache-read-input-token-cost
Closed

silencedoctor wants to merge 1 commit into
BerriAI:litellm_internal_stagingfrom
silencedoctor:fix-deepseek-cache-read-input-token-cost

Conversation

@silencedoctor

Copy link
Copy Markdown
Contributor

Relevant issues

None.

Pre-Submission checklist

  • I have added testing in tests/test_litellm/ (tests/test_litellm/test_deepseek_cache_read_pricing.py)
  • My PR passes make test-unit on the touched test file (uv run pytest tests/test_litellm/test_deepseek_cache_read_pricing.py — 9 passed)
  • My PR's scope is as isolated as possible — it only adds the missing cache_read_input_token_cost field to 8 model entries plus one regression test

Type

🐛 Bug Fix

Changes

8 DeepSeek / OpenRouter-DeepSeek model entries declared only the legacy input_cost_per_token_cache_hit field but were missing the canonical cache_read_input_token_cost field. The cost calculator only reads the latter, so prompt-cache-hit tokens on these models were silently billed at $0 instead of the cache-read rate.

Root cause

litellm/litellm_core_utils/llm_cost_calc/utils.py resolves the cache-read price via cache_read_input_token_cost only:

```python
cache_read_cost_key = _get_service_tier_cost_key("cache_read_input_token_cost", service_tier)
cache_read_cost = cast(float, _get_cost_per_unit(model_info, cache_read_cost_key))
...
prompt_cost += float(prompt_tokens_details["cache_hit_tokens"]) * cache_read_cost
```

No code path consumes input_cost_per_token_cache_hit for cost calculation — it is only declared on ModelInfoBase (litellm/types/utils.py) and listed in the field-name allowlist test. So for models that have only the legacy key, cache_read_cost resolves to None → 0.0, and cache-hit tokens are billed at $0 (since they are also subtracted from text_tokens so they aren't billed at the full input rate either).

Affected models

Model input_cost_per_token_cache_hit (was) cache_read_input_token_cost (now)
deepseek/deepseek-coder 1.4e-08 1.4e-08
deepseek/deepseek-r1 1.4e-07 1.4e-07
deepseek/deepseek-v3.2 2.8e-08 2.8e-08
openrouter/deepseek/deepseek-chat-v3.1 2e-08 2e-08
openrouter/deepseek/deepseek-v3.2 2.8e-08 2.8e-08
openrouter/deepseek/deepseek-v3.2-exp 2e-08 2e-08
openrouter/deepseek/deepseek-r1 1.4e-07 1.4e-07
openrouter/deepseek/deepseek-r1-0528 1.4e-07 1.4e-07

Each new cache_read_input_token_cost value is identical to the model's existing input_cost_per_token_cache_hit. This matches the double-write convention already used by other DeepSeek entries that are billing correctly today (deepseek/deepseek-chat, deepseek/deepseek-reasoner, deepseek/deepseek-v3, novita/deepseek/deepseek-v3.2, etc.).

The change is applied to both pricing JSONs:

  • model_prices_and_context_window.json
  • litellm/model_prices_and_context_window_backup.json

Tests

tests/test_litellm/test_deepseek_cache_read_pricing.py:

  1. Parametrized check that all 8 models declare cache_read_input_token_cost equal to input_cost_per_token_cache_hit.
  2. End-to-end check that generic_cost_per_token bills cache-hit tokens at the cache-read rate for deepseek/deepseek-r1.

```
$ uv run pytest tests/test_litellm/test_deepseek_cache_read_pricing.py -v
...
9 passed in 0.56s
```

Existing tests/test_litellm/test_cost_calculator.py continues to pass (53 passed).

Follow-up

Out of scope for this PR, but worth a separate discussion: input_cost_per_token_cache_hit appears to be a dead field — it is declared in the type definitions and JSON schema, but no calculator code path reads it. Today this is a footgun for community contributors and AI-assisted PRs that add new model entries by copying nearby DeepSeek configs and only carrying the legacy key over. I'll open a separate issue to discuss whether the field should be (a) wired to the calculator as a fallback with a deprecation warning, or (b) fully removed after a deprecation window. Happy to put up either patch once the maintainers signal which direction they prefer.

@silencedoctor

Copy link
Copy Markdown
Contributor Author

Filed the follow-up deprecation discussion as #28854 (separate from this PR — only mentioning here so it's easy to find).

@greptile-apps

greptile-apps Bot commented May 26, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes a silent billing bug where cache-hit tokens on 8 DeepSeek/OpenRouter-DeepSeek models were charged at $0 because the cost calculator reads cache_read_input_token_cost exclusively, but those model entries only declared the legacy input_cost_per_token_cache_hit field.

  • Adds cache_read_input_token_cost (identical value to the existing input_cost_per_token_cache_hit) to 8 model entries in both model_prices_and_context_window.json and the backup file, following the double-write convention already used by correctly-billing DeepSeek models.
  • Adds a new regression test that verifies both the presence/equality of the two keys and an end-to-end cost calculation for deepseek/deepseek-r1; the test fixture leaks LITELLM_LOCAL_MODEL_COST_MAP and the overwritten litellm.model_cost into the process without teardown.

Confidence Score: 4/5

Safe to merge — the pricing JSON edits are narrow additive changes that restore correct cache-read billing for the 8 affected models.

The data changes are correct and well-tested. The only concern is the test fixture that sets a global env var and overwrites litellm.model_cost without restoring them, which could cause interference if tests run in the same process as other test modules.

tests/test_litellm/test_deepseek_cache_read_pricing.py — the autouse fixture needs yield-based teardown to avoid state leakage.

Important Files Changed

Filename Overview
model_prices_and_context_window.json Adds cache_read_input_token_cost to 8 DeepSeek/OpenRouter-DeepSeek model entries, mirroring their existing input_cost_per_token_cache_hit values. Values are consistent with how other DeepSeek models already declare this field.
litellm/model_prices_and_context_window_backup.json Identical fix applied to the backup pricing file — same 8 models, same values, kept in sync with the primary JSON.
tests/test_litellm/test_deepseek_cache_read_pricing.py New regression test covering all 8 affected models; fixture sets local model cost map but does not teardown, leaving global state modified for any co-located test files in the same pytest process.

Reviews (1): Last reviewed commit: "fix(pricing): add cache_read_input_token..." | Re-trigger Greptile

@codspeed

codspeed Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing silencedoctor:fix-deepseek-cache-read-input-token-cost (6195f53) with litellm_internal_staging (04177e7)

Open in CodSpeed

@codecov

codecov Bot commented May 26, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@hclsys

hclsys commented May 26, 2026

Copy link
Copy Markdown
Contributor

Values look right to me — checked each of the 8 entries and the new cache_read_input_token_cost matches the existing input_cost_per_token_cache_hit in every case (e.g. deepseek/deepseek-coder → both 1.4e-08, deepseek/deepseek-r1 → both 1.4e-07, deepseek/deepseek-v3.2 → both 2.8e-08). So the cache-read cost is consistent with the already-recorded cache-hit cost, which is what you'd want.

One thing worth a note for future maintainers: these two fields now duplicate the same number per model (cache_read_input_token_cost is the canonical/cross-provider field, input_cost_per_token_cache_hit the older deepseek-specific one). Keeping them in sync is correct here; just flagging so a later price update doesn't touch one and miss the other → silent cost drift. Could be worth a one-line comment or a tiny test asserting cache_read == cache_hit for these models (you already added test_deepseek_cache_read_pricing.py, so that'd be a natural home).

Nice that both model_prices_and_context_window.json and the _backup.json were updated together — that pair drifting is a common miss.

@silencedoctor

Copy link
Copy Markdown
Contributor Author

Thanks for the review! 🙏

@silencedoctor
silencedoctor force-pushed the fix-deepseek-cache-read-input-token-cost branch from a0a9775 to ed47bde Compare July 29, 2026 11:41
@silencedoctor
silencedoctor changed the base branch from main to litellm_internal_staging July 29, 2026 12:24
8 deepseek and openrouter/deepseek models declared only the legacy
`input_cost_per_token_cache_hit` field, but the cost calculator
(`litellm.litellm_core_utils.llm_cost_calc.utils._get_token_base_cost`)
only reads `cache_read_input_token_cost`. As a result, prompt-cache-hit
tokens were billed at $0 instead of the cache-read rate for:

- deepseek/deepseek-coder
- deepseek/deepseek-r1
- deepseek/deepseek-v3.2
- openrouter/deepseek/deepseek-chat-v3.1
- openrouter/deepseek/deepseek-v3.2
- openrouter/deepseek/deepseek-v3.2-exp
- openrouter/deepseek/deepseek-r1
- openrouter/deepseek/deepseek-r1-0528

This patch adds `cache_read_input_token_cost` with the same value as
the existing legacy field to both pricing JSONs, matching the
convention already used by `deepseek/deepseek-chat`,
`deepseek/deepseek-reasoner`, `deepseek/deepseek-v3`, and others.

Adds a regression test that parametrizes over the 8 models and an
end-to-end check that a cache hit on `deepseek/deepseek-r1` is billed
at the cache-read rate via `generic_cost_per_token`.
@silencedoctor
silencedoctor force-pushed the fix-deepseek-cache-read-input-token-cost branch from ed47bde to 6195f53 Compare July 30, 2026 02:40
@yuneng-berri
yuneng-berri deleted the branch BerriAI:litellm_internal_staging September 13, 2026 04:23
@yuneng-berri yuneng-berri reopened this Sep 13, 2026
devin-ai-integration Bot added a commit that referenced this pull request Sep 19, 2026
…7064

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

Superseded by the rolling registry PR #34941, which re-verified and absorbed the cache_read_input_token_cost additions for the 5 keys that lacked the field on main (the other 3 keys already had it). Closing to keep a single open model-pricing PR.

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