Repository navigation
fix(proxy): keep call_type and request start time on failed-request spend logs - #40558
Conversation
…pend logs post_call_failure_hook pops litellm_logging_obj before the failure callbacks run, so the spend row built from request_data had a blank call_type and used datetime.now() as the start time. A guardrail-blocked MCP tool call therefore showed up in the Logs page as an LLM row with no call type and a 0s duration. Lift call_type and start_time off the logging object alongside the fields already lifted, and have the DB failure hook prefer the lifted start time. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
…atching a module global Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
🤖 Devin AI EngineerI'll be helping with this pull request! Here's what you should know: ✅ I will automatically:
Note: I can only respond to comments from users who have write access to this repository. ⚙️ Control Options:
|
Greptile SummaryThis PR preserves request timing and call type when failed requests move from the logging object into spend tracking. It also retains internally constructed MCP tool labels instead of replacing them with
Confidence Score: 5/5The PR appears safe to merge, with focused changes and regression coverage for the reported failed-request logging behavior The failure path now preserves datetime-based request timing and call type before removing the logging object, and MCP label normalization remains bounded by length and whitespace checks. No outstanding or newly introduced failures were found
|
| Filename | Overview |
|---|---|
| litellm/proxy/utils.py | Preserves call type and request start time before the failure callback removes the logging object |
| litellm/proxy/hooks/proxy_track_cost_callback.py | Uses preserved failure metadata and adds a dependency-injection seam for the spend writer |
| litellm/proxy/spend_tracking/spend_tracking_utils.py | Recognizes internally generated MCP model labels while retaining failed-model sanitization |
| tests/test_litellm/proxy/test_proxy_utils.py | Adds an end-to-end callback regression test covering MCP call type, model identity, and duration |
| tests/test_litellm/proxy/spend_tracking/test_spend_tracking_utils.py | Extends failed-model normalization coverage to MCP tool labels |
Reviews (2): Last reviewed commit: "Merge branch 'litellm_internal_staging' ..." | Re-trigger Greptile
|
bugbot run |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
There was a problem hiding this comment.
🔍 Devin Review: 1 flag
Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
…lure_spend_log_fields Keep the MCP: tool-name model on failed spend rows now that prompt-shaped failed models are remapped to the placeholder Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
|
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 beb6b90. Configure here.
TLDR
Problem this solves:
unknown-modelHow it solves it:
MCP: <tool>names are kept out of theunknown-modelrewrite for failed rowsUser Flow
Before: an MCP client whose tool call is blocked by a guardrail sees the block, but Logs records it as an anonymous 0s LLM failure
tools/callfordeepwiki-ask_questionand a question containing the blocked wordconfidential"text":"Error: Content blocked: keyword 'confidential' detected","isError":truefailure, modelunknown-model, call type empty, start time equal to end time, so 0s durationAfter: the same blocked call shows up in Logs as a
call_mcp_toolfailure with its real durationtools/callwith the blocked wordconfidential"isError":truefailure, modelMCP: deepwiki-ask_question, call typecall_mcp_tool, and a start time tens of milliseconds before the end timeacompletion,aresponses) and a real duration instead of a blank and 0sRelevant issues
Linear ticket
Resolves LIT-7453
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/test_litellm/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or more@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
Two proxies, each
--num_workers 2, each on its own Postgres database, same config. Before is the merge base checked out in a worktree on port 20453, After is this branch on port 20454. Real OpenAI calls and the real DeepWiki MCP server (https://mcp.deepwiki.com/mcp)Shared config:
Shared driver, run once per port. The MCP leg does
initialize,notifications/initialized, thentools/call, all onPOST /mcpwith themcp-session-idheader from the initialize response. The last step reads the rows back through the same endpoint the Logs page usesBoth sides returned the same user-visible bodies for steps 1 to 11, so only the first is shown per side and the rest are summarised. The
/spend/logs/uitable is the proofBefore (1dc0e36)
Guardrail-blocked MCP tool call
./mcp_call.sh 20453 "Is there anything confidential about the proxy"data: {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Error: Content blocked: keyword 'confidential' detected"}],"isError":true}}Sibling requests (steps 2 to 11)
Content blocked: keyword 'topsecret' detected, step 4 returned the OpenAIInvalid 'max_tokens'error, steps 5, 6, 9 and 11 returned real completions, step 7 returnedInvalid model name passed in model=no-such-modelGET /spend/logs/ui
curl -s "http://localhost:20453/spend/logs/ui?start_date=...&end_date=...&page_size=20" -H 'authorization: Bearer sk-1234' | jq ...msisendTime - startTime. Every failure row has an empty call type and 0ms, and the blocked MCP row (second line) has lost its tool name tounknown-model:After (beb6b90)
Guardrail-blocked MCP tool call
./mcp_call.sh 20454 "Is there anything confidential about the proxy"data: {"jsonrpc":"2.0","id":2,"result":{"content":[{"type":"text","text":"Error: Content blocked: keyword 'confidential' detected"}],"isError":true}}Sibling requests (steps 2 to 11)
GET /spend/logs/ui
curl -s "http://localhost:20454/spend/logs/ui?start_date=...&end_date=...&page_size=20" -H 'authorization: Bearer sk-1234' | jq ...call_mcp_toolonMCP: deepwiki-ask_questionat 41ms, every other failure row has its call type and a real duration, and theno-such-modelrow still gets staging'sunknown-modelrename:Observations from the run:
/v1/messagesrow saysacompletion, notanthropic_messages. Pre-existing, unchanged/v1/responsesrows now flush the spend queue at once. Harmlesslist_mcp_toolsrows in After: that proxy had already cached the DeepWiki tool list from an earlier call on both workers, Before was freshly started. Same caching on both sidesAdmin UI Logs page, driven headfully on the same two proxies, is in the PR comments with the before/after row detail screenshots (first run at 6bb60f3 / 9108177: #40558 (comment), rerun after the staging merge linked in the newest comment)
Type
🐛 Bug Fix
Caveats (if any)
Low
/v1/messagesrequests logacompletionas call type, from before this change/v1/responsesrows now take the immediate spend log flush pathFinal Attestation
Link to Devin session: https://app.devin.ai/sessions/54bb4692797f4f4baeb77eb9322e3b7c
Open in Devin Desktop: https://app.devin.ai/desktop/session/54bb4692797f4f4baeb77eb9322e3b7c?variant=devin
Requested by: @yucheng-berri
Note
Low Risk
Changes are limited to failure-path spend logging and MCP model naming in logs; no auth or billing logic changes beyond more accurate metadata on failure rows.
Overview
Fixes failed-request spend logs showing blank
call_type, 0s duration, and MCP tools mislabeled asunknown-modelwhen failures happen afterlitellm_logging_objis stripped fromrequest_data.post_call_failure_hooknow liftscall_typeandstart_timefrom the logging object (via_failure_fields_to_lift) before callbacks run;_ProxyDBLoggerprefers that lifted start time when writing failure rows._looks_like_model_nametreatsMCP: …tool names as valid models using a new prefix constant so guardrail-blocked MCP failures keep the tool name._ProxyDBLoggeralso accepts an injectablespend_writerfor tests.Reviewed by Cursor Bugbot for commit beb6b90. Bugbot is set up for automated code reviews on this repo. Configure here.