fix(anthropic): report real token usage on blocked responses#3
Closed
seph-barker wants to merge 1 commit into
Closed
fix(anthropic): report real token usage on blocked responses#3seph-barker wants to merge 1 commit into
seph-barker wants to merge 1 commit into
Conversation
The ModifyResponseException handler in the /v1/messages endpoint synthesizes a "blocked" response with hardcoded usage of zero input and output tokens, despite the request having consumed real input tokens and the block message carrying real content. Compute input_tokens from the original request messages (carried on the exception's request_data) and output_tokens from the block message text via litellm.token_counter. Token counting is best-effort and falls back to zero on failure so a blocked response is always returned. The streaming synthesis path reuses the same response object, so both paths are fixed consistently. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Collaborator
Author
|
Superseded by upstream BerriAI#31217 — opening directly against the main litellm repo. |
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.
The
ModifyResponseExceptionhandler in the/v1/messagesendpoint synthesizes a "blocked" response reporting zero input and output tokens, even though the request consumed real input tokens and the synthetic block message carries real content. Callers relying on usage (billing, quotas, metrics) under-count every blocked response.Compute
input_tokensfrom the original request messages (carried on the exception'srequest_data) andoutput_tokensfrom the block message text vialitellm.token_counter. Counting is best-effort and falls back to zero on failure so a blocked response is always returned. The streaming synthesis path reuses the same response object, so both paths are fixed by one change.Adds tests asserting nonzero, correct counts and graceful fallback. The endpoint's test file passes (5 tests).