Skip to content

Commit 9b77559

Browse files
Add aggregate team based usage logging (#10039)
* feat(schema.prisma): initial commit adding aggregate table for team spend allows team spend to be visible at 1m+ logs * feat(db_spend_update_writer.py): support logging aggregate team spend allows usage dashboard to work at 1m+ logs * feat(litellm-proxy-extras/): add new migration file * fix(db_spend_update_writer.py): fix return type * build: bump requirements * fix: fix ruff error
1 parent d3e7a13 commit 9b77559

File tree

15 files changed

+432
-65
lines changed

15 files changed

+432
-65
lines changed
Binary file not shown.
Binary file not shown.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- CreateTable
2+
CREATE TABLE "LiteLLM_DailyTeamSpend" (
3+
"id" TEXT NOT NULL,
4+
"team_id" TEXT NOT NULL,
5+
"date" TEXT NOT NULL,
6+
"api_key" TEXT NOT NULL,
7+
"model" TEXT NOT NULL,
8+
"model_group" TEXT,
9+
"custom_llm_provider" TEXT,
10+
"prompt_tokens" INTEGER NOT NULL DEFAULT 0,
11+
"completion_tokens" INTEGER NOT NULL DEFAULT 0,
12+
"spend" DOUBLE PRECISION NOT NULL DEFAULT 0.0,
13+
"api_requests" INTEGER NOT NULL DEFAULT 0,
14+
"successful_requests" INTEGER NOT NULL DEFAULT 0,
15+
"failed_requests" INTEGER NOT NULL DEFAULT 0,
16+
"created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
17+
"updated_at" TIMESTAMP(3) NOT NULL,
18+
19+
CONSTRAINT "LiteLLM_DailyTeamSpend_pkey" PRIMARY KEY ("id")
20+
);
21+
22+
-- CreateIndex
23+
CREATE INDEX "LiteLLM_DailyTeamSpend_date_idx" ON "LiteLLM_DailyTeamSpend"("date");
24+
25+
-- CreateIndex
26+
CREATE INDEX "LiteLLM_DailyTeamSpend_team_id_idx" ON "LiteLLM_DailyTeamSpend"("team_id");
27+
28+
-- CreateIndex
29+
CREATE INDEX "LiteLLM_DailyTeamSpend_api_key_idx" ON "LiteLLM_DailyTeamSpend"("api_key");
30+
31+
-- CreateIndex
32+
CREATE INDEX "LiteLLM_DailyTeamSpend_model_idx" ON "LiteLLM_DailyTeamSpend"("model");
33+
34+
-- CreateIndex
35+
CREATE UNIQUE INDEX "LiteLLM_DailyTeamSpend_team_id_date_api_key_model_custom_ll_key" ON "LiteLLM_DailyTeamSpend"("team_id", "date", "api_key", "model", "custom_llm_provider");
36+

litellm-proxy-extras/pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "litellm-proxy-extras"
3-
version = "0.1.7"
3+
version = "0.1.8"
44
description = "Additional files for the LiteLLM Proxy. Reduces the size of the main litellm package."
55
authors = ["BerriAI"]
66
readme = "README.md"
@@ -22,7 +22,7 @@ requires = ["poetry-core"]
2222
build-backend = "poetry.core.masonry.api"
2323

2424
[tool.commitizen]
25-
version = "0.1.7"
25+
version = "0.1.8"
2626
version_files = [
2727
"pyproject.toml:version",
2828
"../requirements.txt:litellm-proxy-extras==",

litellm/constants.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
########### v2 Architecture constants for managing writing updates to the database ###########
2525
REDIS_UPDATE_BUFFER_KEY = "litellm_spend_update_buffer"
2626
REDIS_DAILY_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_spend_update_buffer"
27+
REDIS_DAILY_TEAM_SPEND_UPDATE_BUFFER_KEY = "litellm_daily_team_spend_update_buffer"
2728
MAX_REDIS_BUFFER_DEQUEUE_COUNT = 100
2829
MAX_SIZE_IN_MEMORY_QUEUE = 10000
2930
MAX_IN_MEMORY_QUEUE_FLUSH_COUNT = 1000

litellm/proxy/_types.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2769,8 +2769,7 @@ class DefaultInternalUserParams(LiteLLMPydanticObjectBase):
27692769
)
27702770

27712771

2772-
class DailyUserSpendTransaction(TypedDict):
2773-
user_id: str
2772+
class BaseDailySpendTransaction(TypedDict):
27742773
date: str
27752774
api_key: str
27762775
model: str
@@ -2784,6 +2783,14 @@ class DailyUserSpendTransaction(TypedDict):
27842783
failed_requests: int
27852784

27862785

2786+
class DailyTeamSpendTransaction(BaseDailySpendTransaction):
2787+
team_id: str
2788+
2789+
2790+
class DailyUserSpendTransaction(BaseDailySpendTransaction):
2791+
user_id: str
2792+
2793+
27872794
class DBSpendUpdateTransactions(TypedDict):
27882795
"""
27892796
Internal Data Structure for buffering spend updates in Redis or in memory before committing them to the database

0 commit comments

Comments
 (0)