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]

v0 customer usage, pending tests + extras version bump

yuneng-jiang562afb208d58
diff --git a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
index 2883dfc4b82..4d4a127f7e8 100644
--- a/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
+++ b/litellm-proxy-extras/litellm_proxy_extras/schema.prisma
@@ -462,6 +462,34 @@ model LiteLLM_DailyOrganizationSpend {
@@index([mcp_namespaced_tool_name])
}
+// Track daily end user (customer) spend metrics per model and key
+model LiteLLM_DailyEndUserSpend {
+ id String @id @default(uuid())
+ end_user_id String?
+ date String
+ api_key String
+ model String?
+ model_group String?
+ custom_llm_provider String?
+ mcp_namespaced_tool_name String?
+ prompt_tokens BigInt @default(0)
+ completion_tokens BigInt @default(0)
+ cache_read_input_tokens BigInt @default(0)
+ cache_creation_input_tokens BigInt @default(0)
+ spend Float @default(0.0)
+ api_requests BigInt @default(0)
+ successful_requests BigInt @default(0)
+ failed_requests BigInt @default(0)
+ created_at DateTime @default(now())
+ updated_at DateTime @updatedAt
+ @@unique([end_user_id, date, api_key, model, custom_llm_provider, mcp_namespaced_tool_name])
+ @@index([date])
+ @@index([end_user_id])
+ @@index([api_key])
+ @@index([model])
+ @@index([mcp_namespaced_tool_name])
+}
+
// Track daily team spend metrics per model and key
model LiteLLM_DailyTeamSpend {
id String @id @default(uuid())
diff --git a/litellm/constants.py b/litellm/constants.py
index fa9f1d527af..ededd35001f 100644
--- a/litellm/constants.py
+++ b/litellm/constants.py
@@ -149,6 +149,7 @@ REDIS_UPDATE_BUFFER_KEY = "litellm_spend_update_buffer"
REDIS_DAILY_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_spend_update_buffer"
REDIS_DAILY_TEAM_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_team_spend_update_buffer"
REDIS_DAILY_ORG_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_org_spend_update_buffer"
+REDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_end_user_spend_update_buffer"
REDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_tag_spend_update_buffer"
MAX_REDIS_BUFFER_DEQUEUE_COUNT = int(os.getenv("MAX_REDIS_BUFFER_DEQUEUE_COUNT", 100))
MAX_SIZE_IN_MEMORY_QUEUE = int(os.getenv("MAX_SIZE_IN_MEMORY_QUEUE", 10000))
diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py
index 53a8627bc8f..b8731bd2e55 100644
--- a/litellm/proxy/_types.py
+++ b/litellm/proxy/_types.py
@@ -3615,6 +3615,8 @@ class DailyOrganizationSpendTransaction(BaseDailySpendTransaction):
class DailyUserSpendTransaction(BaseDailySpendTransaction):
user_id: str
+class DailyEndUserSpendTransaction(BaseDailySpendTransaction):
+ end_user_id: str
class DailyTagSpendTransaction(BaseDailySpendTransaction):
request_id: Optional[str]
diff --git a/litellm/proxy/db/db_spend_update_writer.py b/litellm/proxy/db/db_spend_update_writer.py
index 6c9289e3ff6..715a6ebd25d 100644
--- a/litellm/proxy/db/db_spend_update_writer.py
+++ b/litellm/proxy/db/db_spend_update_writer.py
@@ -25,6 +25,7 @@ from litellm.proxy._types import (
DailyTagSpendTransaction,
DailyOrganizationSpendTransaction,
DailyTeamSpendTransaction,
+ DailyEndUserSpendTransaction,
DailyUserSpendTransaction,
DBSpendUpdateTransactions,
Litellm_EntityType,
@@ -65,6 +66,7 @@ class DBSpendUpdateWriter:
self.spend_update_queue = SpendUpdateQueue()
self.daily_spend_update_queue = DailySpendUpdateQueue()
self.daily_team_spend_update_queue = DailySpendUpdateQueue()
+ self.daily_end_user_spend_update_queue = DailySpendUpdateQueue()
self.daily_org_spend_update_queue = DailySpendUpdateQueue()
self.daily_tag_spend_update_queue = DailySpendUpdateQueue()
@@ -182,6 +184,13 @@ class DBSpendUpdateWriter:
)
)
+ asyncio.create_task(
+ self.add_spend_log_transaction_to_daily_end_user_transaction(
+ payload=payload,
+ prisma_client=prisma_client,
+ )
+ )
+
asyncio.create_task(
self.add_spend_log_transaction_to_daily_team_transaction(
payload=payload,
@@ -475,6 +484,7 @@ class DBSpendUpdateWriter:
daily_spend_update_queue=self.daily_spend_update_queue,
daily_team_spend_update_queue=self.daily_team_spend_update_queue,
daily_org_spend_update_queue=self.daily_org_spend_update_queue,
+ daily_end_user_spend_update_queue=self.daily_end_user_spend_update_queue,
daily_tag_spend_update_queue=self.daily_tag_spend_update_queue,
)
@@ -538,6 +548,16 @@ class DBSpendUpdateWriter:
proxy_logging_obj=proxy_logging_obj,
daily_spend_transactions=daily_tag_spend_update_transactions,
)
+ daily_end_user_spend_update_transactions = (
+ await self.redis_update_buffer.get_all_daily_end_user_spend_update_transactions_from_redis_buffer()
+ )
+ if daily_end_user_spend_update_transactions is not None:
+ await DBSpendUpdateWriter.update_daily_end_user_spend(
+ n_retry_times=n_retry_times,
+ prisma_client=prisma_client,
+ proxy_logging_obj=proxy_logging_obj,
+ daily_spend_transactions=daily_end_user_spend_update_transactions,
+ )
except Exception as e:
verbose_proxy_logger.error(f"Error committing spend updates: {e}")
finally:
@@ -627,6 +647,20 @@ class DBSpendUpdateWriter:
daily_spend_transactions=daily_tag_spend_update_transactions,
)
+ ################## Daily End-User Spend Update Transactions ##################
+ # Aggregate all in memory daily end-user spend transactions and commit to db
+ daily_end_user_spend_update_transactions = cast(
+ Dict[str, DailyEndUserSpendTransaction],
+ await self.daily_end_user_spend_update_queue.flush_and_get_aggregated_daily_spend_update_transactions(),
+ )
+
+ await DBSpendUpdateWriter.update_daily_end_user_spend(
+ n_retry_times=n_retry_times,
+ prisma_client=prisma_client,
+ proxy_logging_obj=proxy_logging_obj,
+ daily_spend_transactions=daily_end_user_spend_update_transactions,
+ )
+
async def _commit_spend_updates_to_db( # noqa: PLR0915
self,
prisma_client: PrismaClient,
@@ -990,6 +1024,20 @@ class DBSpendUpdateWriter:
) -> None:
...
+ @overload
+ @staticmethod
+ async def _update_daily_spend(
+ n_retry_times: int,
+ prisma_client: PrismaClient,
+ proxy_logging_obj: ProxyLogging,
+ daily_spend_transactions: Dict[str, DailyEndUserSpendTransaction],
+ entity_type: Literal["end_user"],
+ entity_id_field: str,
+ table_name: str,
+ unique_constraint_name: str,
+ ) -> None:
+ ...
+
@overload
@staticmethod
async def _update_daily_spend(
@@ -1015,14 +1063,15 @@ class DBSpendUpdateWriter:
Dict[str, DailyTeamSpendTransaction],
Dict[str, DailyTagSpendTransaction],
Dict[str, DailyOrganizationSpendTransaction],
+ Dict[str, DailyEndUserSpendTransaction],
],
- entity_type: Literal["user", "team", "org", "tag"],
+ entity_type: Literal["user", "team", "org", "tag", "end_user"],
entity_id_field: str,
table_name: str,
unique_constraint_name: str,
) -> None:
"""
- Generic function to update daily spend for any entity type (user, team, org, tag)
+ Generic function to update daily spend for any entity type (user, team, org, tag, end_user)
"""
from litellm.proxy.utils import _raise_failed_update_spend_exception
@@ -1267,6 +1316,27 @@ class DBSpendUpdateWriter:
unique_constraint_name="organization_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name",
)
+ @staticmethod
+ async def update_daily_end_user_spend(
+ n_retry_times: int,
+ prisma_client: PrismaClient,
+ proxy_logging_obj: ProxyLogging,
+ daily_spend_transactions: Dict[str, DailyEndUserSpendTransaction],
+ ):
+ """
+ Batch job to update LiteLLM_DailyEndUserSpend table using in-memory daily_spend_transactions
+ """
+ await DBSpendUpdateWriter._update_daily_spend(
+ n_retry_times=n_retry_times,
+ prisma_client=prisma_client,
+ proxy_logging_obj=proxy_logging_obj,
+ daily_spend_transactions=daily_spend_transactions,
+ entity_type="end_user",
+ entity_id_field="end_user_id",
+ table_name="litellm_dailyenduserspend",
+ unique_constraint_name="end_user_id_date_api_key_model_custom_llm_provider_mcp_namespaced_tool_name",
+ )
+
@staticmethod
async def update_daily_tag_spend(
n_retry_times: int,
@@ -1292,7 +1362,7 @@ class DBSpendUpdateWriter:
self,
payload: Union[dict, SpendLogsPayload],
prisma_client: PrismaClient,
- type: Literal["user", "team", "org", "request_tags"] = "user",
+ type: Literal["user", "team", "org", "request_tags", "end_user"] = "user",
) -> Optional[BaseDailySpendTransaction]:
common_expected_keys = ["startTime", "api_key"]
if type == "user":
@@ -1303,6 +1373,8 @@ class DBSpendUpdateWriter:
expected_keys = ["organization_id", *common_expected_keys]
elif type == "request_tags":
expected_keys = ["request_tags", *common_expected_keys]
+ elif type == "end_user":
+ expected_keys = ["end_user_id", *common_expected_keys]
else:
raise ValueError(f"Invalid type: {type}")
if not all(key in payload for key in expected_keys):
@@ -1474,6 +1546,48 @@ class DBSpendUpdateWriter:
update={daily_transaction_key: daily_transaction}
)
+ async def add_spend_log_transaction_to_daily_end_user_transaction(
+ self,
+ payload: SpendLogsPayload,
+ prisma_client: Optional[PrismaClient] = None,
+ ) -> None:
+ if prisma_client is None:
+ verbose_proxy_logger.debug(
+ "prisma_client is None. Skipping writing spend logs to db."
+ )
+ return
+
+ end_user_id = payload.get("end_user")
+ if end_user_id is None or end_user_id == "":
+ verbose_proxy_logger.debug(
+ "end_user is None or empty for request. Skipping incrementing end user spend."
+ )
+ return
+
+ payload_with_end_user_id = cast(
+ SpendLogsPayload,
+ {
+ **payload,
+ "end_user_id": end_user_id,
+ },
+ )
+
+ base_daily_transaction = (
+ await self._common_add_spend_log_transaction_to_daily_transaction(
+ payload_with_end_user_id, prisma_client, "end_user"
+ )
+ )
+ if base_daily_transaction is None:
+ return
+
+ daily_transaction_key = f"{end_user_id}_{base_daily_transaction['date']}_{payload_with_end_user_id['api_key']}_{payload_with_end_user_id['model']}_{payload_with_end_user_id['custom_llm_provider']}"
+ daily_transaction = DailyEndUserSpendTransaction(
+ end_user_id=end_user_id, **base_daily_transaction
+ )
+ await self.daily_end_user_spend_update_queue.add_update(
+ update={daily_transaction_key: daily_transaction}
+ )
+
async def add_spend_log_transaction_to_daily_tag_transaction(
self,
payload: SpendLogsPayload,
diff --git a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
index 921fd9701bd..e3b20d7266d 100644
--- a/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
+++ b/litellm/proxy/db/db_transaction_queue/redis_update_buffer.py
@@ -16,6 +16,7 @@ from litellm.constants import (
REDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY,
REDIS_DAILY_TEAM_SPEND_UPDATE_BUFFER_KEY,
REDIS_DAILY_ORG_SPEND_UPDATE_BUFFER_KEY,
+ REDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY,
REDIS_UPDATE_BUFFER_KEY,
)
from litellm.litellm_core_utils.safe_json_dumps import safe_dumps
@@ -24,6 +25,7 @@ from litellm.proxy._types import (
DailyTeamSpendTransaction,
DailyUserSpendTransaction,
DailyOrganizationSpendTransaction,
+ DailyEndUserSpendTransaction,
DBSpendUpdateTransactions,
)
from litellm.proxy.db.db_transaction_queue.base_update_queue import service_logger_obj
@@ -107,6 +109,7 @@ class RedisUpdateBuffer:
daily_spend_update_queue: DailySpendUpdateQueue,
daily_team_spend_update_queue: DailySpendUpdateQueue,
daily_org_spend_update_queue: DailySpendUpdateQueue,
+ daily_end_user_spend_update_queue: DailySpendUpdateQueue,
daily_tag_spend_update_queue: DailySpendUpdateQueue,
):
"""
@@ -172,6 +175,9 @@ class RedisUpdateBuffer:
daily_org_spend_update_transactions = (
await daily_org_spend_update_queue.flush_and_get_aggregated_daily_spend_update_transactions()
)
+ daily_end_user_spend_update_transactions = (
+ await daily_end_user_spend_update_queue.flush_and_get_aggregated_daily_spend_update_transactions()
+ )
daily_tag_spend_update_transactions = (
await daily_tag_spend_update_queue.flush_and_get_aggregated_daily_spend_update_transactions()
)
@@ -207,6 +213,12 @@ class RedisUpdateBuffer:
service_type=ServiceTypes.REDIS_DAILY_SPEND_UPDATE_QUEUE,
)
+ await self._store_transactions_in_redis(
+ transactions=daily_end_user_spend_update_transactions,
+ redis_key=REDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY,
+ service_type=ServiceTypes.REDIS_DAILY_END_USER_SPEND_UPDATE_QUEUE,
+ )
+
await self._store_transactions_in_redis(
transactions=daily_tag_spend_update_transactions,
redis_key=REDIS_DAILY_TAG_SPEND_UPDATE_BUFFER_KEY,
@@ -365,6 +377,30 @@ class RedisUpdateBuffer:
),
)
+ async def get_all_daily_end_user_spend_update_transactions_from_redis_buffer(
+ self,
+ ) -> Optional[Dict[str, DailyEndUserSpendTransaction]]:
+ """
+ Gets all the daily end-user spend update transactions from Redis
+ """
+ if self.redis_cache is None:
+ return None
+ list_of_transactions = await self.redis_cache.async_lpop(
+ key=REDIS_DAILY_END_USER_SPEND_UPDATE_BUFFER_KEY,
+ count=MAX_REDIS_BUFFER_DEQUEUE_COUNT,
+ )
+ if list_of_transactions is None:
+ return None
+ list_of_daily_spend_update_transactions = [
+ json.loads(transaction) for transaction in list_of_transactions
+ ]
+ return cast(
+ Dict[str, DailyEndUserSpendTransaction],
+ DailySpendUpdateQueue.get_aggregated_daily_spend_update_transactions(
+ list_of_daily_spend_update_transactions
+ ),
+ )
+
async def get_all_daily_tag_spend_update_transactions_from_redis_buffer(
self,
) -> Optional[Dict[str, DailyTagSpendTransaction]]:
diff --git a/litellm/proxy/management_endpoints/customer_endpoints.py b/litellm/proxy/management_endpoints/customer_endpoints.py
index 3afbbdd5a4b..9ff0fe6e590 100644
--- a/litellm/proxy/management_endpoints/customer_endpoints.py
+++ b/litellm/proxy/management_endpoints/customer_endpoints.py
@@ -20,6 +20,10 @@ from litellm._logging import verbose_proxy_logger
from litellm.proxy._types import *
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.utils import handle_exception_on_proxy
+from litellm.types.proxy.management_endpoints.common_daily_activity import (
+ SpendAnalyticsPaginatedResponse,
+)
+from litellm.proxy.management_endpoints.common_daily_activity import get_daily_activity
router = APIRouter()
@@ -673,4 +677,78 @@ async def list_end_user(
str(e)
)
)
- raise handle_exception_on_proxy(e)
\ No newline at end of file
+ raise handle_exception_on_proxy(e)
+
+ "/customer/daily/activity",
+ tags=["Customer Management"],
+ dependencies=[Depends(user_api_key_auth)],
+ response_model=SpendAnalyticsPaginatedResponse,
+)
+ "/end_user/daily/activity",
+ tags=["Customer Management"],
+ include_in_schema=False,
+ dependencies=[Depends(user_api_key_auth)],
+)
+async def get_customer_daily_activity(
+ end_user_ids: Optional[str] = None,
+ start_date: Optional[str] = None,
+ end_date: Optional[str] = None,
+ model: Optional[str] = None,
+ api_key: Optional[str] = None,
+ page: int = 1,
+ page_size: int = 10,
+ exclude_end_user_ids: Optional[str] = None,
+ user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),
+):
+
+ """
+ Get daily activity for specific organizations or all accessible organizations.
+ """
+ from litellm.proxy.proxy_server import (
+ prisma_client,
+ )
+
+ if prisma_client is None:
+ raise HTTPException(
+ status_code=500,
+ detail={"error": CommonProxyErrors.db_not_connected_error.value},
+ )
+
+ # Parse comma-separated ids
+ end_user_ids_list = end_user_ids.split(",") if end_user_ids else None
+ exclude_end_user_ids_list: Optional[List[str]] = None
+ if exclude_end_user_ids:
+ exclude_end_user_ids_list = (
+ exclude_end_user_ids.split(",") if exclude_end_user_ids else None
+ )
+
+
+ # Fetch organization aliases for metadata
+ where_condition = {}
+ if end_user_ids_list:
+ where_condition["user_id"] = {"in": list(end_user_ids_list)}
+ end_user_aliases = await prisma_client.db.litellm_endusertable.find_many(
+ where=where_condition
+ )
+ end_user_alias_metadata = {
+ e.user_id: {"alias": e.alias}
+ for e in end_user_aliases
+ }
+
+ # Query daily activity for organizations
+ return await get_daily_activity(
+ prisma_client=prisma_client,
+ table_name="litellm_dailyenduserspend",
+ entity_id_field="end_user_id",
+ entity_id=end_user_ids_list,
+ entity_metadata_field=end_user_alias_metadata,
+ exclude_entity_ids=exclude_end_user_ids_list,
+ start_date=start_date,
+ end_date=end_date,
+ model=model,
+ api_key=api_key,
+ page=page,
+ page_size=page_size,
+ )
\ No newline at end of file
diff --git a/litellm/proxy/schema.prisma b/litellm/proxy/schema.prisma
index 2883dfc4b82..4d4a127f7e8 100644
--- a/litellm/proxy/schema.prisma
+++ b/litellm/proxy/schema.prisma
@@ -462,6 +462,34 @@ model LiteLLM_DailyOrganizationSpend {
@@index([mcp_namespaced_tool_name])
}
+// Track daily end user (customer) spend metrics per model and key
+model LiteLLM_DailyEndUserSpend {
+ id String @id @default(uuid())
+ end_user_id String?
+ date String
+ api_key String
+ model String?
+ model_group String?
+ custom_llm_provider String?
+ mcp_namespaced_tool_name String?
+ prompt_tokens BigInt @default(0)
+ completion_tokens BigInt @default(0)
+ cache_read_input_tokens BigInt @default(0)
+ cache_creation_input_tokens BigInt @default(0)
+ spend Float @default(0.0)
+ api_requests BigInt @default(0)
+ successful_requests BigInt @default(0)
+ failed_requests BigInt @default(0)
+ created_at DateTime @default(now())
+ updated_at DateTime @updatedAt
+ @@unique([end_user_id, date, api_key, model, custom_llm_provider, mcp_namespaced_tool_name])
+ @@index([date])
+ @@index([end_user_id])
+ @@index([api_key])
+ @@index([model])
+ @@index([mcp_namespaced_tool_name])
+}
+
// Track daily team spend metrics per model and key
model LiteLLM_DailyTeamSpend {
id String @id @default(uuid())
diff --git a/schema.prisma b/schema.prisma
index 2883dfc4b82..4d4a127f7e8 100644
--- a/schema.prisma
+++ b/schema.prisma
@@ -462,6 +462,34 @@ model LiteLLM_DailyOrganizationSpend {
@@index([mcp_namespaced_tool_name])
}
+// Track daily end user (customer) spend metrics per model and key
+model LiteLLM_DailyEndUserSpend {
+ id String @id @default(uuid())
+ end_user_id String?
+ date String
+ api_key String
+ model String?
+ model_group String?
+ custom_llm_provider String?
+ mcp_namespaced_tool_name String?
+ prompt_tokens BigInt @default(0)
+ completion_tokens BigInt @default(0)
+ cache_read_input_tokens BigInt @default(0)
+ cache_creation_input_tokens BigInt @default(0)
+ spend Float @default(0.0)
+ api_requests BigInt @default(0)
+ successful_requests BigInt @default(0)
+ failed_requests BigInt @default(0)
+ created_at DateTime @default(now())
+ updated_at DateTime @updatedAt
+ @@unique([end_user_id, date, api_key, model, custom_llm_provider, mcp_namespaced_tool_name])
+ @@index([date])
+ @@index([end_user_id])
+ @@index([api_key])
+ @@index([model])
+ @@index([mcp_namespaced_tool_name])
+}
+
// Track daily team spend metrics per model and key
model LiteLLM_DailyTeamSpend {
id String @id @default(uuid())