fix(dashscope): align implicit cache pricing reports

This commit is contained in:
liming 2026-09-14 12:33:37 +08:00
parent cfb665c21a
commit 4faa0359cb
3 changed files with 81 additions and 6 deletions

View file

@ -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)

View file

@ -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"),

View file

@ -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"),