Repository navigation
Conversation
… streams A client disconnect cancelled the in-flight provider read and the router closed the attempt, so spend logs only held an estimate from the chunks the client got. Armed streams now read through a shielded task and a bounded detached drain reads them to the terminal usage chunk, matching what BerriAI#36008 did for /v1/messages. Generated with [Devin](https://devin.ai) Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Contributor
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 |
The drain cap only bounded how many drains run, so a stalled provider could hold a drain slot, its buffered chunks and the request's concurrency slot forever. Each drain now stops at CHAT_COMPLETIONS_DETACHED_STREAM_DRAIN_TIMEOUT_SECONDS (default 600), bills the chunks read so far and closes the provider stream.
3 of 6 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TLDR
Problem this solves:
/v1/chat/completionsstreams log far fewer tokens than Bedrock billsHow it solves it:
/v1/messagesCHAT_COMPLETIONS_MAX_DETACHED_STREAM_DRAINS(default 100, 0 turns it off)User Flow
Before: a team streaming long chat completions through the proxy sees spend that undercounts their AWS Bedrock bill whenever a client stops reading early
"stream": trueand a prompt that produces a long answercompletion_tokensin the tens, priced from the few chunks the app receivedAfter: the same interrupted request is logged with the output tokens Bedrock actually billed
"stream": truecompletion_tokensin the thousands, in line with an uninterrupted run of the same promptRelevant issues
Fixes #46499
Related: #33736 (added partial billing on disconnect), #34147, #35958, #36008 (full billing for
/v1/messages)Linear ticket
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<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@greptileaito re-request a review after pushing changes)The new tests are in
TestStreamingDisconnectDrainsProviderUsageintests/unit/proxy/test_common_request_processing.py. A custom provider holds back the end of its stream, including its final usage of 50 prompt and 2000 completion tokens, until after the client has disconnected mid-read. Three mutations were checked against them:Delays 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
Run against a local proxy calling live AWS Bedrock (
us.anthropic.claude-sonnet-5,us-west-2), with Postgres for spend logs. Every request below hit the real Bedrock API and was billed by AWSProxy start command, the same for both runs:
config.yamldeployment:Shared shell setup:
Before (d8f162e)
Client disconnects after 2000 bytes
Client reads the whole stream (control)
After (73ea3b9)
Client disconnects after 2000 bytes
Client reads the whole stream (control)
Type
🐛 Bug Fix
Caveats (if any)
Medium
/v1/messagesthrough the chat adapter/v1/responsesbridged to chat completionsLow
async_data_generatordoes in its cleanupCHAT_COMPLETIONS_MAX_DETACHED_STREAM_DRAINSis excluded from the env-key docs test, like the/v1/messagesdrain settingsFinal Attestation