Skip to content

Conversation

@mfittko
Copy link

@mfittko mfittko commented Nov 28, 2025

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

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Screenshot of tests passing

image

All 12 tests in test_db_spend_update_writer.py pass ✅

Type

🐛 Bug Fix

Changes

Problem

The mcp_namespaced_tool_name field 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_spend method.

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_name to the sort key tuple in _update_daily_spend, using the same x[1].get("mcp_namespaced_tool_name") or "" pattern already used for other optional fields.

Files Changed

  • litellm/proxy/db/db_spend_update_writer.py - Added mcp_namespaced_tool_name to sort key
  • tests/test_litellm/proxy/db/test_db_spend_update_writer.py - Added test case with mcp_namespaced_tool_name: None

Adds mcp_namespaced_tool_name to the sort key in _update_daily_spend
to ensure consistent ordering across all indexed fields. This prevents
potential deadlocks when batching upserts.

The mcp_namespaced_tool_name field is part of the unique constraint
and was missing from the sort key, which could cause issues when
this field contains None values.

Also updates the test to include a case for mcp_namespaced_tool_name
with a None value.
@vercel
Copy link

vercel bot commented Nov 28, 2025

@mfittko is attempting to deploy a commit to the CLERKIEAI Team on Vercel.

A member of the Team first needs to authorize it.

@mfittko
Copy link
Author

mfittko commented Nov 28, 2025

Hey @CAFxX 👋

This PR is a follow-up to your excellent deadlock prevention work in #15281.

You mentioned in the original PR that the sort key should include "all fields that have an index" to ensure consistent ordering across concurrent transactions. After MCP tool support was added (via #12397 and #12894), the mcp_namespaced_tool_name field became part of the unique constraint:

@@unique([user_id, date, api_key, model, custom_llm_provider, mcp_namespaced_tool_name], name: "user_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name")

However, it was never added to the sort key in _update_daily_spend, which means transactions involving different mcp_namespaced_tool_name values could still process in non-deterministic order, potentially leading to deadlocks.

This PR simply adds the missing field to the sort key tuple, using the same x[1].get("mcp_namespaced_tool_name") or "" pattern already used for other optional fields.

Would appreciate your review if you have a moment! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant