mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-19 00:01:29 +00:00
fix(cost): avoid charging audio duration and tokens twice
This commit is contained in:
parent
8b53d48e34
commit
f79a413e74
2 changed files with 24 additions and 1 deletions
|
|
@ -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"])
|
||||
|
||||
|
|
|
|||
|
|
@ -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"])
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue