fix: populate overhead duration metrics for all route types - #30607
fix: populate overhead duration metrics for all route types#30607factnn wants to merge 20 commits into
Conversation
Greptile SummaryThis PR attempts to fix missing
Confidence Score: 3/5The dict-response branch writes overhead to a dict key that all downstream The core fix for dict-typed responses (the litellm/proxy/common_request_processing.py — both the seed read and the two downstream
|
| Filename | Overview |
|---|---|
| litellm/proxy/common_request_processing.py | Adds inline overhead computation for non-chat-completion routes; the dict-response branch writes to a dict key that downstream getattr readers cannot see, leaving /v1/messages overhead still missing from response headers. |
| tests/test_litellm/litellm_core_utils/llm_response_utils/test_response_metadata.py | Adds pytest import and two new test methods; test_overhead_computed_for_routes_without_pre_existing_value exercises update_response_metadata (pre-existing function, not the new production code path), and test_overhead_guard_skips_when_already_present only verifies Python built-in getattr/bool semantics rather than any production logic. |
Reviews (14): Last reviewed commit: "chore: re-trigger CI" | Re-trigger Greptile
5f99a99 to
8fa63c9
Compare
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
@akhiljavelin Thanks for approving this PR! I noticed the CI lint failure was due to a scope issue with start_time, so I've pushed a fix for it. Could you take another look when you get a chance? If there are any other CI issues that need fixing, please let me know! |
d11b7fd to
aed9c48
Compare
|
Thanks for the PR! A couple of things to get this over the finish line:
Triggering Greptile for a code review in the meantime: |
|
@Sameerlite Hi there, thanks for your review! Besides the lint failure (missing |
be1098b to
30fe9ff
Compare
|
@Sameerlite The lint failure was a missing CI script — the litellm team has now fixed it and it passes. The only remaining failure is proxy-infra, which is pre-existing: it crashes because common_request_processing.py directly accesses logging_obj.litellm_call_id without a None guard at lines 898, 1440, 1629, 1701, 1712, and 2312. None of those lines are touched by this PR. Our update_response_metadata call is guarded by if logging_obj is not None, and internally uses getattr which is None-safe. These same two proxy-infra tests failed when akhiljavelin approved — they were just masked by 5 other test issues I've since resolved. All other tests pass. |
5411744 to
1803003
Compare
|
Thanks for the update and for explaining the pre-existing failures! A couple of things still needed before we can merge:
Triggering a fresh Greptile review on the updated commit: Once those are addressed, we'll take another look! |
2163f1a to
7aa7b02
Compare
@Sameerlite thanks for the review!
|
|
Thanks for the follow-up and for tracking down the root cause! The fix looks solid:
One minor nit: Otherwise this looks good to merge once CI is fully green on the latest commit. Tip: You can customize Greptile's behavior for this repo with |
|
Thanks for the contribution! A couple of things to get this over the finish line:
Once those are addressed, we'll take another look! |
88bd035 to
c0248d1
Compare
PR overviewAll previously flagged issues have been addressed. No open security concerns remain on this pull request. Security reviewNo open security issues remain on this pull request. Fixed/addressed: 1 · PR risk: 0/10 |
b2de52e to
fc0e454
Compare
@Sameerlite |
fc0e454 to
5eeaa9b
Compare
chore(ci): promote internal staging to main
cf188a8 to
6959b74
Compare
chore(ci): promote internal staging to main
bc87138 to
6ecd2df
Compare
|
@Sameerlite is there anything else needed on this one? I've been unable to trigger Greptile re-review via @greptileai as a contributor, would you mind triggering it? |
chore(ci): promote internal staging to main
update_response_metadata was only called in litellm.utils.completion() for /v1/chat/completions. Routes like /v1/messages (Anthropic) and /v1/responses (Responses API) skipped this call. Call update_response_metadata from base_process_llm_request after the LLM response is received, but only when litellm_overhead_time_ms is not already set, so chat completions keep their SDK-level timing. Fixes BerriAI#30566
start_time was not needed for overhead computation on non-chat routes because llm_api_duration_ms is also not available. Use end_time for both start and end; the guard prevents affecting chat completions. Fixes BerriAI#30566
Previously the call passed start_time=end_time, producing _response_ms=0 and negative litellm_overhead_time_ms. Use logging_obj.start_time with end_time fallback instead.
Renamed local logging_obj to _logging_obj and start_time to _start_time to prevent overwriting the function-level logging_obj variable, which caused proxy-infra test failures when self.data had no litellm_logging_obj.
overhead calculation
datetime.now(tz=timezone.utc) is aware but logging_obj.start_time is naive, causing "can't subtract offset-naive and offset-aware datetimes" TypeError. Revert to naive datetime.now(). Also restore test file lost in previous API push.
Chat completions already set litellm_overhead_time_ms in the SDK layer. Skip our overhead calculation for acompletion/completion routes to avoid interfering with object responses.
Only set litellm_overhead_time_ms in _hidden_params without going through update_response_metadata which also touches response_cost and calls the cost calculator.
TypedDict responses (e.g. AnthropicMessagesResponse) are plain dicts at runtime, so hasattr(response, "_hidden_params") returns False. Also assign via response["_hidden_params"] for dict types.
d12ba5b to
75b049d
Compare
|
@greptile-apps review |
1 similar comment
|
@greptile-apps review |

Fixes #30566
update_response_metadata was only called from litellm.utils.completion() for
/v1/chat/completions. Routes like /v1/messages (Anthropic) and /v1/responses
(Responses API) skipped this, so x-litellm-overhead-duration-ms and
litellm_overhead_time_ms were never populated.
Fix: call update_response_metadata from base_process_llm_request after the
LLM response is received, covering all route types.
Type
Bug Fix
Changes
compute overhead via update_response_metadata for all route types