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]

refactor(cost): extract batch modality rate lookup

kerry2b32f586c087
diff --git a/litellm/cost_calculator.py b/litellm/cost_calculator.py
index cbb9a45ada9..dce6b7299a2 100644
--- a/litellm/cost_calculator.py
+++ b/litellm/cost_calculator.py
@@ -2278,6 +2278,19 @@ def default_video_cost_calculator(
return 0.0
+def _batch_rate(
+ model_info: ModelInfo,
+ key: Literal[
+ "input_cost_per_audio_token_batches",
+ "input_cost_per_image_token_batches",
+ "input_cost_per_video_token_batches",
+ ],
+ fallback: float,
+) -> float:
+ rate: Final = model_info.get(key)
+ return fallback if rate is None else cast(float, rate)
+
+
def batch_cost_calculator(
usage: Usage,
model: str,
@@ -2344,15 +2357,9 @@ def batch_cost_calculator(
batch_details["video_tokens"],
)
modality_rates: Final = (
- cast(float, model_info.get("input_cost_per_audio_token_batches"))
- if model_info.get("input_cost_per_audio_token_batches") is not None
- else input_cost_per_token_batches,
- cast(float, model_info.get("input_cost_per_image_token_batches"))
- if model_info.get("input_cost_per_image_token_batches") is not None
- else input_cost_per_token_batches,
- cast(float, model_info.get("input_cost_per_video_token_batches"))
- if model_info.get("input_cost_per_video_token_batches") is not None
- else input_cost_per_token_batches,
+ _batch_rate(model_info, "input_cost_per_audio_token_batches", input_cost_per_token_batches),
+ _batch_rate(model_info, "input_cost_per_image_token_batches", input_cost_per_token_batches),
+ _batch_rate(model_info, "input_cost_per_video_token_batches", input_cost_per_token_batches),
)
total_prompt_cost = sum(
tokens * rate