Repository navigation
fix: report real token usage on guardrail-blocked /v1/responses replies - #36907
mateo-berri merged 3 commits into
Conversation
## TLDR Signed-off-by: Ishaan <ishaangupta0408@gmail.com>
|
|
Greptile SummaryThe PR propagates upstream token usage into synthetic guardrail-blocked Responses API replies while preserving zero usage for pre-call blocks.
Confidence Score: 5/5The PR appears safe to merge. No blocking failure remains.
|
| Filename | Overview |
|---|---|
| litellm/integrations/custom_guardrail.py | Extends the passthrough exception helper to carry the blocked upstream response. |
| litellm/llms/base_llm/guardrail_translation/utils.py | Centralizes blocked-response usage extraction and adds Responses API token-shape conversion. |
| litellm/proxy/response_api_endpoints/endpoints.py | Uses original response usage when serializing guardrail-blocked Responses API replies. |
| tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py | Exercises native, bridged-chat, and pre-call usage through the serialized endpoint response. |
| tests/test_litellm/proxy/test_blocked_response_usage.py | Adds focused coverage for usage conversion and propagation through the passthrough exception helper. |
Reviews (2): Last reviewed commit: "Merge remote-tracking branch 'origin/lit..." | Re-trigger Greptile
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Move the blocked-usage mapping for /v1/responses next to blocked_response_usage in guardrail_translation utils, map bridged chat prompt/completion tokens to Responses API input/output tokens, and let raise_passthrough_exception attach the blocked response so post-call guardrail blocks report real usage
…itellm_36907_head
|
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 e06e2e6. Configure here.
|
The added tests execute every new helper line in CI's unit suite; the low codecov patch number reflects coverage upload wiring, not missing tests |
6c2dcb8
into
BerriAI:litellm_internal_staging
TLDR
Problem this solves:
How it solves it:
User Flow
Before: a developer whose app tracks per-request token spend from response bodies gets zeros for every guardrail-blocked Responses API call
{"model": "gpt-4o-mini", "input": "..."}and the reply trips a post-call guardrailusagereads"input_tokens": 0, "output_tokens": 0, "total_tokens": 0After: the same blocked call reports the real token counts
usagenow carries the real counts, e.g."input_tokens": 12, "output_tokens": 8, "total_tokens": 20Relevant issues
Fixes #36880
Linear ticket
Resolves LIT-5615
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
Live proxy, real OpenAI and Anthropic calls, no mocks. Both legs boot the same config: a
post_callcustom guardrail that isdefault_onand raisesModifyResponseExceptionwith the blocked response attached, one native model (openai/gpt-4o-mini) and one chat-bridged model (anthropic/claude-haiku-4-5). Before runs at the merge base2d3c3e3098, after at this PR's heade06e2e6c12. Every call returns 200 carrying the guardrail's blocked message instead of a model reply; only the reported usage differs2d3c3e3098e06e2e6c12Repro setup (config.yaml and the guardrail module)
Before at 2d3c3e3: /v1/responses reports zeros, control endpoints report real usage
{ "id": "resp_2fe68d15-194c-4129-ada8-8fe5b75adb0e", "model": "gpt-4o-mini", "object": "response", "output": [ { "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ] } ], "status": "completed", "usage": { "input_tokens": 0, "input_tokens_details": null, "output_tokens": 0, "output_tokens_details": null, "total_tokens": 0, "cost": null } }{ "id": "resp_2d97f82d-b77e-44a8-a945-894a7cf3b996", "model": "claude-haiku-4-5", "object": "response", "output": [ { "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ] } ], "status": "completed", "usage": { "input_tokens": 0, "input_tokens_details": null, "output_tokens": 0, "output_tokens_details": null, "total_tokens": 0, "cost": null } }{ "id": "msg_28dd94f4-8df5-497c-9e1d-441b31cab61d", "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ], "model": "claude-haiku-4-5", "stop_reason": "end_turn", "usage": { "input_tokens": 12, "output_tokens": 10 } }{ "id": "chatcmpl-b4f56270-ae32-49ac-a178-ad36c2910279", "model": "gpt-4o-mini", "object": "chat.completion", "choices": [ { "finish_reason": "content_filter", "index": 0, "message": { "content": "Content flagged by policy, response withheld", "role": "assistant" } } ], "usage": { "completion_tokens": 7, "prompt_tokens": 12, "total_tokens": 19 } }After at e06e2e6: /v1/responses reports real usage for native and bridged models, control endpoints unchanged
{ "id": "resp_830b71b5-2aa2-4d48-bbf5-cd5913cc3f24", "model": "gpt-4o-mini", "object": "response", "output": [ { "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ] } ], "status": "completed", "usage": { "input_tokens": 12, "input_tokens_details": { "audio_tokens": null, "cached_tokens": 0, "text_tokens": null, "cache_write_tokens": 0 }, "output_tokens": 8, "output_tokens_details": { "reasoning_tokens": 0, "text_tokens": null }, "total_tokens": 20, "cost": null } }{ "id": "resp_9d500821-ad5b-4b88-9afc-fbf0f7ccd788", "model": "claude-haiku-4-5", "object": "response", "output": [ { "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ] } ], "status": "completed", "usage": { "input_tokens": 12, "input_tokens_details": { "audio_tokens": null, "cached_tokens": 0, "text_tokens": 12, "cache_write_tokens": 0 }, "output_tokens": 10, "output_tokens_details": { "reasoning_tokens": 0, "text_tokens": 10 }, "total_tokens": 22, "cost": null } }{ "id": "msg_cf458d37-a442-4fdd-aff4-c0eb0ca838a8", "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "Content flagged by policy, response withheld" } ], "model": "claude-haiku-4-5", "stop_reason": "end_turn", "usage": { "input_tokens": 12, "output_tokens": 10 } }{ "id": "chatcmpl-c8946773-1cc4-41ad-9403-89559d582194", "model": "gpt-4o-mini", "object": "chat.completion", "choices": [ { "finish_reason": "content_filter", "index": 0, "message": { "content": "Content flagged by policy, response withheld", "role": "assistant" } } ], "usage": { "completion_tokens": 7, "prompt_tokens": 12, "total_tokens": 19 } }Observations from the QA run, all pre-existing behaviors this PR leaves alone:
Type
🐛 Bug Fix
Caveats (if any)
Final Attestation