Skip to content

fix(cost): bill speech at a deployment's custom per-character price - #53265

Merged
kenneth-berri merged 3 commits into
mainfrom
litellm_speech_custom_per_character_pricing
Nov 21, 2026
Merged

kenneth-berri merged 3 commits into
mainfrom
litellm_speech_custom_per_character_pricing

Conversation

@kenneth-berri

@kenneth-berri kenneth-berri commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Handwritten 1-3 sentence description

Setting your own per-character price on a text-to-speech model now changes the tracked cost. Before, the proxy ignored it and billed the public list price.

Details

Link to Devin session: https://app.devin.ai/sessions/1d80c4eaf5034fd2882d1a7921ff4763
Open in Devin Desktop: https://app.devin.ai/desktop/session/1d80c4eaf5034fd2882d1a7921ff4763?variant=devin
Requested by: @kenneth-berri

TLDR

Problem this solves:

  • A custom input_cost_per_character on a speech deployment was ignored
  • Speech was always billed at the public list price
  • A deployment priced at 0 per character was still charged
  • A free speech request returned no cost header

How it solves it:

  • A deployment that sets only a per-character rate now counts as priced
  • Its own rate is used instead of the public row
  • The /v1/audio/speech route sends a zero cost as 0.0 instead of dropping the header

User Flow

Before: a proxy admin with their own text-to-speech rate sees spend tracked at the public list price

  1. They add input_cost_per_character: 0.0002 under model_info (or litellm_params) on an ElevenLabs or OpenAI speech model and restart the proxy
  2. A developer sends POST https://litellm-domain/v1/audio/speech with that model and a 37 character input
  3. The audio comes back with x-litellm-response-cost: 0.00256 for ElevenLabs, which is the public rate of 0.00008 per character

After: the same request is tracked at the admin's own rate

  1. They add input_cost_per_character: 0.0002 under model_info (or litellm_params) on the same model and restart the proxy
  2. The developer sends the same POST https://litellm-domain/v1/audio/speech
  3. The audio comes back with x-litellm-response-cost: 0.0064, which is 32 billed characters at 0.0002

Relevant issues

Found while reproducing #18058. That issue is closed and was about a missing price row, this is the custom price override on the same route.

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*, make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Delays in PR merge?

If you're seeing a delay in your PR being merged, ping the LiteLLM Team on Slack (#pr-review).

Screenshots / Proof of Fix

Live proxy against real ElevenLabs and OpenAI. One worker, no Redis and no database, because the cost of one request does not depend on shared state. Before is the merge base f74d0dc and After is b37e26d.

Env vars: ELEVENLABS_API_KEY, OPENAI_API_KEY

Proxy command: python litellm/proxy/proxy_cli.py --config config.yaml --port 4060

config.yaml
model_list:
  - model_name: elevenlabs-list-price
    litellm_params:
      model: elevenlabs/eleven_multilingual_v2
      api_key: os.environ/ELEVENLABS_API_KEY
  - model_name: elevenlabs-model-info-price
    litellm_params:
      model: elevenlabs/eleven_multilingual_v2
      api_key: os.environ/ELEVENLABS_API_KEY
    model_info:
      input_cost_per_character: 0.0002
  - model_name: elevenlabs-litellm-params-price
    litellm_params:
      model: elevenlabs/eleven_multilingual_v2
      api_key: os.environ/ELEVENLABS_API_KEY
      input_cost_per_character: 0.0002
  - model_name: openai-tts-list-price
    litellm_params:
      model: openai/tts-1
      api_key: os.environ/OPENAI_API_KEY
  - model_name: openai-tts-model-info-price
    litellm_params:
      model: openai/tts-1
      api_key: os.environ/OPENAI_API_KEY
    model_info:
      input_cost_per_character: 0.0002
  - model_name: openai-tts-free
    litellm_params:
      model: openai/tts-1
      api_key: os.environ/OPENAI_API_KEY
    model_info:
      input_cost_per_character: 0
general_settings:
  master_key: sk-1234
  dangerously_permit_weak_or_unset_master_key: true
run.sh, the client
#!/bin/bash
for m in elevenlabs-list-price elevenlabs-model-info-price elevenlabs-litellm-params-price openai-tts-list-price openai-tts-model-info-price openai-tts-free; do
  voice=alloy; case $m in elevenlabs*) voice=21m00Tcm4TlvDq8ikWAM;; esac
  curl -s -o /tmp/out.mp3 -D /tmp/h.txt -w "$m status=%{http_code} bytes=%{size_download} " http://localhost:4060/v1/audio/speech \
    -H "Authorization: Bearer sk-1234" -H "Content-Type: application/json" \
    -d "{\"model\": \"$m\", \"input\": \"Testing speech via the LiteLLM proxy.\", \"voice\": \"$voice\"}"
  grep -i '^x-litellm-response-cost:' /tmp/h.txt | tr -d '\r' || echo "(no x-litellm-response-cost header)"
