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]

unit testing for SpendUpdateQueue

Ishaan Jaff115946d40245
diff --git a/litellm/proxy/db/spend_update_queue.py b/litellm/proxy/db/spend_update_queue.py
index 29118073efa..fbd96c6c2df 100644
--- a/litellm/proxy/db/spend_update_queue.py
+++ b/litellm/proxy/db/spend_update_queue.py
@@ -1,5 +1,5 @@
import asyncio
-from typing import TYPE_CHECKING, Any, Dict, List
+from typing import TYPE_CHECKING, Any, List
from litellm._logging import verbose_proxy_logger
from litellm.proxy._types import (
@@ -72,14 +72,22 @@ class SpendUpdateQueue:
for update in updates:
entity_type = update.get("entity_type")
- entity_id = update.get("entity_id")
- response_cost = update.get("response_cost")
-
- if entity_type is None or entity_id is None or response_cost is None:
- raise ValueError("Invalid update: %s", update)
+ entity_id = update.get("entity_id") or ""
+ response_cost = update.get("response_cost") or 0
+
+ if entity_type is None:
+ verbose_proxy_logger.debug(
+ "Skipping update spend for update: %s, because entity_type is None",
+ update,
+ )
+ continue
dict_key = entity_type_to_dict_key.get(entity_type)
if dict_key is None:
+ verbose_proxy_logger.debug(
+ "Skipping update spend for update: %s, because entity_type is not in entity_type_to_dict_key",
+ update,
+ )
continue # Skip unknown entity types
# Type-safe access using if/elif statements
@@ -117,6 +125,6 @@ class SpendUpdateQueue:
if entity_id not in transactions_dict:
transactions_dict[entity_id] = 0
- transactions_dict[entity_id] += response_cost
+ transactions_dict[entity_id] += response_cost or 0
return db_spend_update_transactions
diff --git a/tests/litellm/proxy/db/test_spend_update_queue.py b/tests/litellm/proxy/db/test_spend_update_queue.py
new file mode 100644
index 00000000000..89d494a0701
--- /dev/null
+++ b/tests/litellm/proxy/db/test_spend_update_queue.py
@@ -0,0 +1,152 @@
+import asyncio
+import json
+import os
+import sys
+
+import pytest
+from fastapi.testclient import TestClient
+
+from litellm.proxy._types import Litellm_EntityType, SpendUpdateQueueItem
+from litellm.proxy.db.spend_update_queue import SpendUpdateQueue
+
+sys.path.insert(
+ 0, os.path.abspath("../../..")
+) # Adds the parent directory to the system path
+
+
+def spend_queue():
+ return SpendUpdateQueue()
+
+
+async def test_add_update(spend_queue):
+ # Test adding a single update
+ update: SpendUpdateQueueItem = {
+ "entity_type": Litellm_EntityType.USER,
+ "entity_id": "user123",
+ "response_cost": 0.5,
+ }
+ await spend_queue.add_update(update)
+
+ # Verify update was added by checking queue size
+ assert spend_queue.update_queue.qsize() == 1
+
+
+async def test_missing_response_cost(spend_queue):
+ # Test with missing response_cost - should default to 0
+ update: SpendUpdateQueueItem = {
+ "entity_type": Litellm_EntityType.USER,
+ "entity_id": "user123",
+ }
+
+ await spend_queue.add_update(update)
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Should have created entry with 0 cost
+ assert aggregated["user_list_transactions"]["user123"] == 0
+
+
+async def test_missing_entity_id(spend_queue):
+ # Test with missing entity_id - should default to empty string
+ update: SpendUpdateQueueItem = {
+ "entity_type": Litellm_EntityType.USER,
+ "response_cost": 1.0,
+ }
+
+ await spend_queue.add_update(update)
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Should use empty string as key
+ assert aggregated["user_list_transactions"][""] == 1.0
+
+
+async def test_none_values(spend_queue):
+ # Test with None values
+ update: SpendUpdateQueueItem = {
+ "entity_type": Litellm_EntityType.USER,
+ "entity_id": None, # type: ignore
+ "response_cost": None,
+ }
+
+ await spend_queue.add_update(update)
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Should handle None values gracefully
+ assert aggregated["user_list_transactions"][""] == 0
+
+
+async def test_multiple_updates_with_missing_fields(spend_queue):
+ # Test multiple updates with various missing fields
+ updates: list[SpendUpdateQueueItem] = [
+ {
+ "entity_type": Litellm_EntityType.USER,
+ "entity_id": "user123",
+ "response_cost": 0.5,
+ },
+ {
+ "entity_type": Litellm_EntityType.USER,
+ "entity_id": "user123", # missing response_cost
+ },
+ {
+ "entity_type": Litellm_EntityType.USER, # missing entity_id
+ "response_cost": 1.5,
+ },
+ ]
+
+ for update in updates:
+ await spend_queue.add_update(update)
+
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Verify aggregation
+ assert (
+ aggregated["user_list_transactions"]["user123"] == 0.5
+ ) # only the first update with valid cost
+ assert (
+ aggregated["user_list_transactions"][""] == 1.5
+ ) # update with missing entity_id
+
+
+async def test_unknown_entity_type(spend_queue):
+ # Test with unknown entity type
+ update: SpendUpdateQueueItem = {
+ "entity_type": "UNKNOWN_TYPE", # type: ignore
+ "entity_id": "123",
+ "response_cost": 0.5,
+ }
+
+ await spend_queue.add_update(update)
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Should ignore unknown entity type
+ assert all(len(transactions) == 0 for transactions in aggregated.values())
+
+
+async def test_missing_entity_type(spend_queue):
+ # Test with missing entity type
+ update: SpendUpdateQueueItem = {"entity_id": "123", "response_cost": 0.5}
+
+ await spend_queue.add_update(update)
+ aggregated = (
+ await spend_queue.flush_and_get_aggregated_db_spend_update_transactions()
+ )
+
+ # Should ignore updates without entity type
+ assert all(len(transactions) == 0 for transactions in aggregated.values())