fix(elevenlabs): count leading and trailing whitespace in /v1/audio/speech characters

ElevenLabs counted 73 for a 70-character input with two leading spaces and a trailing newline, both directly and through the proxy, so the speech bill now counts every character
This commit is contained in:
mateo-berri 2026-10-21 02:53:29 -07:00
parent f8ad7834e1
commit 7d7949dbb9
2 changed files with 5 additions and 5 deletions

View file

@ -1845,7 +1845,7 @@ def completion_cost(
)
elif call_type in _SPEECH_CALL_TYPES:
prompt_characters = (
len(prompt.strip())
len(prompt)
if custom_llm_provider == LlmProviders.ELEVENLABS.value
else litellm.utils.count_characters(text=prompt)
)

View file

@ -9134,7 +9134,7 @@ def test_azure_tts_cost_uses_test_owned_character_rate(monkeypatch: pytest.Monke
assert cost == pytest.approx(0.05)
def test_elevenlabs_tts_cost_counts_the_spaces_between_words(monkeypatch: pytest.MonkeyPatch) -> None:
def test_elevenlabs_tts_cost_counts_every_whitespace_character(monkeypatch: pytest.MonkeyPatch) -> None:
model: Final = "litellm-test-tts"
monkeypatch.setitem(
litellm.model_cost,
@ -9146,7 +9146,7 @@ def test_elevenlabs_tts_cost_counts_the_spaces_between_words(monkeypatch: pytest
"mode": "audio_speech",
},
)
spoken: Final = "hello there, world"
prompt: Final = " hello there, world\n"
cost: Final = response_cost_calculator(
response_object=TranscriptionResponse(text=""),
@ -9154,10 +9154,10 @@ def test_elevenlabs_tts_cost_counts_the_spaces_between_words(monkeypatch: pytest
custom_llm_provider="elevenlabs",
call_type="aspeech",
optional_params={},
prompt=f" {spoken}\n",
prompt=prompt,
)
assert cost == pytest.approx(len(spoken) * 0.01)
assert cost == pytest.approx(len(prompt) * 0.01)
def test_moderation_cost_uses_test_owned_token_rate() -> None: