Skip to content

fix(proxy): price a billed failure row at the deployment's own rate like the success path - #47437

Merged
klu-berri merged 1 commit into
mainfrom
litellm_lit145_deployment_rate_on_failure_rows
Oct 20, 2026
Merged

klu-berri merged 1 commit into
mainfrom
litellm_lit145_deployment_rate_on_failure_rows

Conversation

@klu-berri

@klu-berri klu-berri commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • The first two PRs of this stack price a billed failure row at the cost map's rate for the deployment model
  • The success path prices the same request at the deployment's own rate (input_cost_per_token registered under its model_info.id) and, for an Azure deployment whose name is not in the cost map, at its model_info.base_model; a failure row priced differently disagrees with the success row for the same prompt, or stays at $0 for an Azure alias

How it solves it:

  • _failure_fields_to_lift resolves the deployment the way the success path does: Logging.get_router_model_id() for the deployment id (checks litellm_metadata then metadata, tolerates the None litellm_metadata that get_litellm_params() always writes) and get_base_model_from_metadata for base_model
  • _estimated_input_cost prices base_model or model and passes custom_cost_per_token from deployment_pricing_model_info(model_id, deployment_model), the same helper the success path's get_router_deployment_model_info uses; None means the cost map
  • The id comes from the logging object's own litellm_params, never from the flattened model_call_details copy a request body key named litellm_params can overwrite

Third PR of the LIT-145 stack; #47435, #47456 and #47436 are merged, so this one is on main.

User Flow

Before: a timed-out request on a deployment with its own rate is priced at the public rate

  1. The proxy admin sets input_cost_per_token: 0.00001 on the gpt-4.1 deployment with timeout: 1.5
  2. A developer sends POST https://litellm-domain/v1/chat/completions with "model": "gpt-4.1" and a long prompt
  3. They get HTTP 408 after the retries
  4. https://litellm-domain/ui/?page=logs shows the failure with $0.000052 spend (cost-map rate), while a successful call on that deployment is priced at 0.00001 per input token

After: the failure row uses the deployment's rate

  1. Same config
  2. Same request
  3. Same HTTP 408
  4. https://litellm-domain/ui/?page=logs shows the failure with prompt_tokens 24 and $0.00024 spend (24 x 0.00001)

Linear ticket

Resolves LIT-145

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/proxy/test_proxy_utils_model_creation_and_error_logging.py -v (146 passed)
  • 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

Screenshots / Proof of Fix

Real OpenAI, local proxy with Postgres. Same config and curl as the read-timeout PR, with the deployment's own rate added:

model_list:
  - model_name: gpt-4.1
    litellm_params:
      model: openai/gpt-4.1
      api_key: os.environ/OPENAI_API_KEY
      timeout: 1.5
      input_cost_per_token: 0.00001
      output_cost_per_token: 0.00004
router_settings:
  num_retries: 2

Before (origin/main 4bafebb, $0; with the first PR of the stack it would be 26 x 0.000002 = 5.2e-05)

request_id        | 72b13814-9aba-4999-9a2a-82a049126794
model             | openai/gpt-4.1
spend             | 0
prompt_tokens     | 24
completion_tokens | 0
status            | failure
attempted_retries | 2

