Skip to content

fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row - #47436

Merged
klu-berri merged 4 commits into
mainfrom
litellm_lit145_client_disconnect_input_cost
Oct 20, 2026
Merged

klu-berri merged 4 commits into
mainfrom
litellm_lit145_client_disconnect_input_cost

Conversation

@klu-berri

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

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • With general_settings.cancel_on_disconnect: true, a caller that hangs up mid-request makes the proxy cancel the upstream call and write a 499 failure row with prompt_tokens 0 and spend 0
  • The provider had the prompt and keeps working after LiteLLM cancels, so it bills the prompt

How it solves it:

  • _client_disconnected recognises the 499 (LITELLM_HTTP_STATUS_CLIENT_DISCONNECTED) the proxy raises on cancel, and _provider_billed_the_failure treats it like the read timeout from the first PR of this stack
  • The proxy-only error handler stamps litellm_no_upstream_llm_call on that 499 even though the call had reached the provider, so _failure_usage_to_lift no longer drops the estimate when the failure is provider-billed; the 499 row gets its estimated prompt_tokens and their input price
  • A 499 from a caller that hung up before the request reached the provider stays at $0: since fix(logging): do not stamp a provider handoff when pre_call runs for a proxy-only failure #47456 pre_call no longer stamps first_api_call_start_time for a proxy-only failure, so the row's dispatched check reads a real handoff or nothing
  • Without cancel_on_disconnect nothing changes: the proxy waits for the provider and already logs the full success row

Second PR of the LIT-145 stack; #47435 (read timeout) and #47456 (handoff stamp) are merged, so this one is on main.

User Flow

Before: a cancelled request is logged with no tokens and no spend although the provider billed it

  1. The proxy admin sets general_settings.cancel_on_disconnect: true
  2. A developer sends POST https://litellm-domain/v1/chat/completions with "model": "gpt-4.1" and closes the connection after 1 s
  3. The proxy cancels the upstream call and logs a 499 failure
  4. https://litellm-domain/ui/?page=logs shows the request with prompt_tokens 0 and $0 spend

After: the same request shows the input cost the provider charged

  1. Same config
  2. Same request
  3. Same 499
  4. https://litellm-domain/ui/?page=logs shows prompt_tokens 24 and $0.000048 spend

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 (142 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. Config:

model_list:
  - model_name: gpt-4.1
    litellm_params:
      model: openai/gpt-4.1
      api_key: os.environ/OPENAI_API_KEY
      timeout: 60
general_settings:
  master_key: sk-...
  cancel_on_disconnect: true

The caller hangs up after 1 s (curl --max-time 1 ..., exit 28, no HTTP response), so the proxy cancels the upstream call and writes the 499 row.

Before (this code path is unchanged on origin/main; observed at b501c23)

request_id        | 34481ab6-25c4-4158-93f9-b8530ac5a001
model             | openai/gpt-4.1
spend             | 0
prompt_tokens     | 0
completion_tokens | 0
status            | failure
error_class       | HTTPException
error_code        | 499

After (run at this PR's tip 798ee1e)

HTTP 000 in 1.004363s (curl exit 28)
request_id        | 35590e7e-f10c-4e01-b0d0-cc334b3c1260
model             | openai/gpt-4.1
spend             | 4.8e-05
prompt_tokens     | 24
completion_tokens | 0
status            | failure

Type

🐛 Bug Fix

Caveats (if any)

Low

  • Only the proxy's own 499 (cancel_on_disconnect) is treated as billed; a disconnect without that setting already logs a full success row and is untouched
  • Same floor as the read-timeout PR: output tokens are unknowable, completion_tokens stays 0

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 failure-path spend estimation and billing attribution for 499 errors; incorrect dispatch detection could mis-price or skip estimates, but scope is limited to proxy error logging.

Overview
Fixes spend logging for client-cancelled (499) requests when cancel_on_disconnect is enabled: failure rows now get estimated prompt tokens and input-side cost instead of $0, matching the case where the provider already received the prompt.

The proxy treats HTTP 499 (LITELLM_HTTP_STATUS_CLIENT_DISCONNECTED) like a read timeout via _client_disconnected and _provider_billed_the_failure, and passes that into failure-path usage lifting. _failure_usage_to_lift no longer returns early on LITELLM_LOGGING_NO_UPSTREAM_LLM_CALL when the failure is provider-billed (that flag is also set on 499 even after handoff). 499 before provider handoff still logs zero usage/cost.

Unit tests cover priced post-dispatch disconnect and unpriced pre-handoff disconnect.

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

⚠️ 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_lit145_client_disconnect_input_cost (798ee1e) with main (3fa1587)

Open in CodSpeed

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

Base automatically changed from litellm_lit145_read_timeout_input_cost to main October 19, 2026 23:55
@klu-berri
klu-berri force-pushed the litellm_lit145_client_disconnect_input_cost branch from f930be0 to b0a738f Compare October 19, 2026 23:55
@klu-berri

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

@kerry-berri

kerry-berri commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

litellm-stampbot: risk high

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

Rules (stampbot/risk_rules.yml):

  • low: no rule flagged it

Cursor Bugbot: Medium Risk at 798ee1e. Changes failure-path spend estimation and billing attribution for 499 errors; incorrect dispatch detection could mis-price or skip estimates, but scope is limited to proxy error logging. 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

@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/proxy/utils.py Resolved
@kerry-berri kerry-berri added the risk:high Sensitive, broad, or high-risk change label Oct 20, 2026
…dler stamps it so a pre-handoff 499 is not priced

Copy link
Copy Markdown
Contributor Author

@litellm-stampbot

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

pamanseau pushed a commit to KubeTEE-AI/litellm that referenced this pull request Oct 25, 2026
…99) request's spend row (BerriAI#47436)

Backport of BerriAI#47436 to stable/1.103.x

Cherry-picked from 0e7c490 (main)
Cartofante pushed a commit to CartoDB/litellm that referenced this pull request Oct 26, 2026
…99) request's spend row (BerriAI#47436)

* fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row

* fix(proxy): read the provider handoff before the proxy-only error handler stamps it so a pre-handoff 499 is not priced

* fix(proxy): drop the pre-logging handoff read now that pre_call no longer stamps proxy-only failures

---------

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

Backport of BerriAI#47436 to stable/1.106.x
Cherry-picked from 0e7c490 (main)
yuneng-berri pushed a commit that referenced this pull request Oct 28, 2026
…99) request's spend row (#47436)

* fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row

* fix(proxy): read the provider handoff before the proxy-only error handler stamps it so a pre-handoff 499 is not priced

* fix(proxy): drop the pre-logging handoff read now that pre_call no longer stamps proxy-only failures

---------

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

* fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row

* fix(proxy): read the provider handoff before the proxy-only error handler stamps it so a pre-handoff 499 is not priced

* fix(proxy): drop the pre-logging handoff read now that pre_call no longer stamps proxy-only failures

---------

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

Backport of BerriAI#47436 to stable/1.105.x.
Cherry-picked from 0e7c490 (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