Repository navigation
fix(guardrails): instrument during-call and post-call guardrail latency - #31414
yassin-berriai merged 1 commit into
Conversation
litellm_guardrail_latency_seconds was only emitted for pre-call guardrails. during_call_hook and post_call_success_hook ran guardrails without recording any latency, so during-call and post-call guardrail time was invisible in the metric and leaked into litellm_overhead_latency_metric, making the documented "subtract guardrail latency from overhead" workaround under-report total guardrail time. Extract the find-the-PrometheusLogger-and-record step into _emit_guardrail_metrics and add _run_guardrail_with_metrics, a single wrapper that times a guardrail coroutine, classifies its outcome (success / intervened / error), enriches any raised HTTPException, and records the latency under the given hook_type. Route the pre-call emit, during_call_hook, and post_call_success_hook through it so every guardrail phase contributes to the metric the same way. Resolves LIT-3999
|
|
Greptile SummaryThis PR fixes a gap where
Confidence Score: 5/5Safe to merge — the change is purely additive metric instrumentation; the actual guardrail execution path is unchanged and the exception-enrichment behaviour is preserved identically. Both new static methods are small and well-bounded. No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/proxy/utils.py | Extracts _emit_guardrail_metrics helper and introduces _run_guardrail_with_metrics wrapper that times a guardrail coroutine and records latency; wires during_call_hook and post_call_success_hook through the new wrapper so all three hook phases now emit litellm_guardrail_latency_seconds. Logic is correct and consistent with the existing pre-call timing path. |
| tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py | Updates renamed method reference, extends existing success/error unit tests to assert metric recording, and adds two end-to-end regression tests for during_call_hook and post_call_success_hook with a stub PrometheusLogger verifying the emitted hook_type label. Tests are mock-only with no network calls. |
Reviews (1): Last reviewed commit: "fix(guardrails): instrument during-call ..." | Re-trigger Greptile
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Greptile SummaryThis PR fixes a gap where
Confidence Score: 5/5Safe to merge — the change is additive (adds metric emission where none existed) and the existing pre-call guardrail flow is unchanged apart from the inline loop being extracted to a shared helper. The refactor is tightly scoped: three call sites updated to use the new wrapper, no changes to the guardrail execution logic itself, and the wrapper's No files require special attention.
|
| Filename | Overview |
|---|---|
| litellm/proxy/utils.py | Extracts _emit_guardrail_metrics helper, renames _run_guardrail_task_with_enrichment to _run_guardrail_with_metrics adding timing + Prometheus emission, and wires during_call_hook and post_call_success_hook through the new wrapper so all three phases emit litellm_guardrail_latency_seconds. |
| tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py | Updates renamed test helpers, adds metric-emission assertions to existing tests, and adds two new end-to-end regression tests that verify during_call_hook and post_call_success_hook each record a hook_type-labelled latency metric via a stub PrometheusLogger. |
Reviews (2): Last reviewed commit: "fix(guardrails): instrument during-call ..." | Re-trigger Greptile
Relevant issues
Resolves LIT-3999
Linear ticket
LIT-3999
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
@greptileaiand received a Confidence Score of at least 4/5 before requesting a maintainer review (Greptile scored 5/5)Screenshots / Proof of Fix
Local proxy with three guardrails of the same pass-through class attached in
pre_call,during_call, andpost_callmode, prometheus enabled, hitting a real OpenAI model. The guardrail sleeps a few ms per phase so the latency is visible.config.yamlphase_timing_guardrail.pySend a few real completions, then scrape the metric.
Before (current
litellm_internal_staging); only the pre-call guardrail is instrumented even though all three ranAfter (this PR); every phase contributes
The
_sumseries confirms real time is captured per phase, not just a countType
🐛 Bug Fix
Changes
litellm_guardrail_latency_secondsonly recorded latency for pre-call guardrails.during_call_hook(runningasync_moderation_hook) andpost_call_success_hook(runningasync_post_call_success_hook) executed guardrails without emitting any latency, so during-call and post-call guardrail time was invisible in the metric and double-counted aslitellm_overhead_latency_metric. That broke the documented workaround of subtracting guardrail latency from overhead to isolate LiteLLM's internal overhead, and it under-reported total guardrail latency.The metric recorder (
_record_guardrail_metrics) already accepted ahook_typelabel forpre_call,during_call, andpost_call; nothing ever called it for the latter two. This pulls the "find the PrometheusLogger and record" step out into_emit_guardrail_metricsand adds_run_guardrail_with_metrics, one wrapper that times a guardrail coroutine, classifies the outcome (success / intervened / error), enriches a raisedHTTPExceptionwith the guardrail context, and records the latency under the suppliedhook_type. The pre-call emit,during_call_hook, andpost_call_success_hookall route through it, so each phase is instrumented identically and the duplicated enrichment try/except blocks in the post-call loop collapse into the shared path.Scope is limited to the non-streaming during-call and post-call dispatch the ticket calls out. The streaming post-call guardrail path (
async_post_call_streaming_hook/async_post_call_streaming_iterator_hook) accumulates latency per chunk and is a separate mechanism; it is left for a follow-up rather than widened here.Tests extend the mapped
tests/test_litellm/proxy/utils/proxy_logging/test_guardrail_pipeline.py._run_guardrail_with_metricsis covered for the success and error paths, andduring_call_hook/post_call_success_hookare driven end to end with a stub PrometheusLogger asserting the recordedhook_type. A surgical mutation that revertsduring_call_hookto bypass the wrapper makestest_during_call_hook_records_latency_metricfail, confirming the regression bites.