Skip to content

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]

perf(spend): avoid duplicate daily agent transaction computation (#21187)

Emerson Gomes06e7bfce2e9f
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py
index dc928921425..9675b82b145 100644
--- a/litellm/proxy/db/db_spend_update_writer.py
+++ b/litellm/proxy/db/db_spend_update_writer.py
@@ -1725,13 +1725,6 @@ class DBSpendUpdateWriter:
"prisma_client is None. Skipping writing spend logs to db."
)
return
- base_daily_transaction = (
- await self._common_add_spend_log_transaction_to_daily_transaction(
- payload, prisma_client, "agent"
- )
- )
- if base_daily_transaction is None:
- return
if payload["agent_id"] is None:
verbose_proxy_logger.debug(
"agent_id is None for request. Skipping incrementing agent spend."
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.py
index 6ccecf59eed..1dd5cba2c4b 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
@@ -756,6 +756,45 @@ async def test_add_spend_log_transaction_to_daily_agent_transaction_injects_agen
assert transaction["custom_llm_provider"] == "openai"
+async def test_add_spend_log_transaction_to_daily_agent_transaction_calls_common_helper_once():
+ writer = DBSpendUpdateWriter()
+ mock_prisma = MagicMock()
+ mock_prisma.get_request_status = MagicMock(return_value="success")
+
+ payload = {
+ "request_id": "req-common-helper",
+ "agent_id": "agent-abc",
+ "user": "test-user",
+ "startTime": "2024-01-01T12:00:00",
+ "api_key": "test-key",
+ "model": "gpt-4",
+ "custom_llm_provider": "openai",
+ "model_group": "gpt-4-group",
+ "prompt_tokens": 12,
+ "completion_tokens": 6,
+ "spend": 0.25,
+ "metadata": '{"usage_object": {}}',
+ }
+
+ writer.daily_agent_spend_update_queue.add_update = AsyncMock()
+ original_common_helper = (
+ writer._common_add_spend_log_transaction_to_daily_transaction
+ )
+ writer._common_add_spend_log_transaction_to_daily_transaction = AsyncMock(
+ wraps=original_common_helper
+ )
+
+ await writer.add_spend_log_transaction_to_daily_agent_transaction(
+ payload=payload,
+ prisma_client=mock_prisma,
+ )
+
+ assert (
+ writer._common_add_spend_log_transaction_to_daily_transaction.await_count == 1
+ )
+
+
@pytest.mark.asyncio
async def test_add_spend_log_transaction_to_daily_agent_transaction_skips_when_agent_id_missing():
"""
@@ -960,4 +999,4 @@ async def test_update_daily_spend_re_raises_exception_after_logging():
entity_id_field="user_id",
table_name="litellm_dailyuserspend",
unique_constraint_name="user_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name_endpoint",
- )
\ No newline at end of file
+ )