After (run at this PR's tip f49a368)

spend is 24 x 0.00001 from the deployment, not the cost map's 0.000002:

HTTP 408 in 10.551771s
request_id        | 6e150153-da0c-4865-a5c8-d4810ba95af3
model             | openai/gpt-4.1
spend             | 0.00024
prompt_tokens     | 24
completion_tokens | 0
status            | failure

Unit tests cover the other sources: a deployment registered under its model_info.id with the real get_litellm_params() shape (test_the_deployment_rate_is_found_on_the_logging_objects_real_litellm_params), an Azure deployment priced at its base_model (test_an_azure_deployment_is_priced_at_its_base_model_like_the_success_path), and a request body {"litellm_params": {"input_cost_per_token": 0}} that cannot zero the price (test_a_litellm_params_key_in_the_request_body_cannot_zero_the_timeout_price).

Type

🐛 Bug Fix

Caveats (if any)

Low

  • A model the cost map does not know, on a deployment that registered no pricing of its own under its model_info.id and has no base_model, keeps its failure row at spend 0
  • base_model is read from model_call_details["litellm_params"] through get_base_model_from_metadata, exactly as the success path reads it, so the two rows agree by construction

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


Note

Medium Risk
Changes proxy spend accounting for failed requests; logic is aligned with existing success-path pricing but affects reported spend and billing visibility.

Overview
Provider-billed failure rows (timeouts, client disconnects after dispatch) now use the same pricing rules as successful requests, instead of only the public cost map rate for the deployment model name.

_failure_fields_to_lift passes Logging.get_router_model_id() and get_base_model_from_metadata() into the estimate path. _estimated_input_cost prices base_model or model and supplies custom_cost_per_token from a new _deployment_cost_per_token helper (via deployment_pricing_model_info). Deployment id is taken from the logging object’s litellm_params, not from model_call_details where a request-body litellm_params key could overwrite it.

Unit tests cover router-registered deployment rates, Azure aliases via base_model, and the spoofed-body case.

Reviewed by Cursor Bugbot for commit f49a368. Bugbot is set up for automated code reviews on this repo. Configure here.

@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@codspeed

codspeed Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 32 untouched benchmarks


Comparing litellm_lit145_deployment_rate_on_failure_rows (f49a368) with main (0e7c490)

Open in CodSpeed

Comment thread litellm/proxy/utils.py Resolved
@veria-ai

veria-ai Bot commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

PR overview

All previously flagged issues have been addressed. No open security concerns remain on this pull request.

Security review

No open security issues remain on this pull request.

Fixed/addressed: 1 · PR risk: 0/10

@codecov

codecov Bot commented Oct 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@klu-berri
klu-berri force-pushed the litellm_lit145_deployment_rate_on_failure_rows branch from 77b4f1a to aa22488 Compare October 19, 2026 23:55
Base automatically changed from litellm_lit145_client_disconnect_input_cost to main October 20, 2026 02:08
@klu-berri
klu-berri force-pushed the litellm_lit145_deployment_rate_on_failure_rows branch from 9ae4d8f to f49a368 Compare October 20, 2026 02:08
@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-berri

kerry-berri commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk high

Not approved at f49a368. risk is high, a human reviewer is needed.

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Medium Risk at f49a368. Changes proxy spend accounting for failed requests; logic is aligned with existing success-path pricing but affects reported spend and billing visibility. Bugbot run requested, finished in 4 min.
Bugbot Low is low, Medium and High are high. The judge model was not called.

Pushes do not re-run this check. Comment @litellm-stampbot again to re-check the new head.

@kerry-berri

Copy link
Copy Markdown
Contributor

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.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit f49a368. Configure here.

@kerry-berri kerry-berri added the risk:high Sensitive, broad, or high-risk change label Oct 20, 2026
@klu-berri
klu-berri merged commit fb555b0 into main Oct 20, 2026
96 of 97 checks passed
@klu-berri
klu-berri deleted the litellm_lit145_deployment_rate_on_failure_rows branch October 20, 2026 02:55
pamanseau pushed a commit to KubeTEE-AI/litellm that referenced this pull request Oct 25, 2026
…ike the success path (BerriAI#47437)

Backport of BerriAI#47437 to stable/1.103.x

Cherry-picked from fb555b0 (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…ike the success path (BerriAI#47437)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47437 to stable/1.106.x
Cherry-picked from fb555b0 (main)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…ike the success path (#47437)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit fb555b0)
haowang1013 pushed a commit to directivegames/litellm that referenced this pull request Nov 3, 2026
…ike the success path (BerriAI#47437)

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>

Backport of BerriAI#47437 to stable/1.105.x.
Cherry-picked from fb555b0 (main).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:high Sensitive, broad, or high-risk change

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants