Repository navigation
fix(responses-api): report real usage on guardrail-blocked replies - #36895
Closed
unfitcoder101 wants to merge 1 commit into
Closed
unfitcoder101 wants to merge 1 commit into
unfitcoder101 wants to merge 1 commit into
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
Contributor
|
Thanks for taking this on. The fix landed via #36907, which shares the /v1/messages helper and adds endpoint-level tests, so closing this as superseded |
Author
|
Makes sense — good to see it land either way. Thanks for the update. |
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.
Fixes #36880
Problem
When a guardrail blocks a request/response through the /v1/responses
endpoint, the returned violation message always reports zero usage
(
ResponseAPIUsage(input_tokens=0, output_tokens=0, total_tokens=0)),even for a post-call block where the upstream LLM call already happened
and consumed real tokens. Anything reading usage off that response
(billing, monitoring, budget checks) sees zero cost for a call that
actually cost money.
Root cause
The
except ModifyResponseException as e:handler inlitellm/proxy/response_api_endpoints/endpoints.py hardcodes zero usage
instead of reading
e.original_response, which already carries the realusage when the block happened post-call. The chat completions endpoint
already solves this correctly via
_blocked_response_usage()inproxy_server.py — the Responses API endpoint just never got the
equivalent handling.
Fix
Added
_blocked_responses_api_usage(), mirroring_blocked_response_usage'slogic for the Responses API's usage shape:
original_response is None(pre-call block, no upstream call happened) → zero usageoriginal_response.usagealready Responses-API-shaped (native /v1/responsesupstream call) → passed through directly
original_response.usageChat-Completions-shaped (a bridged call underthe hood) → field-mapped (prompt_tokens→input_tokens,
completion_tokens→output_tokens)
Testing
Added 3 unit tests covering all three branches (None, Responses-shaped,
Chat-shaped) directly against the new helper function. I wasn't able to
run these through the full pytest suite locally — the proxy test conftest
requires a generated
prismaclient, which needs a DB step outside thischange's scope — so I verified the exact same assertions via a standalone
script against the real function instead; all three pass. Happy to run
the full suite if a maintainer can point me to the quickest prisma-generate
path, or if CI covers it directly.