Skip to content

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]

perf: skip cost component calculations when usage values are zero

Ryan Crabbe00a1ba7bdcb4
diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py
index dae1cf1cace..d1a47efde7e 100644
--- a/litellm/litellm_core_utils/llm_cost_calc/utils.py
+++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py
@@ -518,47 +518,52 @@ def _calculate_input_cost(
prompt_cost += float(prompt_tokens_details["cache_hit_tokens"]) * cache_read_cost
### AUDIO COST
- prompt_cost += calculate_cost_component(
- model_info, "input_cost_per_audio_token", prompt_tokens_details["audio_tokens"]
- )
+ if prompt_tokens_details["audio_tokens"]:
+ prompt_cost += calculate_cost_component(
+ model_info, "input_cost_per_audio_token", prompt_tokens_details["audio_tokens"]
+ )
### IMAGE TOKEN COST
- # For image token costs:
- # First check if input_cost_per_image_token is available. If not, default to generic input_cost_per_token.
- image_token_cost_key = "input_cost_per_image_token"
- if model_info.get(image_token_cost_key) is None:
- image_token_cost_key = "input_cost_per_token"
- prompt_cost += calculate_cost_component(
- model_info, image_token_cost_key, prompt_tokens_details["image_tokens"]
- )
+ if prompt_tokens_details["image_tokens"]:
+ # For image token costs:
+ # First check if input_cost_per_image_token is available. If not, default to generic input_cost_per_token.
+ image_token_cost_key = "input_cost_per_image_token"
+ if model_info.get(image_token_cost_key) is None:
+ image_token_cost_key = "input_cost_per_token"
+ prompt_cost += calculate_cost_component(
+ model_info, image_token_cost_key, prompt_tokens_details["image_tokens"]
+ )
### CACHE WRITING COST - Now uses tiered pricing
- prompt_cost += calculate_cache_writing_cost(
- cache_creation_tokens=prompt_tokens_details["cache_creation_tokens"],
- cache_creation_token_details=prompt_tokens_details[
- "cache_creation_token_details"
- ],
- cache_creation_cost_above_1hr=cache_creation_cost_above_1hr,
- cache_creation_cost=cache_creation_cost,
- )
+ if prompt_tokens_details["cache_creation_tokens"]:
+ prompt_cost += calculate_cache_writing_cost(
+ cache_creation_tokens=prompt_tokens_details["cache_creation_tokens"],
+ cache_creation_token_details=prompt_tokens_details[
+ "cache_creation_token_details"
+ ],
+ cache_creation_cost_above_1hr=cache_creation_cost_above_1hr,
+ cache_creation_cost=cache_creation_cost,
+ )
### CHARACTER COST
-
- prompt_cost += calculate_cost_component(
- model_info, "input_cost_per_character", prompt_tokens_details["character_count"]
- )
+ if prompt_tokens_details["character_count"]:
+ prompt_cost += calculate_cost_component(
+ model_info, "input_cost_per_character", prompt_tokens_details["character_count"]
+ )
### IMAGE COUNT COST
- prompt_cost += calculate_cost_component(
- model_info, "input_cost_per_image", prompt_tokens_details["image_count"]
- )
+ if prompt_tokens_details["image_count"]:
+ prompt_cost += calculate_cost_component(
+ model_info, "input_cost_per_image", prompt_tokens_details["image_count"]
+ )
### VIDEO LENGTH COST
- prompt_cost += calculate_cost_component(
- model_info,
- "input_cost_per_video_per_second",
- prompt_tokens_details["video_length_seconds"],
- )
+ if prompt_tokens_details["video_length_seconds"]:
+ prompt_cost += calculate_cost_component(
+ model_info,
+ "input_cost_per_video_per_second",
+ prompt_tokens_details["video_length_seconds"],
+ )
return prompt_cost