Repository navigation
fix(anthropic): log partial stream spend when a /v1/messages client disconnects mid-stream - #37558
Conversation
…isconnects mid-stream
|
bugbot run |
Greptile SummaryThe PR ensures partial Anthropic Messages stream spend is dispatched when a client disconnects or consumption is cancelled.
Confidence Score: 5/5The PR appears safe to merge, with the disconnect and cancellation paths covered by focused regression tests and no actionable defects identified. The changed teardown flow logs collected chunks once, preserves cancellation, leaves normal completion behavior intact, and deterministically reaches partial-spend dispatch from the Bedrock wrapper.
|
| Filename | Overview |
|---|---|
| litellm/llms/anthropic/experimental_pass_through/messages/streaming_iterator.py | Dispatches normal and partial-stream logging through the global worker and logs collected chunks during generator cancellation or closure. |
| litellm/llms/bedrock/messages/invoke_transformations/anthropic_claude3_transformation.py | Closes the inner SSE generator during Bedrock wrapper teardown so disconnect-driven partial logging runs deterministically. |
| tests/test_litellm/llms/anthropic/experimental_pass_through/messages/test_streaming_iterator.py | Adds coverage for partial logging on explicit closure and cancellation, including the zero-chunk case. |
| tests/test_litellm/llms/bedrock/messages/invoke_transformations/test_anthropic_claude3_transformation.py | Verifies closing the outer Bedrock stream reaches the inner logging handler. |
Reviews (1): Last reviewed commit: "fix(anthropic): log partial stream spend..." | Re-trigger Greptile
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 9209bb6. Configure here.
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
TLDR
Problem this solves:
/v1/messagesstreams recorded zero spendHow it solves it:
User Flow
Before: a developer streams a Bedrock-backed answer through
/v1/messages, their end user cancels mid-answer, and the request never shows up in spend tracking"model": "bedrock-claude-opus-5","stream": true, and"max_tokens": 4000[]back no matter how long they waitAfter: the same canceled stream lands in spend logs within seconds, with the partial output tokens counted
"model": "bedrock-claude-opus-5","stream": true, and"max_tokens": 4000successrow seconds later, with the prompt tokens plus the output tokens generated before the cancel and their real costRelevant issues
Linear ticket
Resolves LIT-5839
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaito re-request a review after pushing changes)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
Shared setup, both sides identical: proxy from the tree under test with a fresh Postgres DB, real Bedrock calls in us-east-1
Before (eecb226)
Control: stream left to complete
curl -s -N -D headers http://127.0.0.1:44545/v1/messages -H "Authorization: Bearer $LITELLM_MASTER_KEY" -H "anthropic-version: 2023-06-01" -H "Content-Type: application/json" -d '{"model":"bedrock-claude-opus-5","stream":true,"max_tokens":600,"messages":[{"role":"user","content":"Write about 400 words on how rivers shape landscapes."}]}'message_stopwith"usage": {"input_tokens": 25, "output_tokens": 600},x-litellm-call-id: 23eea144-e8c7-4fe1-8a72-f5d28be8d648curl -s "http://127.0.0.1:44545/spend/logs?request_id=23eea144-e8c7-4fe1-8a72-f5d28be8d648" -H "Authorization: Bearer $LITELLM_MASTER_KEY"returns one row:status success, call_type anthropic_messages, prompt_tokens 25, completion_tokens 600, total_tokens 625, spend 0.0166375Client disconnect mid-stream
"max_tokens": 4000and a longer prompt, piped through| head -n 150so curl exits mid-stream after 50 SSE events,x-litellm-call-id: 62b260f4-c72f-4a79-987f-3aea51b6a521curl -s "http://127.0.0.1:44545/spend/logs?request_id=62b260f4-c72f-4a79-987f-3aea51b6a521" -H "Authorization: Bearer $LITELLM_MASTER_KEY"polled every 3s for 45s returns[]every time: the tokens Bedrock generated and billed are never trackedAfter (9209bb6)
Control: stream left to complete
x-litellm-call-id: 23c11e3c-09f9-47dd-88fd-94b7a40b9b8cmessage_stopwith"usage": {"input_tokens": 25, "output_tokens": 600}curl -s "http://127.0.0.1:44545/spend/logs?request_id=23c11e3c-09f9-47dd-88fd-94b7a40b9b8c" -H "Authorization: Bearer $LITELLM_MASTER_KEY"returns one row identical in shape to Before:status success, call_type anthropic_messages, prompt_tokens 25, completion_tokens 600, total_tokens 625, spend 0.0166375Client disconnect mid-stream
| head -n 150, curl exits after 50 SSE events,x-litellm-call-id: 61a2b61c-13d0-4bcf-b50a-6cc21a69427acurl -s "http://127.0.0.1:44545/spend/logs?request_id=61a2b61c-13d0-4bcf-b50a-6cc21a69427a" -H "Authorization: Bearer $LITELLM_MASTER_KEY"returns one row at the first 3s poll:status success, call_type anthropic_messages, prompt_tokens 23, completion_tokens 115, total_tokens 138, spend 0.003289Type
🐛 Bug Fix
Caveats (if any)
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
9209bb6 passes /live-pr-risk