cookbook/LiteLLM_User_Based_Rate_Limits.ipynb
In this notebook we create a $0.0002 daily budget per user and make completion calls using the litellm budget manager
!pip install litellm uuid
import uuid
import os
os.environ['OPENAI_API_KEY'] = ""
This code does the following
litellm.completion() request only if the user is under their budgetfrom litellm import BudgetManager, completion
# Initializes a litellm.BudgetManager()
budget_manager = BudgetManager(project_name="liteLLM_project", client_type="hosted") # see https://docs.litellm.ai/docs/budget_manager
user_id = str(uuid.uuid4()) # create a new user id
daily_budget = 0.0002
# Checks if a budget exists for a user
if not budget_manager.is_valid_user(user_id):
# Creates a $0.0002 budget if the user does not exisr
print(f"No budget exists for user: {user_id}\n")
print(f"Creating a budget for user: {user_id}, daily budget ${daily_budget}\n")
budget_manager.create_budget(total_budget=daily_budget, user=user_id, duration="daily") # duration can be daily, weekly, monthly
# Makes a `litellm.completion()` request only if the user is under their budget
current_spend_for_user = budget_manager.get_current_cost(user=user_id)
budget_for_user = budget_manager.get_total_budget(user_id)
print(f"User: {user_id} has spent ${current_spend_for_user}, budget for user: ${budget_for_user}\n")
if current_spend_for_user <= budget_for_user:
response = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey, how's it going?"}])
budget_manager.update_cost(completion_obj=response, user=user_id)
else:
response = "Sorry - no budget!"
print(response)
user_id = "29af95f8-c3c6-4c8c-b080-8b2d18d25432" # set in the previous cell
for _ in range(10):
# check if a given call can be made
current_spend_for_user = budget_manager.get_current_cost(user=user_id)
budget_for_user = budget_manager.get_total_budget(user_id)
print(f"User: {user_id} has spent ${current_spend_for_user}, budget for user: ${budget_for_user}\n")
if current_spend_for_user <= budget_for_user:
response = completion(model="gpt-3.5-turbo", messages=[{"role": "user", "content": "Hey, how's it going?"}])
budget_manager.update_cost(completion_obj=response, user=user_id)
else:
response = "Sorry - no budget!"
print(f"User: {user_id} has exceeded budget, current spend ${current_spend_for_user}, budget for user: ${budget_for_user}\n")
break # no more requests
# print(response)