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]

fix(whisper): correct output_cost_per_second pricing and cost calculation (#23842)

Geoffray Viossat00dd9844158c
diff --git a/litellm/llms/openai/cost_calculation.py b/litellm/llms/openai/cost_calculation.py
index ac1e4a6b08f..d5077e25a43 100644
--- a/litellm/llms/openai/cost_calculation.py
+++ b/litellm/llms/openai/cost_calculation.py
@@ -114,7 +114,7 @@ def cost_per_second(
)
## COST PER SECOND ##
completion_cost = model_info["output_cost_per_second"] * duration
- elif (
+ if (
"input_cost_per_second" in model_info
and model_info["input_cost_per_second"] is not None
):
@@ -123,7 +123,6 @@ def cost_per_second(
)
## COST PER SECOND ##
prompt_cost = model_info["input_cost_per_second"] * duration
- completion_cost = 0.0
return prompt_cost, completion_cost
diff --git a/litellm/model_prices_and_context_window_backup.json b/litellm/model_prices_and_context_window_backup.json
index 879dd42be47..595cdf3e138 100644
--- a/litellm/model_prices_and_context_window_backup.json
+++ b/litellm/model_prices_and_context_window_backup.json
@@ -5725,7 +5725,7 @@
"input_cost_per_second": 0.0001,
"litellm_provider": "azure",
"mode": "audio_transcription",
- "output_cost_per_second": 0.0001
+ "output_cost_per_second": 0.0
},
"azure_ai/Cohere-embed-v3-english": {
"input_cost_per_token": 1e-07,
@@ -31996,7 +31996,7 @@
"input_cost_per_second": 0.0001,
"litellm_provider": "openai",
"mode": "audio_transcription",
- "output_cost_per_second": 0.0001,
+ "output_cost_per_second": 0.0,
"supported_endpoints": [
"/v1/audio/transcriptions"
]
diff --git a/model_prices_and_context_window.json b/model_prices_and_context_window.json
index bbf9f6d9dc8..c1ba4d88d4d 100644
--- a/model_prices_and_context_window.json
+++ b/model_prices_and_context_window.json
@@ -5725,7 +5725,7 @@
"input_cost_per_second": 0.0001,
"litellm_provider": "azure",
"mode": "audio_transcription",
- "output_cost_per_second": 0.0001
+ "output_cost_per_second": 0.0
},
"azure_ai/Cohere-embed-v3-english": {
"input_cost_per_token": 1e-07,
@@ -32000,7 +32000,7 @@
"input_cost_per_second": 0.0001,
"litellm_provider": "openai",
"mode": "audio_transcription",
- "output_cost_per_second": 0.0001,
+ "output_cost_per_second": 0.0,
"supported_endpoints": [
"/v1/audio/transcriptions"
]
diff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.py
index 8f5c3ece0ca..88c384bcaec 100644
--- a/tests/test_litellm/test_cost_calculator.py
+++ b/tests/test_litellm/test_cost_calculator.py
@@ -1,5 +1,6 @@
import os
import sys
+from unittest.mock import patch
import pytest
@@ -16,6 +17,7 @@ from litellm.cost_calculator import (
handle_realtime_stream_cost_calculation,
response_cost_calculator,
)
+from litellm.llms.openai.cost_calculation import cost_per_second
from litellm.types.llms.openai import OpenAIRealtimeStreamList
from litellm.types.utils import ModelResponse, PromptTokensDetailsWrapper, Usage
from litellm.utils import TranscriptionResponse
@@ -1970,3 +1972,53 @@ def test_additional_costs_only_for_azure_ai():
completion_tokens=50,
)
assert result is None, "Vertex AI should have no additional costs"
+
+
+class TestCostPerSecondArithmetic:
+ """Unit tests for the cost_per_second arithmetic itself.
+
+ The tests in TestCostCalculatorReadsDurationFromHiddenParams mock
+ openai_cost_per_second entirely — they verify that the right duration
+ is forwarded, but never that the math inside cost_per_second is correct.
+ These tests cover the arithmetic directly.
+ """
+
+ def test_input_cost_per_second_only(self):
+ """input_cost_per_second * duration = prompt_cost; completion_cost = 0."""
+ with patch("litellm.llms.openai.cost_calculation.get_model_info") as mock_info:
+ mock_info.return_value = {
+ "input_cost_per_second": 0.0001,
+ "output_cost_per_second": 0.0,
+ }
+ prompt_cost, completion_cost_val = cost_per_second(
+ model="whisper-1", custom_llm_provider="openai", duration=60.0
+ )
+ assert prompt_cost == pytest.approx(0.006)
+ assert completion_cost_val == 0.0
+
+ def test_both_input_and_output_cost_per_second(self):
+ """When both fields are set, they are applied independently."""
+ with patch("litellm.llms.openai.cost_calculation.get_model_info") as mock_info:
+ mock_info.return_value = {
+ "input_cost_per_second": 0.002,
+ "output_cost_per_second": 0.003,
+ }
+ prompt_cost, completion_cost_val = cost_per_second(
+ model="some-model", custom_llm_provider="openai", duration=10.0
+ )
+ assert prompt_cost == pytest.approx(0.02)
+ assert completion_cost_val == pytest.approx(0.03)
+
+ def test_zero_duration_returns_zero_cost(self):
+ """A zero-duration transcription must cost nothing regardless of the rate."""
+ with patch("litellm.llms.openai.cost_calculation.get_model_info") as mock_info:
+ mock_info.return_value = {
+ "input_cost_per_second": 0.0001,
+ "output_cost_per_second": 0.0,
+ }
+ prompt_cost, completion_cost_val = cost_per_second(
+ model="whisper-1", custom_llm_provider="openai", duration=0.0
+ )
+ assert prompt_cost == 0.0
+ assert completion_cost_val == 0.0
+