Skip to content

fix(proxy): price earlier timed-out retry attempts at their own deployment's model rate - #47648

Merged
klu-berri merged 5 commits into
mainfrom
litellm_timed_out_retries_lift
Oct 20, 2026
Merged

klu-berri merged 5 commits into
mainfrom
litellm_timed_out_retries_lift

Conversation

@klu-berri

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

Copy link
Copy Markdown
Contributor

When a request times out on its first model group and the fallback group times out too, the proxy writes one spend row per attempt the provider billed. Since #47490 the rows for the first group's attempts were priced at the final group's model rate, so a gpt-5.4-mini attempt was billed as gpt-5.4. Each earlier attempt is now priced at the model that actually served it

Details

TLDR

Problem this solves:

  • After a fallback, earlier timed-out attempts on another model were priced at the final model's rate

How it solves it:

  • Each retry record now carries the deployment's model and base_model (from model_info or litellm_params)
  • Earlier attempts are priced at their own record's model, not the final attempt's
  • With git stash push -- litellm/ the three new or changed tests fail, with the fix they pass

The router still keeps the last 4 retry records per request, so only those earlier attempts get rows, same as before this PR

User Flow

Before: a developer whose timeout-mini group (gpt-5.4-mini) falls back to gpt-5.4 sees the gpt-5.4-mini attempt billed at gpt-5.4's rate

  1. They send POST http://localhost:4000/v1/chat/completions with "model": "timeout-mini", every attempt times out after 1s, and they get a 408 after 9s
  2. http://localhost:4000/ui/?page=logs shows three failure rows for the request
  3. The gpt-5.4-mini attempt row shows $0.00007 spend, which is gpt-5.4's input rate for 28 tokens, not gpt-5.4-mini's $0.000021

