From 4faa0359cbdcf5be4b6409dc9469ba5df281f501 Mon Sep 17 00:00:00 2001 From: liming Date: Mon, 14 Sep 2026 12:33:37 +0800 Subject: [PATCH] fix(dashscope): align implicit cache pricing reports --- .../litellm_core_utils/llm_cost_calc/utils.py | 8 +++ litellm/llms/dashscope/cost_calculator.py | 22 +++++-- .../test_dashscope_cost_calculator.py | 57 ++++++++++++++++++- 3 files changed, 81 insertions(+), 6 deletions(-) diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index f1f9ff39056..63b234f4bcb 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -1586,6 +1586,14 @@ def calculate_prompt_caching_savings( threshold_is_inclusive=_uses_inclusive_token_thresholds(custom_llm_provider), missing_cache_read_uses_input=True, ) + provider_cache_read_cost: Final = _resolve_cache_read_cost_rate( + custom_llm_provider=custom_llm_provider, + model_info=model_info, + usage=usage, + current_time=billed_at, + ) + if provider_cache_read_cost is not None: + cache_read_cost = provider_cache_read_cost write_rate: Final = cache_creation_cost or prompt_base_cost write_rate_1h: Final = cache_creation_cost_above_1hr or write_rate prompt_tokens_details: Final = parse_prompt_tokens_details(usage) diff --git a/litellm/llms/dashscope/cost_calculator.py b/litellm/llms/dashscope/cost_calculator.py index 9dc6fb76f1e..fd1b8ac9809 100644 --- a/litellm/llms/dashscope/cost_calculator.py +++ b/litellm/llms/dashscope/cost_calculator.py @@ -122,6 +122,20 @@ def _flat_rates( ) +def _tier_cache_read_rate( + model_info: ModelInfo, + tier: dict[str, object], + cache_read_mode: QwenContextCacheMode | None, +) -> float: + if cache_read_mode != "implicit": + return tier_rate(tier, "cache_read_input_token_cost", "input_cost_per_token") + if "implicit_cache_read_input_token_cost" in tier: + return tier_rate(tier, "implicit_cache_read_input_token_cost") + if model_info.get("implicit_cache_read_input_token_cost") is not None: + return _flat_cache_read_rate(model_info, cache_read_mode) + return tier_rate(tier, "cache_read_input_token_cost", "input_cost_per_token") + + def _tier_rates( model_info: ModelInfo, tier: dict[str, object], @@ -133,10 +147,10 @@ def _tier_rates( flat_rates: Final = _flat_rates(model_info, cache_read_mode) tier_declares_output: Final = "output_cost_per_token" in tier tier_declares_reasoning: Final = "output_cost_per_reasoning_token" in tier - cache_read_rate: Final = ( - tier_rate(tier, "implicit_cache_read_input_token_cost") - if cache_read_mode == "implicit" and "implicit_cache_read_input_token_cost" in tier - else tier_rate(tier, "cache_read_input_token_cost", "input_cost_per_token") + cache_read_rate: Final = _tier_cache_read_rate( + model_info=model_info, + tier=tier, + cache_read_mode=cache_read_mode, ) return TokenRates( input_rate=tier_rate(tier, "input_cost_per_token"), diff --git a/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py b/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py index bc42799fb5c..ab07a576b2d 100644 --- a/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py +++ b/tests/test_litellm/llms/dashscope/test_dashscope_cost_calculator.py @@ -18,6 +18,7 @@ import pytest import litellm from litellm.litellm_core_utils.llm_cost_calc.utils import ( _resolve_cache_read_cost_rate, + calculate_prompt_caching_savings, get_token_type_cost_breakdown, ) from litellm.llms.dashscope.cost_calculator import ( @@ -25,6 +26,7 @@ from litellm.llms.dashscope.cost_calculator import ( ) from litellm.types.utils import ( CompletionTokensDetailsWrapper, + ModelInfo, PromptTokensDetailsWrapper, Usage, ) @@ -200,12 +202,63 @@ class TestDashscopeCostCalculator: usage = Usage( prompt_tokens=1000, completion_tokens=0, - prompt_tokens_details={"cached_tokens": 600, "cache_type": "ephemeral"}, + prompt_tokens_details={"cached_tokens": 600}, ) prompt_cost, _ = dashscope_cost_per_token(model="qwen-tiered-cache-mode-test", usage=usage) - assert math.isclose(prompt_cost, (400 * 1e-06) + (600 * 1e-07), rel_tol=1e-10) + assert math.isclose(prompt_cost, (400 * 1e-06) + (600 * 2e-07), rel_tol=1e-10) + + def test_dashscope_tiered_pricing_uses_model_level_implicit_cache_read_rate(self): + litellm.model_cost["dashscope/qwen-tiered-model-implicit-rate-test"] = { + "litellm_provider": "dashscope", + "mode": "chat", + "implicit_cache_read_input_token_cost": 2e-07, + "tiered_pricing": [ + { + "range": [0, 1000], + "input_cost_per_token": 1e-06, + "output_cost_per_token": 4e-06, + "cache_read_input_token_cost": 1e-07, + } + ], + } + usage = Usage( + prompt_tokens=1000, + completion_tokens=0, + prompt_tokens_details={"cached_tokens": 600}, + ) + + prompt_cost, _ = dashscope_cost_per_token( + model="qwen-tiered-model-implicit-rate-test", + usage=usage, + ) + + assert math.isclose(prompt_cost, (400 * 1e-06) + (600 * 2e-07), rel_tol=1e-10) + + def test_dashscope_prompt_caching_savings_uses_implicit_cache_read_rate(self): + model_info: ModelInfo = { + "key": "qwen-cache-savings-mode-test", + "litellm_provider": "dashscope", + "mode": "chat", + "input_cost_per_token": 1e-06, + "output_cost_per_token": 4e-06, + "cache_read_input_token_cost": 1e-07, + "implicit_cache_read_input_token_cost": 2e-07, + } + usage = Usage( + prompt_tokens=1000, + completion_tokens=0, + prompt_tokens_details={"cached_tokens": 600}, + ) + + savings = calculate_prompt_caching_savings( + model_info=model_info, + usage=usage, + custom_llm_provider="dashscope", + ) + + assert math.isclose(savings, 600 * (1e-06 - 2e-07), rel_tol=1e-10) @pytest.mark.parametrize( ("cache_type", "expected_cache_rate"),