diff --git a/litellm/litellm_core_utils/llm_cost_calc/utils.py b/litellm/litellm_core_utils/llm_cost_calc/utils.py index 8fc428b38ae..2b339eac845 100644 --- a/litellm/litellm_core_utils/llm_cost_calc/utils.py +++ b/litellm/litellm_core_utils/llm_cost_calc/utils.py @@ -956,7 +956,9 @@ def _calculate_input_cost( ) ### AUDIO COST - if prompt_tokens_details["audio_tokens"]: + if prompt_tokens_details["audio_tokens"] and not ( + prompt_tokens_details["audio_length_seconds"] and model_info.get("input_cost_per_audio_per_second") is not None + ): audio_cost_key: Final = _get_service_tier_cost_key("input_cost_per_audio_token", service_tier) prompt_cost += calculate_cost_component(model_info, audio_cost_key, prompt_tokens_details["audio_tokens"]) diff --git a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py index 2289de9a951..07a950fc272 100644 --- a/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py +++ b/tests/test_litellm/litellm_core_utils/llm_cost_calc/test_llm_cost_calc_utils.py @@ -1,5 +1,6 @@ import json from datetime import datetime, timezone +from typing import Final import pytest from fastapi.testclient import TestClient @@ -52,6 +53,26 @@ def _local_model_cost_map(monkeypatch: pytest.MonkeyPatch) -> None: monkeypatch.setattr(litellm, "model_cost", litellm.get_model_cost_map(url="")) +@pytest.mark.parametrize( + "duration,second_rate,expected", + ((2.0, 0.00016, 0.00032), (2.0, 0.0, 0.0), (2.0, None, 0.000416), (0.0, 0.00016, 0.000416)), +) +def test_audio_duration_and_tokens_bill_only_once(duration: float, second_rate: float | None, expected: float) -> None: + info: Final[ModelInfo] = { + "input_cost_per_token": 0.0, + "output_cost_per_token": 0.0, + "input_cost_per_audio_token": 6.5e-6, + "input_cost_per_audio_per_second": second_rate, + } + usage: Final = Usage( + prompt_tokens=64, + completion_tokens=0, + prompt_tokens_details=PromptTokensDetailsWrapper(audio_tokens=64, audio_length_seconds=duration, text_tokens=0), + ) + cost, _ = generic_cost_per_token("audio-billing-fixture", usage, "vertex_ai", model_info=info) + assert cost == pytest.approx(expected) + + @pytest.mark.parametrize("prompt_tokens", [100, 200000, 200001]) @pytest.mark.parametrize("read_rate", [None, 0.0, 0.25e-6]) @pytest.mark.parametrize("service_tier", [None, "priority"])