The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
refactor(proxy): share one typed increment for key spend and total_spend writes
yassin6b7cafe92b7d
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.pyindex 5b43ed53117..599ae90bcae 100644--- a/litellm/proxy/db/db_spend_update_writer.py+++ b/litellm/proxy/db/db_spend_update_writer.py@@ -18,6 +18,8 @@ from types import MappingProxyTypefrom typing import TYPE_CHECKING, Any, Final, Literal, Protocol, cast, overloadfrom urllib.parse import quote, unquote+from typing_extensions import ReadOnly, TypedDict+import litellmfrom litellm._logging import verbose_proxy_loggerfrom litellm.caching import RedisCache@@ -109,6 +111,10 @@ def _batch_cost_row_to_write(payload: SpendLogsPayload, disable_spend_logs: boolreturn MappingProxyType({field: value for field, value in payload.items() if field in _BATCH_COST_CLAIM_FIELDS})+class _SpendIncrement(TypedDict):+ increment: ReadOnly[float]++class _SpendBatch(Protocol):litellm_usertable: BatchTablelitellm_verificationtoken: BatchTable@@ -1615,11 +1621,12 @@ class DBSpendUpdateWriter:async with transaction.batch_() as batcher:# Sort by token for consistent lock ordering across pods to prevent deadlocks.for token, response_cost in sorted(key_list_transactions.items()):+ spend_increment: _SpendIncrement = {"increment": response_cost}batcher.litellm_verificationtoken.update_many( # 'update_many' prevents error from being raised if no row existswhere={"token": token},data={- "spend": {"increment": response_cost},- "total_spend": {"increment": response_cost},+ "spend": spend_increment,+ "total_spend": spend_increment,"last_active": datetime.now(timezone.utc),},)diff --git a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py b/tests/test_litellm/proxy/db/test_db_spend_update_writer.pyindex 7be6f809c0d..8f72e1d6248 100644--- a/tests/test_litellm/proxy/db/test_db_spend_update_writer.py+++ b/tests/test_litellm/proxy/db/test_db_spend_update_writer.py@@ -1695,13 +1695,12 @@ async def test_commit_spend_updates_to_db_increments_key_total_spend_alongside_s"agent_list_transactions": {},}- with patch("litellm.proxy.utils._raise_failed_update_spend_exception"):- await db_writer._commit_spend_updates_to_db(- prisma_client=mock_prisma_client,- n_retry_times=0,- proxy_logging_obj=MagicMock(),- db_spend_update_transactions=db_spend_update_transactions,- )+ await db_writer._commit_spend_updates_to_db(+ prisma_client=mock_prisma_client,+ n_retry_times=0,+ proxy_logging_obj=MagicMock(),+ db_spend_update_transactions=db_spend_update_transactions,+ )calls = mock_batcher.litellm_verificationtoken.update_many.call_args_listassert [c.kwargs["where"] for c in calls] == [{"token": "hashed_token_abc"}, {"token": "hashed_token_def"}]