done

Before (f74d0dc)

  1. ./run.sh
elevenlabs-list-price status=200 bytes=45600 x-litellm-response-cost: 0.00256
elevenlabs-model-info-price status=200 bytes=42674 x-litellm-response-cost: 0.00256
elevenlabs-litellm-params-price status=200 bytes=42674 x-litellm-response-cost: 0.00256
openai-tts-list-price status=200 bytes=36864 x-litellm-response-cost: 0.00048
openai-tts-model-info-price status=200 bytes=38016 x-litellm-response-cost: 0.00048
openai-tts-free status=200 bytes=36480 x-litellm-response-cost: 0.00048

Every override row is billed at the list price, including the row priced at 0.

After (b37e26d)

  1. ./run.sh
elevenlabs-list-price status=200 bytes=43510 x-litellm-response-cost: 0.00256
elevenlabs-model-info-price status=200 bytes=39749 x-litellm-response-cost: 0.0064
elevenlabs-litellm-params-price status=200 bytes=39749 x-litellm-response-cost: 0.0064
openai-tts-list-price status=200 bytes=37632 x-litellm-response-cost: 0.00048
openai-tts-model-info-price status=200 bytes=38016 x-litellm-response-cost: 0.0064
openai-tts-free status=200 bytes=38016 x-litellm-response-cost: 0.0

The override rows are billed at 32 characters times 0.0002, and the list price rows are unchanged. The free row reports 0.0.

Regression test: tests/unit/test_cost_calculator.py::test_speech_cost_bills_the_deployment_per_character_rate, 8 cases, all fail at the merge base and pass at b37e26d. tests/unit/proxy/proxy_server/test_routes_audio.py::test_audio_speech_reports_a_free_clip_as_zero_cost covers the header, and its zero cost case fails without the route change.

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • Azure, Vertex and other per-character speech providers were not run live
    • They share the same code path as the two tested providers
  • The changed check is shared with other "is this row priced" lookups
    • 60 public rows priced only per character now count as priced there
    • Not exercised live outside the speech route

Low

  • A speech request with a zero cost now always sends x-litellm-response-cost: 0.0
    • The route cannot tell a free model from a zero cost with no known price

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

@veria-ai

veria-ai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

No security issues were found in this pull request.

Reviews

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using default effort and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit ef452e2. Configure here.

"input_cost_per_query",
"input_cost_per_text_message",
"input_cost_per_character",
"output_cost_per_character",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zero per-character speech price marked as unknown

Medium Severity

Cost selection now picks up a deployment's per-character rate, but _response_price_is_known and cost_headers don't. For a deployment priced at 0, the cost is 0 while price_known stays false, so the proxy drops x-litellm-response-cost. The request then looks unpriced instead of free. The PR's own live run shows this, and the new test only checks completion_cost.

Additional Locations (1)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit ef452e2. Configure here.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cost here is 0 and known. The speech route reads it with or "", so any zero cost drops the header. That line predates this PR, so I am leaving it for a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in d8119f1. The cost was already 0 and known, the speech route dropped it with or "". It now sends 0.0, with a test.

@greptile-apps

greptile-apps Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] This PR appears safe to merge with no outstanding findings.

Summary

Speech billing now recognizes a deployment's per-character price, including zero. The speech route preserves zero in the cost header.

  • Speech requests use the deployment’s per-character price.
  • Audio speech responses show a zero cost when the price is zero.

Reviews (4) · Last reviewed commit: "test(proxy): type the free speech cost h..." · Reviewed by Greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor

cursor Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Bugbot is paused — your team has an unpaid invoice

Bugbot is paused because your team has an unpaid invoice.

Pay it from the billing page in the Cursor dashboard and Bugbot resumes automatically.

(requestId: 580d6d73-0076-485a-93fb-c008cad807f1)

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

bugbot run

@cursor

cursor Bot commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

Bugbot is paused — your team has an unpaid invoice

Bugbot is paused because your team has an unpaid invoice.

Pay it from the billing page in the Cursor dashboard and Bugbot resumes automatically.

(requestId: f26e45c2-8a70-4eae-909c-762b85e43497)

Comment thread litellm/proxy/proxy_server.py Resolved
@codecov

codecov Bot commented Nov 21, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kenneth-berri

Copy link
Copy Markdown
Collaborator Author

@greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants