From 3623d49c2f0b42de1665625dd48a8de15e1971fd Mon Sep 17 00:00:00 2001 From: Ishaan Jaff Date: Sat, 6 Jul 2024 14:24:29 -0700 Subject: [PATCH] /spend/report view spend for a specific key --- .../spend_management_endpoints.py | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/litellm/proxy/spend_tracking/spend_management_endpoints.py b/litellm/proxy/spend_tracking/spend_management_endpoints.py index 87bd85078cc..6f887cc1bd9 100644 --- a/litellm/proxy/spend_tracking/spend_management_endpoints.py +++ b/litellm/proxy/spend_tracking/spend_management_endpoints.py @@ -821,6 +821,10 @@ async def get_global_spend_report( default="team", description="Group spend by internal team or customer or api_key", ), + api_key: Optional[str] = fastapi.Query( + default=None, + description="View spend for a specific api_key. Example api_key='sk-1234", + ), ): """ Get Daily Spend per Team, based on specific startTime and endTime. Per team, view usage by each key, model @@ -1040,6 +1044,50 @@ async def get_global_spend_report( return [] return db_response + elif api_key is not None: + if api_key.startswith("sk-"): + api_key = hash_token(token=api_key) + sql_query = """ + WITH SpendByModelApiKey AS ( + SELECT + sl.api_key, + sl.model, + SUM(sl.spend) AS model_cost, + SUM(sl.prompt_tokens) AS model_input_tokens, + SUM(sl.completion_tokens) AS model_output_tokens + FROM + "LiteLLM_SpendLogs" sl + WHERE + sl."startTime" BETWEEN $1::date AND $2::date AND sl.api_key = $3 + GROUP BY + sl.api_key, + sl.model + ) + SELECT + api_key, + SUM(model_cost) AS total_cost, + SUM(model_input_tokens) AS total_input_tokens, + SUM(model_output_tokens) AS total_output_tokens, + jsonb_agg(jsonb_build_object( + 'model', model, + 'total_cost', model_cost, + 'total_input_tokens', model_input_tokens, + 'total_output_tokens', model_output_tokens + )) AS model_details + FROM + SpendByModelApiKey + GROUP BY + api_key + ORDER BY + total_cost DESC; + """ + db_response = await prisma_client.db.query_raw( + sql_query, start_date_obj, end_date_obj, api_key + ) + if db_response is None: + return [] + + return db_response except Exception as e: raise HTTPException(