Repository navigation
fix(proxy): price a 499 spend row only for the proxy's own disconnect, and never after the call already completed - #47652
Conversation
…, and never after the call already completed
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
|
|
|
@litellm-stampbot |
litellm-stampbot: risk highNot approved at Rules (
Cursor Bugbot: Medium Risk at Pushes do not re-run this check. Comment |
|
bugbot run |
Merging this PR will not alter performance
|
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…ued, not merely claimed
|
@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 db0310a. Configure here.
…, and never after the call already completed (BerriAI#47652) * fix(proxy): price a 499 spend row only for the proxy's own disconnect, and never after the call already completed * fix(proxy): gate the zero-cost 499 row on the success log being enqueued, not merely claimed --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47652 to stable/1.106.x Cherry-picked from ffb7133 (main)
…, and never after the call already completed (#47652) * fix(proxy): price a 499 spend row only for the proxy's own disconnect, and never after the call already completed * fix(proxy): gate the zero-cost 499 row on the success log being enqueued, not merely claimed --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> (cherry picked from commit ffb7133)
…, and never after the call already completed (BerriAI#47652) * fix(proxy): price a 499 spend row only for the proxy's own disconnect, and never after the call already completed * fix(proxy): gate the zero-cost 499 row on the success log being enqueued, not merely claimed --------- Co-authored-by: devin-ai-integration[bot] <158243242+devin-ai-integration[bot]@users.noreply.github.com> Backport of BerriAI#47652 to stable/1.105.x. Cherry-picked from ffb7133 (main).
Two spend-row bugs in the 499 pricing that #47436 shipped. A provider that itself answers HTTP 499 was being billed as if the caller had hung up on the proxy mid-generation, and a caller who hung up while a during_call guardrail was still running got the prompt billed a second time on top of the completed call's real cost. Both rows now stay at zero, the real client-disconnect row keeps its estimated input cost
Details
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a team's cost dashboard shows spend the provider never charged for
OpenAIException - client closed request"guardrails": ["slow-during-call"]and hang up after 3 seconds while the guardrail is still runningAfter: the same requests show only what the provider actually charged
"guardrails": ["slow-during-call"]and hang up after 3 secondsAffected release
regression since #47436
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
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 moreDelays 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 from the checkout (
python litellm/proxy/proxy_cli.py --config config.yaml --detailed_debug --port 4000), real Postgresdatabase_url,cancel_on_disconnect: true, real OpenAIgpt-5.5calls that cost real money. The upstream 499 case cannot be steered through a real provider, so that one deployment points at a minimal fake upstream on 127.0.0.1:8499 that answers every POST with HTTP 499 and{"error": {"message": "client closed request", "code": 499}}. The during_call guardrail is aCustomGuardrailwhoseasync_moderation_hooksleeps 8 seconds and returns. The hang-up case iscurl -m 3, which exits 28 on both sidesconfig.yaml
Before (5c341e7)
Upstream answers HTTP 499
curl -s -m 30 http://127.0.0.1:4000/v1/chat/completions -H "Authorization: Bearer sk-lit499-master" -H "Content-Type: application/json" -d '{"model":"gpt-5.5-upstream-499","messages":[{"role":"user","content":"main3 f5 one short sentence about tea"}]}'{"error":{"message":"litellm.APIError: APIError: OpenAIException - client closed request\n\nLiteLLM: model group 'gpt-5.5-upstream-499' failed with the error above. No fallback was attempted.","type":"invalid_request_error","param":null,"code":"499"}}psql "$DATABASE_URL" -c "select model, status, prompt_tokens, completion_tokens, spend, request_id from \"LiteLLM_SpendLogs\" where \"startTime\" >= '2026-10-20T21:27:20Z' order by \"startTime\";"(rows shared with the next case, first row is this request)Hang up while a during_call guardrail is pending
curl -s -m 3 http://127.0.0.1:4000/v1/chat/completions -H "Authorization: Bearer sk-lit499-master" -H "Content-Type: application/json" -d '{"model":"gpt-5.5","guardrails":["slow-during-call"],"messages":[{"role":"user","content":"main3 f6 one short sentence about tea"}]}'; echo "curl exit=$?"curl exit=28After (db0310a)
Upstream answers HTTP 499
curl -s -m 30 http://127.0.0.1:4000/v1/chat/completions -H "Authorization: Bearer sk-lit499-master" -H "Content-Type: application/json" -d '{"model":"gpt-5.5-upstream-499","messages":[{"role":"user","content":"tip2 f5 one short sentence about tea"}]}'{"error":{"message":"litellm.APIError: APIError: OpenAIException - client closed request\n\nLiteLLM: model group 'gpt-5.5-upstream-499' failed with the error above. No fallback was attempted.","type":"invalid_request_error","param":null,"code":"499"}}psql "$DATABASE_URL" -c "select model, status, prompt_tokens, completion_tokens, spend, request_id from \"LiteLLM_SpendLogs\" where \"startTime\" >= '2026-10-20T21:51:37Z' order by \"startTime\";"(rows shared with the next case, first row is this request)Hang up while a during_call guardrail is pending
curl -s -m 3 http://127.0.0.1:4000/v1/chat/completions -H "Authorization: Bearer sk-lit499-master" -H "Content-Type: application/json" -d '{"model":"gpt-5.5","guardrails":["slow-during-call"],"messages":[{"role":"user","content":"tip2 f6 one short sentence about tea"}]}'; echo "curl exit=$?"curl exit=28Control on both sides, a hang-up with no guardrail while gpt-5.5 is generating (
curl -m 3, "write 400 words about tea"), still lands one failure row priced at the input rate (8e-05 on both sides, 16 estimated prompt tokens), so the #47436 behavior for a real mid-generation disconnect is unchangedType
🐛 Bug Fix
Caveats (if any)
Medium
Final Attestation
Link to Devin session: https://app.devin.ai/sessions/f4606f7d1d4d4537a43e14b62cc0b537
Open in Devin Desktop: https://app.devin.ai/desktop/session/f4606f7d1d4d4537a43e14b62cc0b537?variant=devin
Requested by: @kerrylu-berri
Note
Medium Risk
Changes proxy failure spend attribution and disconnect detection; wrong logic would under- or over-charge customers on edge paths (499, guardrails).
Overview
Fixes incorrect spend on HTTP 499 and double-billing when a client disconnects after the LLM call already finished.
499 classification:
_client_disconnectednow only treats the proxy’s own FastAPIHTTPException(status 499) as a billed client hang-up. Upstream/provider failures that surface aslitellm.APIErrorwith code 499 are no longer priced like a mid-generation disconnect.No second charge after success:
Loggingtracks whether the async success log was actually enqueued (mark_async_success_log_enqueuedin the@clientpath), not just claimed. Failure-path spend lifting skips estimated input billing when that flag is set, so a 499 raised while a during_call guardrail is still pending does not add another prompt charge on top of the completed call. If success logging was only claimed and deferred for post_call guardrails but never flushed before disconnect, failure billing still applies so the request is charged once.Unit tests cover upstream 499 at $0, post-completion disconnect at $0, and deferred-never-flushed disconnect still priced.
Reviewed by Cursor Bugbot for commit db0310a. Bugbot is set up for automated code reviews on this repo. Configure here.