mirror of
https://github.com/tiennm99/litellm.git
synced 2026-07-09 19:10:50 +00:00
perf(spend): avoid duplicate daily agent transaction computation (#21187)
This commit is contained in:
committed by
Sameer Kankute
parent
7ef9083812
commit
06e7bfce2e
@@ -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."
|
||||
|
||||
@@ -756,6 +756,45 @@ async def test_add_spend_log_transaction_to_daily_agent_transaction_injects_agen
|
||||
assert transaction["custom_llm_provider"] == "openai"
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
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",
|
||||
)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user