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
Fix audio cost per second override (#19158)
Dushyant2a0f87bde048
diff --git a/docs/my-website/docs/sdk_custom_pricing.md b/docs/my-website/docs/sdk_custom_pricing.mdindex c8577115109..ac956db472a 100644--- a/docs/my-website/docs/sdk_custom_pricing.md+++ b/docs/my-website/docs/sdk_custom_pricing.md@@ -2,7 +2,10 @@Register custom pricing for sagemaker completion model.-For cost per second pricing, you **just** need to register `input_cost_per_second`.+For cost per second pricing, register `input_cost_per_second`. If your provider+charges for audio output duration (e.g., TTS), also set `output_cost_per_second`.+Values of `0` are treated as not billable, so `output_cost_per_second: 0` will+not override `input_cost_per_second`.```python# !pip install boto3diff --git a/litellm/llms/openai/cost_calculation.py b/litellm/llms/openai/cost_calculation.pyindex e5349db3af7..339ce9caedd 100644--- a/litellm/llms/openai/cost_calculation.py+++ b/litellm/llms/openai/cost_calculation.py@@ -105,25 +105,21 @@ def cost_per_second(prompt_cost = 0.0completion_cost = 0.0## Speech / Audio cost calculation- if (- "output_cost_per_second" in model_info- and model_info["output_cost_per_second"] is not None- ):+ output_cost_per_second = model_info.get("output_cost_per_second")+ input_cost_per_second = model_info.get("input_cost_per_second")++ if output_cost_per_second is not None and output_cost_per_second > 0:verbose_logger.debug(- f"For model={model} - output_cost_per_second: {model_info.get('output_cost_per_second')}; duration: {duration}"+ f"For model={model} - output_cost_per_second: {output_cost_per_second}; duration: {duration}")## COST PER SECOND ##- completion_cost = model_info["output_cost_per_second"] * duration- elif (- "input_cost_per_second" in model_info- and model_info["input_cost_per_second"] is not None- ):+ completion_cost = output_cost_per_second * duration+ if input_cost_per_second is not None and input_cost_per_second > 0:verbose_logger.debug(- f"For model={model} - input_cost_per_second: {model_info.get('input_cost_per_second')}; duration: {duration}"+ f"For model={model} - input_cost_per_second: {input_cost_per_second}; duration: {duration}")## COST PER SECOND ##- prompt_cost = model_info["input_cost_per_second"] * duration- completion_cost = 0.0+ prompt_cost = input_cost_per_second * durationreturn prompt_cost, completion_costdiff --git a/tests/test_litellm/test_cost_calculator.py b/tests/test_litellm/test_cost_calculator.pyindex 4d6599fc1b5..42bcfca2311 100644--- a/tests/test_litellm/test_cost_calculator.py+++ b/tests/test_litellm/test_cost_calculator.py@@ -192,6 +192,40 @@ def test_transcription_cost_falls_back_to_duration():assert pytest.approx(cost, rel=1e-6) == expected_cost+def test_transcription_cost_prefers_input_when_output_zero(monkeypatch):+ from litellm import completion_cost++ os.environ["LITELLM_LOCAL_MODEL_COST_MAP"] = "True"+ litellm.model_cost = litellm.get_model_cost_map(url="")++ model_name = "custom-whisper-input-only"+ custom_model_info = {+ "input_cost_per_second": 0.00005,+ "output_cost_per_second": 0.0,+ "litellm_provider": "openai",+ "mode": "audio_transcription",+ "supported_endpoints": ["/v1/audio/transcriptions"],+ }+ monkeypatch.setattr(+ litellm,+ "model_cost",+ {**litellm.model_cost, model_name: custom_model_info},+ )++ response = TranscriptionResponse(text="demo text")+ response.duration = 300.0++ cost = completion_cost(+ completion_response=response,+ model=model_name,+ custom_llm_provider="openai",+ call_type="atranscription",+ )++ expected_cost = 300.0 * 0.00005+ assert pytest.approx(cost, rel=1e-6) == expected_cost++def test_handle_realtime_stream_cost_calculation():from litellm.cost_calculator import RealtimeAPITokenUsageProcessor