After: the same request bills the gpt-5.4-mini attempt at gpt-5.4-mini's rate

  1. They send the same POST http://localhost:4000/v1/chat/completions with "model": "timeout-mini" and get the same 408 after 9s
  2. http://localhost:4000/ui/?page=logs shows the same three failure rows
  3. The gpt-5.4-mini attempt row shows $0.000021 spend (gpt-5.4-mini's input rate for 28 tokens), the gpt-5.4 rows keep $0.00007

Affected release

regression since #47490

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

Screenshots / Proof of Fix

Live proxy started from the checkout with python litellm/proxy/proxy_cli.py --config ~/lit145/config.yaml --detailed_debug --port 4000, a local Postgres 16 database_url, and real OpenAI calls. Every attempt is cut by the 1s timeout, so each attempt is a real provider call that OpenAI bills for the prompt

Config (~/lit145/config.yaml):

model_list:
  - model_name: timeout-mini
    litellm_params:
      model: openai/gpt-5.4-mini
      api_key: os.environ/OPENAI_API_KEY
      timeout: 1
      num_retries: 1
    model_info:
      id: dep-mini
  - model_name: fallback-big
    litellm_params:
      model: openai/gpt-5.4
      api_key: os.environ/OPENAI_API_KEY
      timeout: 1
      num_retries: 1
    model_info:
      id: dep-fb-big
router_settings:
  fallbacks:
    - timeout-mini: ["fallback-big"]
general_settings:
  master_key: os.environ/LITELLM_MASTER_KEY
  database_url: os.environ/DATABASE_URL

Rates from the cost map: gpt-5.4 input 2.5e-06 per token (28 tokens = 7e-05), gpt-5.4-mini input 7.5e-07 per token (28 tokens = 2.1e-05). The request makes 2 attempts on dep-mini then 2 on dep-fb-big. The router keeps the last 4 retry records and the fallback handler re-logs the attempt that exhausted the first group, so the surviving earlier attempts are dep-mini retry 1 and dep-fb-big retry 0, and the request's own failure row bills the last dep-fb-big attempt

Spend rows read with:

psql "$DATABASE_URL" -c "select end_user, model_id, spend, prompt_tokens, metadata->>'attempted_retries' as retries, metadata->>'status' as status from \"LiteLLM_SpendLogs\" where end_user like 'lit145-f16-%' order by end_user, model_id, (metadata->>'attempted_retries')::int;"

Before (5c341e7)

  1. Command:
    time curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" \
      -d '{"model":"timeout-mini","user":"lit145-f16-main","messages":[{"role":"user","content":"Write a 1500 word essay on the history of the Roman aqueducts, with no headings."}]}'
  2. Output:
    {"error":{"message":"litellm.Timeout: APITimeoutError - Request timed out. Error_str: Request timed out. - timeout value=1.0, time taken=1.02 seconds\n\nDeployment Info: request_timeout: None\ntimeout: 1.0\nstream_timeout: None\n\nLiteLLM: model group 'timeout-mini' failed with the error above. Fallback to fallback-big also failed: litellm.Timeout: APITimeoutError - Request timed out. Error_str: Request timed out. - timeout value=1.0, time taken=1.02 seconds\n\nDeployment Info: request_timeout: None\ntimeout: 1.0\nstream_timeout: None LiteLLM Retried: 1 times, LiteLLM Max Retries: 1","type":"invalid_request_error","param":null,"code":"408"}}
    real    0m8.737s
    
  3. Spend rows, the dep-mini (gpt-5.4-mini) attempt row at 7e-05 is gpt-5.4's rate:
         end_user     |  model_id  |         spend         | prompt_tokens | retries | status
    -----------------+------------+-----------------------+---------------+---------+---------
     lit145-f16-main | dep-fb-big | 7.000000000000001e-05 |            28 | 0       | failure
     lit145-f16-main | dep-mini   | 7.000000000000001e-05 |            28 | 1       | failure
     lit145-f16-main | dep-mini   | 7.000000000000001e-05 |            28 | 1       | failure
    

After (3b7504f)

  1. Command:
    time curl -s http://localhost:4000/v1/chat/completions -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "Content-Type: application/json" \
      -d '{"model":"timeout-mini","user":"lit145-f16-tip","messages":[{"role":"user","content":"Write a 1500 word essay on the history of the Roman aqueducts, with no headings."}]}'
  2. Output:
    {"error":{"message":"litellm.Timeout: APITimeoutError - Request timed out. Error_str: Request timed out. - timeout value=1.0, time taken=1.02 seconds\n\nDeployment Info: request_timeout: None\ntimeout: 1.0\nstream_timeout: None\n\nLiteLLM: model group 'timeout-mini' failed with the error above. Fallback to fallback-big also failed: litellm.Timeout: APITimeoutError - Request timed out. Error_str: Request timed out. - timeout value=1.0, time taken=1.02 seconds\n\nDeployment Info: request_timeout: None\ntimeout: 1.0\nstream_timeout: None LiteLLM Retried: 1 times, LiteLLM Max Retries: 1","type":"invalid_request_error","param":null,"code":"408"}}
    real    0m9.462s
    
  3. Spend rows, the dep-mini attempt row is now 2.1e-05 (gpt-5.4-mini's rate), the two gpt-5.4 attempts stay at 7e-05 (the 7e-05 row keyed dep-mini / retries 1 is the request's own failure row billing the last gpt-5.4 attempt, see Caveats):
         end_user     |  model_id  |         spend         | prompt_tokens | retries | status
    -----------------+------------+-----------------------+---------------+---------+---------
     lit145-f16-tip  | dep-fb-big | 7.000000000000001e-05 |            28 | 0       | failure
     lit145-f16-tip  | dep-mini   | 7.000000000000001e-05 |            28 | 1       | failure
     lit145-f16-tip  | dep-mini   |               2.1e-05 |            28 | 1       | failure
    

Type

🐛 Bug Fix

Caveats (if any)

Medium

  • After a fallback, the request's own failure row keeps the first group's model_id and attempted_retries (dep-mini / 1 above) while billing the last fallback attempt. Pre-existing, same on main, not touched here
  • The router keeps the last 4 retry records per request, so a request with more than 4 failed attempts only gets rows for the last 3 earlier ones. Pre-existing, kept on purpose to bound per-request metadata

Low

  • An earlier attempt whose deployment model has no provider prefix and is not a known model name is billed $0 and debug-logged, the same as the final row for such a deployment

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

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


Note

Medium Risk
Changes proxy spend accounting for multi-hop retries and fallbacks; incorrect pricing would affect billing visibility but not request routing or auth.

Overview
Fixes a spend-log regression where earlier timed-out retry attempts were priced at the final model’s input rate after router fallbacks (e.g. a mini model attempt billed like the larger fallback model).

Router retry breadcrumbs now store each failed hop’s deployment model and base_model on RetryAttemptRecord (log_retry), including Azure base_model from deployment litellm_params when it isn’t on metadata. Proxy failure billing uses new _record_pricing / _billed_earlier_attempt helpers so each earlier timeout row is costed from that record’s model and base_model, not from the final attempt’s model_call_details.

Unit tests cover cross-model fallback pricing and log_retry field population.

Reviewed by Cursor Bugbot for commit f578c6e. 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.

@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@kerry-berri

kerry-berri commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk high

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

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Medium Risk at 0214d73. Changes how failure spend rows are counted and priced in the proxy; incorrect rates would affect billing/reporting but not request routing or auth. Bugbot run requested, finished in 6 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

@codspeed

codspeed Bot commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

⚠️ 1 benchmark spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

✅ 32 untouched benchmarks


Comparing litellm_timed_out_retries_lift (3b7504f) with main (6bc1b2b)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (640f2d8) during the generation of this report, so 6bc1b2b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@devin-ai-integration devin-ai-integration Bot changed the title fix(proxy): bill every earlier timed-out retry attempt at its own deployment's model rate fix(proxy): price earlier timed-out retry attempts at their own deployment's model rate Oct 20, 2026

@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.

Stale Bugbot comment from a previous run.

Comment thread litellm/router.py Resolved
@kerry-berri kerry-berri added the risk:high Sensitive, broad, or high-risk change label Oct 20, 2026
@klu-berri

Copy link
Copy Markdown
Contributor Author

@BugBot run

… base_model helper as covered through log_retry

@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 f578c6e. Configure here.

@klu-berri
klu-berri enabled auto-merge (squash) October 20, 2026 22:08
@klu-berri
klu-berri merged commit 27efc38 into main Oct 20, 2026
88 of 89 checks passed
@klu-berri
klu-berri deleted the litellm_timed_out_retries_lift branch October 20, 2026 22:26
devin-ai-integration Bot added a commit that referenced this pull request Oct 21, 2026
…yment's model rate (#47648)

Backport of #47648 to stable/1.103.x

Cherry-picked from 27efc38 (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…yment's model rate (BerriAI#47648)

* fix(proxy): bill every earlier timed-out retry attempt at its own deployment's model rate

* fix(router): read the retry record's base_model through the typed metadata helper

* fix(router): keep the four-record retry breadcrumb cap

* fix(router): carry a deployment's litellm_params base_model on its retry record

* chore(router): place the cast-ok reason on the cast line and list the base_model helper as covered through log_retry

---------

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

Backport of BerriAI#47648 to stable/1.106.x
Cherry-picked from 27efc38 (main)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…yment's model rate (#47648)

* fix(proxy): bill every earlier timed-out retry attempt at its own deployment's model rate

* fix(router): read the retry record's base_model through the typed metadata helper

* fix(router): keep the four-record retry breadcrumb cap

* fix(router): carry a deployment's litellm_params base_model on its retry record

* chore(router): place the cast-ok reason on the cast line and list the base_model helper as covered through log_retry

---------

Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com>
(cherry picked from commit 27efc38)
haowang1013 pushed a commit to directivegames/litellm that referenced this pull request Nov 3, 2026
…yment's model rate (BerriAI#47648)

* fix(proxy): bill every earlier timed-out retry attempt at its own deployment's model rate

* fix(router): read the retry record's base_model through the typed metadata helper

* fix(router): keep the four-record retry breadcrumb cap

* fix(router): carry a deployment's litellm_params base_model on its retry record

* chore(router): place the cast-ok reason on the cast line and list the base_model helper as covered through log_retry

---------

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

Backport of BerriAI#47648 to stable/1.105.x.
Cherry-picked from 27efc38 (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