mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-17 09:34:48 +00:00
fix(cost): keep cache totals aligned with resolved rates
This commit is contained in:
parent
b9db42ac1c
commit
bfca7aa236
3 changed files with 40 additions and 7 deletions
|
|
@ -60,9 +60,6 @@ from litellm.llms.databricks.cost_calculator import (
|
|||
from litellm.llms.deepseek.cost_calculator import (
|
||||
cost_per_token as deepseek_cost_per_token,
|
||||
)
|
||||
from litellm.llms.fireworks_ai.cost_calculator import (
|
||||
cost_per_token as fireworks_ai_cost_per_token,
|
||||
)
|
||||
from litellm.llms.gemini.cost_calculator import cost_per_token as gemini_cost_per_token
|
||||
from litellm.llms.lemonade.cost_calculator import (
|
||||
cost_per_token as lemonade_cost_per_token,
|
||||
|
|
@ -76,9 +73,6 @@ from litellm.llms.openai.cost_calculation import (
|
|||
from litellm.llms.openai.cost_calculation import (
|
||||
cost_per_token as openai_cost_per_token,
|
||||
)
|
||||
from litellm.llms.perplexity.cost_calculator import (
|
||||
cost_per_token as perplexity_cost_per_token,
|
||||
)
|
||||
from litellm.llms.tencent.cost_calculator import (
|
||||
cost_per_token as tencent_cost_per_token,
|
||||
)
|
||||
|
|
@ -647,6 +641,10 @@ def cost_per_token(
|
|||
elif custom_llm_provider == "databricks":
|
||||
return databricks_cost_per_token(model=model, usage=usage_block)
|
||||
elif custom_llm_provider == "fireworks_ai":
|
||||
from litellm.llms.fireworks_ai.cost_calculator import (
|
||||
cost_per_token as fireworks_ai_cost_per_token,
|
||||
)
|
||||
|
||||
return fireworks_ai_cost_per_token(model=model, usage=usage_block)
|
||||
elif custom_llm_provider == "azure":
|
||||
return azure_openai_cost_per_token(
|
||||
|
|
@ -662,6 +660,10 @@ def cost_per_token(
|
|||
elif custom_llm_provider == "tencent":
|
||||
return tencent_cost_per_token(model=model, usage=usage_block)
|
||||
elif custom_llm_provider == "perplexity":
|
||||
from litellm.llms.perplexity.cost_calculator import (
|
||||
cost_per_token as perplexity_cost_per_token,
|
||||
)
|
||||
|
||||
return perplexity_cost_per_token(model=model, usage=usage_block)
|
||||
elif custom_llm_provider == "xai":
|
||||
return xai_cost_per_token(model=model, usage=usage_block)
|
||||
|
|
|
|||
|
|
@ -1283,12 +1283,19 @@ def generic_cost_per_token(
|
|||
current_time=billing_time,
|
||||
threshold_is_inclusive=_uses_inclusive_token_thresholds(custom_llm_provider),
|
||||
)
|
||||
resolved_cache_read_cost: Final = _resolve_cache_read_cost_rate(
|
||||
model_info=model_info,
|
||||
usage=usage,
|
||||
current_time=billing_time,
|
||||
service_tier=service_tier,
|
||||
)
|
||||
effective_cache_read_cost: Final = cache_read_cost if resolved_cache_read_cost is None else resolved_cache_read_cost
|
||||
|
||||
prompt_cost = _calculate_input_cost(
|
||||
prompt_tokens_details=prompt_tokens_details,
|
||||
model_info=model_info,
|
||||
prompt_base_cost=prompt_base_cost,
|
||||
cache_read_cost=cache_read_cost,
|
||||
cache_read_cost=effective_cache_read_cost,
|
||||
cache_creation_cost=cache_creation_cost,
|
||||
cache_creation_cost_above_1hr=cache_creation_cost_above_1hr,
|
||||
service_tier=service_tier,
|
||||
|
|
|
|||
|
|
@ -19,6 +19,7 @@ import litellm
|
|||
from litellm.litellm_core_utils.llm_cost_calc.utils import (
|
||||
_resolve_cache_read_cost_rate,
|
||||
calculate_prompt_caching_savings,
|
||||
generic_cost_per_token,
|
||||
get_token_type_cost_breakdown,
|
||||
)
|
||||
from litellm.llms.dashscope.cost_calculator import (
|
||||
|
|
@ -404,6 +405,29 @@ class TestDashscopeCostCalculator:
|
|||
|
||||
assert math.isclose(savings, 600 * (1e-06 - 2e-07), rel_tol=1e-10)
|
||||
|
||||
def test_openai_compatible_qwen_total_uses_implicit_cache_read_rate(self):
|
||||
model_info: ModelInfo = {
|
||||
"key": "qwen-generic-cache-mode-test",
|
||||
"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},
|
||||
)
|
||||
|
||||
prompt_cost, _ = generic_cost_per_token(
|
||||
model="custom-qwen",
|
||||
usage=usage,
|
||||
custom_llm_provider="openai",
|
||||
model_info=model_info,
|
||||
)
|
||||
|
||||
assert math.isclose(prompt_cost, (400 * 1e-06) + (600 * 2e-07), rel_tol=1e-10)
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
("cache_type", "expected_cache_rate"),
|
||||
[("ephemeral", 1e-07), (None, 2e-07)],
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue