Repository navigation
fix(anthropic): log 0 output tokens when an error frame follows message_start - #53914
Conversation
…ge_start A /v1/messages stream that ended with an upstream error event right after message_start logged the message_start placeholder as 1 output token on the failed attempt, while the same stream cut by a dropped connection logged 0. Assembly raised on the error event and fell back to usage-only parsing, which copies the placeholder. The error events are now left out before assembly, so both failure shapes report the same usage and cost Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
|
[Medium impact] The PR appears safe to merge, with no new actionable issues found.
|
| try: | ||
| assembled: Final = AnthropicPassthroughLoggingHandler.build_complete_streaming_response( | ||
| all_chunks=all_chunks, | ||
| all_chunks=anthropic_sse_events_before_upstream_error(all_chunks), |
There was a problem hiding this comment.
Required live proof is missing
This changes how provider errors become logged responses, but the supplied proof uses only synthetic upstreams and unit tests. The repository requires before/after requests through a running proxy to real providers. Add reproducible commands, commit hashes, and actual outputs, with the after run at dd6827970ba6942999030610a0309d17cd70ec9f. This requirement must be satisfied before merging.
Rule Used: Apply this rule only to PRs that fix LLM translation bugs or add LLM translation features. LLM translation means converting requests, responses, streaming events, tool calls, and errors between the API a caller uses on LiteLLM (any endpoint or SDK fu... (source)
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
There was a problem hiding this comment.
Anthropic cannot be made to send an error frame right after message_start on demand, so the proof is the real proxy run against a scripted upstream, rerun at 08fe119 with 39 passed
… split into lines The logging path for a stream that ran to its end hands assembly the SSE lines one by one, with no blank lines between frames, so the frame scan found no error event and returned the lines unchanged. The filter now looks at each event it splits out, so both input shapes drop the error Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
… error-free streams Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
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 529d421. Configure here.
…08_x fix(anthropic): log 0 output tokens when an error frame follows message_start on lts/1.108.x (#53914)
Handwritten 1-3 sentence description
When Anthropic sends an error in the middle of a streamed reply before any text arrived, the failed attempt was logged with one output token that was never generated. It now logs zero, the same as when the connection simply drops at that point, so failed attempts are costed the same way whichever way they fail
Details
Link to Devin session: https://app.devin.ai/sessions/fcfafbe746ed40609bc6395f0854cdbb
Open in Devin Desktop: https://app.devin.ai/desktop/session/fcfafbe746ed40609bc6395f0854cdbb?variant=devin
Requested by: @mateo-berri
TLDR
Problem this solves:
message_startlogged 1 phantom output tokenHow it solves it:
errorevents out before the stream is assembled for loggingUser Flow
Before: a failed attempt that Anthropic ended with an error frame shows one output token nobody received
"stream": trueto an Anthropic model withnum_retries: 1message_startand thenevent: error, and the gateway retries and completes the replyAfter: the same failed attempt shows zero output tokens
"stream": truemessage_startand thenevent: error, and the gateway retries and completes the replyRelevant issues
Follow-up to #53394 and #53885
Pre-Submission checklist
Screenshots / Proof of Fix
Both runs are
tests/integration/run.py providers --workers 2 tests/integration/messages_endpoint/providers/anthropic/test_anthropic_messages_pre_content_retry_wire.pywith this PR's testsBefore (551a18b, product files from main)
1 failed, 38 passedtest_superseded_attempt_and_its_retry_each_log_their_own_usage[recoverable-error-frame]fails withAssertionError: (('failure', 5, 1), ('success', 5, 3))After (529d421)
39 passedMutation: reverting the two files under
litellm/turns the unit tests namedtest_upstream_error_frame_...red in all four of their cases (4 failed), and restoring them turns them green (236 passedfor the file). Reverting only 08fe119's change to the filter turns the two cases that pass the stream as separate lines red (2 failed, 2 passed)Type
🐛 Bug Fix
Caveats (if any)
Low
message_startplaceholder as output. It now logs the output tokens estimated from the text that arrived, matching a dropped connection after content, so those failed attempts cost slightly more in the logs than beforerun-cilabel, so it first runs on main after merge. It was run locally as shown aboveNote
Medium Risk
Adjusts token and cost accounting on Anthropic streaming failure paths used by spend logs and callbacks; behavior for failures after some content may bill slightly more output than before (more accurate).
Overview
Anthropic streaming failures that end with an upstream error SSE frame (e.g. right after
message_start) were logged with a phantom output token from themessage_startplaceholder, while a connection drop at the same point logged 0 output tokens.This change adds
anthropic_sse_events_before_upstream_error, which strips upstream error events before stream assembly, and wires it intoAnthropicPassthroughLoggingHandler._assemble_streaming_responseso partial-failure logging follows the same path for error frames and transport drops. Integration and unit tests now assert matching usage/cost for both failure shapes (including when chunks are split into lines).Reviewed by Cursor Bugbot for commit 529d421. Bugbot is set up for automated code reviews on this repo. Configure here.