diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.py index 565bf47fc14..9e1230c3895 100644 --- a/litellm/proxy/proxy_server.py +++ b/litellm/proxy/proxy_server.py @@ -1062,11 +1062,18 @@ async def user_api_key_auth( # Check 4. Token Spend is under budget if valid_token.spend is not None and valid_token.max_budget is not None: - # collect information for alerting - if isinstance(user_id_information, list): - user_id_information = user_id_information[0] - user_email = user_id_information.get("user_email", None) + #################################### + # collect information for alerting # + #################################### + + user_email = None + # Check if the token has any user id information + if user_id_information is not None: + if isinstance(user_id_information, list): + user_id_information = user_id_information[0] + user_email = user_id_information.get("user_email", None) + call_info = CallInfo( token=valid_token.token, spend=valid_token.spend, @@ -1083,6 +1090,10 @@ async def user_api_key_auth( ) ) + #################################### + # collect information for alerting # + #################################### + if valid_token.spend >= valid_token.max_budget: raise Exception( f"ExceededTokenBudget: Current spend for token: {valid_token.spend}; Max Budget for Token: {valid_token.max_budget}"