Skip to content

fix(responses-api): report real usage on guardrail-blocked replies - #36895

Closed
unfitcoder101 wants to merge 1 commit into
BerriAI:mainfrom
unfitcoder101:fix/response-api-guardrail-block-usage
Closed

unfitcoder101 wants to merge 1 commit into
BerriAI:mainfrom
unfitcoder101:fix/response-api-guardrail-block-usage

Conversation

@unfitcoder101

Copy link
Copy Markdown

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 in
litellm/proxy/response_api_endpoints/endpoints.py hardcodes zero usage
instead of reading e.original_response, which already carries the real
usage when the block happened post-call. The chat completions endpoint
already solves this correctly via _blocked_response_usage() in
proxy_server.py — the Responses API endpoint just never got the
equivalent handling.

Fix

Added _blocked_responses_api_usage(), mirroring _blocked_response_usage's
logic for the Responses API's usage shape:

  • original_response is None (pre-call block, no upstream call happened) → zero usage
  • original_response.usage already Responses-API-shaped (native /v1/responses
    upstream call) → passed through directly
  • original_response.usage Chat-Completions-shaped (a bridged call under
    the 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 prisma client, which needs a DB step outside this
change'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.

@codecov

codecov Bot commented Aug 14, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
litellm/proxy/response_api_endpoints/endpoints.py 88.88% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@codspeed

codspeed Bot commented Aug 14, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing unfitcoder101:fix/response-api-guardrail-block-usage (2134d3f) with main (bd0d135)

Open in CodSpeed

@mateo-berri

Copy link
Copy Markdown
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

@unfitcoder101

Copy link
Copy Markdown
Author

Makes sense — good to see it land either way. Thanks for the update.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants