fix(db): add mcp_namespaced_tool_name to daily spend sort key #17261
+17
−2
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Title
fix(db): add mcp_namespaced_tool_name to daily spend sort key
Relevant issues
Pre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
tests/litellm/directory, Adding at least 1 test is a hard requirement - see detailsmake test-unitScreenshot of tests passing
All 12 tests in
test_db_spend_update_writer.pypass ✅Type
🐛 Bug Fix
Changes
Problem
The
mcp_namespaced_tool_namefield is part of the unique constraint (e.g.,user_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name) but was missing from the sort key in the_update_daily_spendmethod.When transactions with different values for this field are processed in non-deterministic order, it can lead to database deadlocks during concurrent upsert operations.
Solution
Add
mcp_namespaced_tool_nameto the sort key tuple in_update_daily_spend, using the samex[1].get("mcp_namespaced_tool_name") or ""pattern already used for other optional fields.Files Changed
litellm/proxy/db/db_spend_update_writer.py- Addedmcp_namespaced_tool_nameto sort keytests/test_litellm/proxy/db/test_db_spend_update_writer.py- Added test case withmcp_namespaced_tool_name: None