The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
feat(cost-calculator): compute and store per-type cache costs in CostBreakdown (cache_read_cost, cache_creation_cost)
Ishaan Jaffer781fc6311b5a
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.pyindex 699afba412f..bd5fe65ff2d 100644--- a/litellm/cost_calculator.py+++ b/litellm/cost_calculator.py@@ -966,6 +966,8 @@ def _store_cost_breakdown_in_logging_obj(margin_percent: Optional[float] = None,margin_fixed_amount: Optional[float] = None,margin_total_amount: Optional[float] = None,+ cache_read_cost: Optional[float] = None,+ cache_creation_cost: Optional[float] = None,) -> None:"""Helper function to store cost breakdown in the logging object.@@ -1001,6 +1003,8 @@ def _store_cost_breakdown_in_logging_obj(margin_percent=margin_percent,margin_fixed_amount=margin_fixed_amount,margin_total_amount=margin_total_amount,+ cache_read_cost=cache_read_cost,+ cache_creation_cost=cache_creation_cost,)except Exception as breakdown_error:@@ -1599,6 +1603,20 @@ def completion_cost( # noqa: PLR0915# Store cost breakdown in logging object if availableif litellm_logging_obj is not None:+ _cache_read_cost: Optional[float] = None+ _cache_creation_cost: Optional[float] = None+ if cost_per_token_usage_object is not None:+ _cr = getattr(cost_per_token_usage_object, "cache_read_input_tokens", None) or (cost_per_token_usage_object.model_extra or {}).get("cache_read_input_tokens")+ _cc = getattr(cost_per_token_usage_object, "cache_creation_input_tokens", None) or (cost_per_token_usage_object.model_extra or {}).get("cache_creation_input_tokens")+ if (_cr or _cc) and model:+ try:+ _mi = litellm.get_model_info(model=model, custom_llm_provider=custom_llm_provider)+ if _cr and _mi.get("cache_read_input_token_cost"):+ _cache_read_cost = float(_cr) * float(_mi["cache_read_input_token_cost"])+ if _cc and _mi.get("cache_creation_input_token_cost"):+ _cache_creation_cost = float(_cc) * float(_mi["cache_creation_input_token_cost"])+ except Exception:+ pass_store_cost_breakdown_in_logging_obj(litellm_logging_obj=litellm_logging_obj,prompt_tokens_cost_usd_dollar=prompt_tokens_cost_usd_dollar,@@ -1612,6 +1630,8 @@ def completion_cost( # noqa: PLR0915margin_percent=margin_percent,margin_fixed_amount=margin_fixed_amount,margin_total_amount=margin_total_amount,+ cache_read_cost=_cache_read_cost,+ cache_creation_cost=_cache_creation_cost,)return _final_cost