Repository navigation
feat: add model leaderboard page #43649
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
6bdac6f
feat(proxy): add model leaderboard analytics
ishaanj-berri b1e9192
feat: add model insights task and range constants
ishaanj-berri 4f39c7d
feat: record task type from task tags in model usage rollup
ishaanj-berri 558a3fc
feat: serve 365 days of model insights by UTC date
ishaanj-berri 08ebc0c
test: cover task tag resolution in model usage rollup
ishaanj-berri b9ba2d9
test: update model insights range limit test to 365 days
ishaanj-berri 631286b
chore: regenerate dashboard api types for model insights
ishaanj-berri a9d7b87
feat: add model insights aggregation helpers
ishaanj-berri c18ed82
test: cover model insights aggregation helpers
ishaanj-berri 8079554
feat: redesign model leaderboard with stacked bars, treemap and ranking
ishaanj-berri a0a0baa
test: update model leaderboard view test
ishaanj-berri ebcedb6
feat: mark model leaderboard as beta in sidebar
ishaanj-berri ebbac03
chore: sync schema.prisma copies from root
github-actions[bot] 7102e8b
fix: only treat task: prefixed tags as model insight tasks
ishaanj-berri d7d3b90
feat: add metric type for model insights ranking
ishaanj-berri 6c5d38d
fix: rank model insights by selected metric and scope detail queries …
ishaanj-berri faf8bc8
test: plain tags are not model insight tasks
ishaanj-berri c2411be
test: cover metric ranking, deployment scoping and rollup round trip
ishaanj-berri e648296
fix: build model insights weeks and halves from the requested date range
ishaanj-berri d5fa1c8
test: cover empty weeks and range-based change comparison
ishaanj-berri d7bd5a6
fix: refetch by metric, show load errors and ignore stale responses
ishaanj-berri 4848d64
test: cover metric refetch and error state
ishaanj-berri b87e4ae
feat: define model insight tasks in a JSON file
ishaanj-berri f37f5d6
feat: return task labels and categories from model insights
ishaanj-berri 641a5b2
feat: load model insight tasks from JSON
ishaanj-berri b515b41
refactor: validate rollup task tags against the JSON task list
ishaanj-berri 48aff5f
feat: serve the task list with model insights
ishaanj-berri 8add5af
refactor: drop hardcoded task list from constants
ishaanj-berri 8fe05c5
build: ship model insight tasks JSON in the wheel
ishaanj-berri 8153733
test: cover model insight task JSON
ishaanj-berri 7e1b592
refactor: take task labels and categories from the API
ishaanj-berri cbbe04a
test: pass task info to task tile builder
ishaanj-berri f1c5eec
refactor: color treemap by API-provided category
ishaanj-berri ee3bff1
test: include tasks in model leaderboard fixture
ishaanj-berri f26cc48
fix: make daily model usage migration idempotent
ishaanj-berri 337b1b0
feat: bound the model insights task query size
ishaanj-berri aed0bdf
fix: compute task breakdown independent of the chart metric
ishaanj-berri c569893
test: task breakdown is stable across chart metrics
ishaanj-berri f89746e
chore: regenerate lazy openapi snapshot for model insights
ishaanj-berri 1ee8fbf
chore: regenerate dashboard api types for model insights
ishaanj-berri 9226ad0
fix: keep previous ranking dimmed while a new metric loads
ishaanj-berri bb290ed
test: cover stale metric state in model leaderboard
ishaanj-berri 3cb62e3
refactor: drop task row cap constant
ishaanj-berri f933406
fix: return the full task breakdown instead of a truncated one
ishaanj-berri 8223bab
test: task query is not truncated
ishaanj-berri c006b28
feat: add task summary types for model insights
ishaanj-berri 742b3e3
feat: summarise tasks server-side on a separate model insights endpoint
ishaanj-berri a82ae63
test: cover the model insights tasks endpoint
ishaanj-berri 511e773
chore: regenerate lazy openapi snapshot for model insights tasks
ishaanj-berri 0eedc5f
chore: regenerate dashboard api types for model insights tasks
ishaanj-berri 24d7f02
refactor: drop client-side task aggregation
ishaanj-berri ac4f5fa
test: remove client-side task aggregation tests
ishaanj-berri c5e4341
feat: load task breakdown separately from the chart metric
ishaanj-berri e795e50
test: task breakdown is not refetched on chart metric change
ishaanj-berri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...extras/litellm_proxy_extras/migrations/20260928000000_add_daily_model_usage/migration.sql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| CREATE TABLE "LiteLLM_DailyModelUsage" ( | ||
| "date" TEXT NOT NULL, | ||
| "model_group" TEXT NOT NULL, | ||
| "model" TEXT NOT NULL, | ||
| "custom_llm_provider" TEXT NOT NULL, | ||
| "task_type" TEXT NOT NULL, | ||
| "spend" DOUBLE PRECISION NOT NULL DEFAULT 0.0, | ||
| "prompt_tokens" BIGINT NOT NULL DEFAULT 0, | ||
| "completion_tokens" BIGINT NOT NULL DEFAULT 0, | ||
| "request_count" BIGINT NOT NULL DEFAULT 0, | ||
| "successful_requests" BIGINT NOT NULL DEFAULT 0, | ||
| "failed_requests" BIGINT NOT NULL DEFAULT 0, | ||
| "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, | ||
| "updated_at" TIMESTAMP(3) NOT NULL, | ||
| CONSTRAINT "LiteLLM_DailyModelUsage_pkey" PRIMARY KEY ("date", "model_group", "model", "custom_llm_provider", "task_type") | ||
| ); | ||
|
|
||
| CREATE INDEX "LiteLLM_DailyModelUsage_date_idx" ON "LiteLLM_DailyModelUsage"("date"); | ||
| CREATE INDEX "LiteLLM_DailyModelUsage_model_group_idx" ON "LiteLLM_DailyModelUsage"("model_group"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| from datetime import datetime | ||
| from typing import Final | ||
|
|
||
| from pydantic import TypeAdapter, ValidationError | ||
|
|
||
| from litellm.constants import ( | ||
| INTERNAL_CALL_ORIGIN_METADATA_KEY, | ||
| MODEL_INSIGHTS_DEFAULT_TASK, | ||
| MODEL_INSIGHTS_TASK_TAG_PREFIX, | ||
| MODEL_INSIGHTS_TASK_TYPES, | ||
| ) | ||
| from litellm.proxy._types import SpendLogsPayload | ||
| from litellm.proxy.utils import PrismaClient | ||
| from litellm.repositories.table_repositories import DailyModelUsageRepository | ||
|
|
||
| _METADATA: Final = TypeAdapter(dict[str, object]) | ||
| _TAGS: Final = TypeAdapter(list[object]) | ||
|
|
||
|
|
||
| def model_usage_task_type(request_tags: str) -> str: | ||
| try: | ||
| tags: Final = _TAGS.validate_json(request_tags) | ||
| except ValidationError: | ||
| return MODEL_INSIGHTS_DEFAULT_TASK | ||
| for tag in tags: | ||
| task = tag.removeprefix(MODEL_INSIGHTS_TASK_TAG_PREFIX) if isinstance(tag, str) else None | ||
| if task in MODEL_INSIGHTS_TASK_TYPES: | ||
| return task | ||
| return MODEL_INSIGHTS_DEFAULT_TASK | ||
|
|
||
|
|
||
| def _is_internal_call(metadata: str) -> bool: | ||
| try: | ||
| decoded: Final = _METADATA.validate_json(metadata) | ||
| except ValidationError: | ||
| return False | ||
| return bool(decoded.get(INTERNAL_CALL_ORIGIN_METADATA_KEY)) | ||
|
|
||
|
|
||
| def _date_from_start_time(start_time: datetime | str) -> str | None: | ||
| if isinstance(start_time, datetime): | ||
| return start_time.date().isoformat() | ||
| return start_time[:10] if len(start_time) >= 10 else None | ||
|
|
||
|
|
||
| async def increment_daily_model_usage(prisma_client: PrismaClient, payload: SpendLogsPayload) -> None: | ||
| date: Final = _date_from_start_time(payload["startTime"]) | ||
| if date is None or _is_internal_call(payload["metadata"]): | ||
| return | ||
|
|
||
| model: Final = payload["model"] or "unknown" | ||
| model_group: Final = payload["model_group"] or model | ||
| provider: Final = payload["custom_llm_provider"] or "unknown" | ||
| task_type: Final = model_usage_task_type(payload["request_tags"]) | ||
| successful: Final = 1 if payload["status"] == "success" else 0 | ||
| failed: Final = 1 - successful | ||
| key: Final = { | ||
| "date": date, | ||
| "model_group": model_group, | ||
| "model": model, | ||
| "custom_llm_provider": provider, | ||
| "task_type": task_type, | ||
| } | ||
| await DailyModelUsageRepository(prisma_client).table.upsert( | ||
| where={"date_model_group_model_custom_llm_provider_task_type": key}, | ||
| data={ | ||
| "create": { | ||
| **key, | ||
| "spend": payload["spend"], | ||
| "prompt_tokens": payload["prompt_tokens"], | ||
| "completion_tokens": payload["completion_tokens"], | ||
| "request_count": 1, | ||
| "successful_requests": successful, | ||
| "failed_requests": failed, | ||
| }, | ||
| "update": { | ||
| "spend": {"increment": payload["spend"]}, | ||
| "prompt_tokens": {"increment": payload["prompt_tokens"]}, | ||
| "completion_tokens": {"increment": payload["completion_tokens"]}, | ||
| "request_count": {"increment": 1}, | ||
| "successful_requests": {"increment": successful}, | ||
| "failed_requests": {"increment": failed}, | ||
| }, | ||
| }, | ||
| ) | ||
161 changes: 161 additions & 0 deletions
161
litellm/proxy/management_endpoints/model_insights_endpoints.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| from collections.abc import Mapping | ||
| from datetime import date, datetime, timedelta, timezone | ||
| from typing import Annotated, Final | ||
|
|
||
| from fastapi import APIRouter, Depends, HTTPException, Query | ||
| from pydantic import BaseModel, Field, TypeAdapter | ||
|
|
||
| from litellm.constants import MODEL_INSIGHTS_MAX_RANGE_DAYS, MODEL_INSIGHTS_TOP_MODELS | ||
| from litellm.proxy._types import CommonProxyErrors, LitellmUserRoles, UserAPIKeyAuth | ||
| from litellm.proxy.auth.user_api_key_auth import user_api_key_auth | ||
| from litellm.repositories.table_repositories import DailyModelUsageRepository | ||
| from litellm.types.model_insights import ( | ||
| ModelInsightDailyMetric, | ||
| ModelInsightMetric, | ||
| ModelInsightsResponse, | ||
| ModelInsightTaskMetric, | ||
| ) | ||
|
|
||
| router: Final = APIRouter() | ||
|
|
||
|
|
||
| class _Sums(BaseModel): | ||
| spend: float = 0.0 | ||
| prompt_tokens: int = 0 | ||
| completion_tokens: int = 0 | ||
| request_count: int = 0 | ||
| successful_requests: int = 0 | ||
| failed_requests: int = 0 | ||
|
|
||
|
|
||
| class _GroupedModel(BaseModel): | ||
| model_group: str | ||
| model: str | ||
| custom_llm_provider: str | ||
| sums: _Sums = Field(alias="_sum") | ||
|
|
||
|
|
||
| class _GroupedDaily(_GroupedModel): | ||
| date: str | ||
|
|
||
|
|
||
| class _GroupedTask(_GroupedModel): | ||
| task_type: str | ||
|
|
||
|
|
||
| _MODEL_ROWS: Final = TypeAdapter(list[_GroupedModel]) | ||
| _DAILY_ROWS: Final = TypeAdapter(list[_GroupedDaily]) | ||
| _TASK_ROWS: Final = TypeAdapter(list[_GroupedTask]) | ||
| _SUM_FIELDS: Final = { | ||
| "spend": True, | ||
| "prompt_tokens": True, | ||
| "completion_tokens": True, | ||
| "request_count": True, | ||
| "successful_requests": True, | ||
| "failed_requests": True, | ||
| } | ||
|
|
||
|
|
||
| def _parse_date(value: str | None, fallback: date) -> date: | ||
| if value is None: | ||
| return fallback | ||
| try: | ||
| return date.fromisoformat(value) | ||
| except ValueError as exc: | ||
| raise HTTPException(status_code=400, detail="Dates must use YYYY-MM-DD") from exc | ||
|
|
||
|
|
||
| def _metric(row: _GroupedModel) -> ModelInsightMetric: | ||
| return ModelInsightMetric( | ||
| model_group=row.model_group, | ||
| model=row.model, | ||
| provider=row.custom_llm_provider, | ||
| spend=row.sums.spend, | ||
| prompt_tokens=row.sums.prompt_tokens, | ||
| completion_tokens=row.sums.completion_tokens, | ||
| requests=row.sums.request_count, | ||
| successful_requests=row.sums.successful_requests, | ||
| failed_requests=row.sums.failed_requests, | ||
| ) | ||
|
|
||
|
|
||
| def _total_tokens(row: _GroupedModel) -> int: | ||
| return row.sums.prompt_tokens + row.sums.completion_tokens | ||
|
|
||
|
|
||
| def _top_model_rows(rows: list[_GroupedModel]) -> list[_GroupedModel]: | ||
| return sorted(rows, key=_total_tokens, reverse=True)[:MODEL_INSIGHTS_TOP_MODELS] | ||
|
|
||
|
|
||
| def _daily_metric(row: _GroupedDaily) -> ModelInsightDailyMetric: | ||
| return ModelInsightDailyMetric(date=row.date, **_metric(row).model_dump()) | ||
|
|
||
|
|
||
| def _task_metric(row: _GroupedTask) -> ModelInsightTaskMetric: | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| return ModelInsightTaskMetric(task_type=row.task_type, **_metric(row).model_dump()) | ||
|
|
||
|
|
||
| @router.get( | ||
| "/model-insights", | ||
| tags=["model insights"], | ||
| dependencies=[Depends(user_api_key_auth)], | ||
| response_model=ModelInsightsResponse, | ||
| ) | ||
| async def get_model_insights( | ||
| user_api_key_dict: Annotated[UserAPIKeyAuth, Depends(user_api_key_auth)], | ||
| start_date: Annotated[str | None, Query(description="YYYY-MM-DD, defaults to 365 days ago")] = None, | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| end_date: Annotated[str | None, Query(description="YYYY-MM-DD, defaults to today")] = None, | ||
| ) -> ModelInsightsResponse: | ||
| from litellm.proxy.proxy_server import prisma_client | ||
|
|
||
| if user_api_key_dict.user_role not in (LitellmUserRoles.PROXY_ADMIN, LitellmUserRoles.PROXY_ADMIN_VIEW_ONLY): | ||
| raise HTTPException(status_code=403, detail="Only proxy admins can view deployment-wide model insights") | ||
| if prisma_client is None: | ||
| raise HTTPException(status_code=500, detail=CommonProxyErrors.db_not_connected_error.value) | ||
|
|
||
| end_day: Final = _parse_date(end_date, datetime.now(timezone.utc).date()) | ||
| start_day: Final = _parse_date(start_date, end_day - timedelta(days=MODEL_INSIGHTS_MAX_RANGE_DAYS - 1)) | ||
| if start_day > end_day or (end_day - start_day).days >= MODEL_INSIGHTS_MAX_RANGE_DAYS: | ||
| raise HTTPException( | ||
| status_code=400, detail=f"Date range must be between 1 and {MODEL_INSIGHTS_MAX_RANGE_DAYS} days" | ||
| ) | ||
|
|
||
| date_window: Final[Mapping[str, object]] = {"date": {"gte": start_day.isoformat(), "lte": end_day.isoformat()}} | ||
| table: Final = DailyModelUsageRepository(prisma_client).table | ||
| grouped_model_rows: Final = _MODEL_ROWS.validate_python( | ||
| await table.group_by( | ||
| by=["model_group", "model", "custom_llm_provider"], | ||
| sum=_SUM_FIELDS, | ||
| where=date_window, | ||
| ) | ||
| ) | ||
| model_rows: Final = _top_model_rows(grouped_model_rows) | ||
| selected_models: Final = [row.model_group for row in model_rows] | ||
| selected_window: Final = {**date_window, "model_group": {"in": selected_models}} | ||
| daily_rows: Final = _DAILY_ROWS.validate_python( | ||
|
greptile-apps[bot] marked this conversation as resolved.
Resolved
|
||
| await table.group_by( | ||
| by=["date", "model_group", "model", "custom_llm_provider"], | ||
| sum=_SUM_FIELDS, | ||
| where=selected_window, | ||
| order={"date": "asc"}, | ||
| ) | ||
| if selected_models | ||
| else [] | ||
| ) | ||
| task_rows: Final = _TASK_ROWS.validate_python( | ||
| await table.group_by( | ||
| by=["task_type", "model_group", "model", "custom_llm_provider"], | ||
| sum=_SUM_FIELDS, | ||
| where=selected_window, | ||
| order={"_sum": {"completion_tokens": "desc"}}, | ||
| ) | ||
| if selected_models | ||
| else [] | ||
| ) | ||
| return ModelInsightsResponse( | ||
| start_date=start_day.isoformat(), | ||
| end_date=end_day.isoformat(), | ||
| top_models=[_metric(row) for row in model_rows], | ||
| daily=[_daily_metric(row) for row in daily_rows], | ||
| by_task=[_task_metric(row) for row in task_rows], | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.