Repository navigation
fix(proxy): price the estimated input tokens on a client-cancelled (499) request's spend row - #47436
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
Merging this PR will not alter performance
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…99) request's spend row
f930be0 to
b0a738f
Compare
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Cursor Bugbot: Medium Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
…dler stamps it so a pre-handoff 499 is not priced
…_disconnect_input_cost
…nger stamps proxy-only failures
|
@litellm-stampbot |
|
bugbot run |
There was a problem hiding this comment.
✅ 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.
…99) request's spend row (BerriAI#47436) Backport of BerriAI#47436 to stable/1.103.x Cherry-picked from 0e7c490 (main)
…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)
…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)
…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).
TLDR
Problem this solves:
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 withprompt_tokens0 andspend0How it solves it:
_client_disconnectedrecognises the 499 (LITELLM_HTTP_STATUS_CLIENT_DISCONNECTED) the proxy raises on cancel, and_provider_billed_the_failuretreats it like the read timeout from the first PR of this stacklitellm_no_upstream_llm_callon that 499 even though the call had reached the provider, so_failure_usage_to_liftno longer drops the estimate when the failure is provider-billed; the 499 row gets its estimatedprompt_tokensand their input pricepre_callno longer stampsfirst_api_call_start_timefor a proxy-only failure, so the row's dispatched check reads a real handoff or nothingcancel_on_disconnectnothing changes: the proxy waits for the provider and already logs the full success rowSecond 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
general_settings.cancel_on_disconnect: true"model": "gpt-4.1"and closes the connection after 1 sprompt_tokens0 and $0 spendAfter: the same request shows the input cost the provider charged
prompt_tokens24 and $0.000048 spendLinear ticket
Resolves LIT-145
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/proxy/test_proxy_utils_model_creation_and_error_logging.py -v(142 passed)Screenshots / Proof of Fix
Real OpenAI, local proxy with Postgres. Config:
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)
After (run at this PR's tip 798ee1e)
Type
🐛 Bug Fix
Caveats (if any)
Low
cancel_on_disconnect) is treated as billed; a disconnect without that setting already logs a full success row and is untouchedcompletion_tokensstays 0Link 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_disconnectis 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_disconnectedand_provider_billed_the_failure, and passes that into failure-path usage lifting._failure_usage_to_liftno longer returns early onLITELLM_LOGGING_NO_UPSTREAM_LLM_CALLwhen 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.