From ee07cb9657f294622f81dc9ce3c6f395a9e07b67 Mon Sep 17 00:00:00 2001 From: Krrish Dholakia Date: Mon, 11 Sep 2023 14:51:01 -0700 Subject: [PATCH] adding budget manager to docs --- docs/my-website/docs/budget_manager.md | 72 ++++++++++++++++++++++++++ docs/my-website/sidebars.js | 3 +- litellm/budget_manager.py | 2 +- 3 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 docs/my-website/docs/budget_manager.md diff --git a/docs/my-website/docs/budget_manager.md b/docs/my-website/docs/budget_manager.md new file mode 100644 index 00000000000..d247e06bcb8 --- /dev/null +++ b/docs/my-website/docs/budget_manager.md @@ -0,0 +1,72 @@ +# 💸 User-based Rate Limiting + +LiteLLM exposes the `BudgetManager` class to help with user-based rate limiting. + +## quick start + +```python +from litellm import BudgetManager, completion + +budget_manager = BudgetManager(project_name="test_project") + +user = "1234" + +# create a budget for a user +budget_manager.create_budget(total_budget=10, user=user) + +# check if a given call can be made +if budget_manager.get_current_cost(user=user) <= budget_manager.get_total_budget(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) +else: + response = "Sorry - no budget!" +``` + +[**Implementation Code**](https://github.com/BerriAI/litellm/blob/main/litellm/budget_manager.py) + +## advanced usage + +BudgetManager creates a dictionary to manage the user budgets, where the key is user and the object is their current cost + model-specific costs. By default this is saved to a local, but you can change it to be stored to a hosted client (either self-hosted or LiteLLM one). + +### get model-breakdown per user + +``` +user = "1234" +# ... +budget_manager.get_model_cost(user=user) # {"gpt-3.5-turbo-0613": 7.3e-05} +``` + +[**Implementation Code**](https://github.com/BerriAI/litellm/blob/817798c692207569a17c26186d10541aa83f04e7/litellm/budget_manager.py#L71) + +### save budget to disk + +When you call `save_data()` it will check for the self.type (by default this is set to client), and save the dictionary to a local `user_cost.json` file. + +```python +# ... +budget_manager.save_data() # 👈 save to user_cost.json() +``` + +[**Implementation Code**](https://github.com/BerriAI/litellm/blob/817798c692207569a17c26186d10541aa83f04e7/litellm/budget_manager.py#L83) + +### save budget to db (LiteLLM) + +Set the BudgetManager type to `client`. +```python +budget_manager = BudgetManager(project_name="test_project", type="client") +# ... +budget_manager.save_data() # 👈 saved to hosted db +``` + +[**Implementation Code**](https://github.com/BerriAI/litellm/blob/817798c692207569a17c26186d10541aa83f04e7/litellm/budget_manager.py#L11) + +### save budget to db (self-hosted) + +Set the BudgetManager type to `client`. Overwrite the api_base +```python +budget_manager = BudgetManager(project_name="test_project", type="client", api_base="your_custom_api") +# ... +budget_manager.save_data() # 👈 saved to self-hosted db +``` + +[**Implementation Code**](https://github.com/BerriAI/litellm/blob/817798c692207569a17c26186d10541aa83f04e7/litellm/budget_manager.py#L11) \ No newline at end of file diff --git a/docs/my-website/sidebars.js b/docs/my-website/sidebars.js index 5476f477ed4..9969630771f 100644 --- a/docs/my-website/sidebars.js +++ b/docs/my-website/sidebars.js @@ -75,7 +75,7 @@ const sidebars = { "token_usage", "exception_mapping", 'debugging/local_debugging', - 'debugging/hosted_debugging', + "budget_manager", { type: 'category', label: 'Tutorials', @@ -121,6 +121,7 @@ const sidebars = { items: [ 'extras/secret', 'extras/contributing', + 'debugging/hosted_debugging', ], }, { diff --git a/litellm/budget_manager.py b/litellm/budget_manager.py index c7f75fa9ffd..63c81d70978 100644 --- a/litellm/budget_manager.py +++ b/litellm/budget_manager.py @@ -5,7 +5,7 @@ import requests from typing import Optional class BudgetManager: - def __init__(self, project_name: str, type: str = "client", api_base: Optional[str] = None): + def __init__(self, project_name: str, type: str = "local", api_base: Optional[str] = None): self.type = type self.project_name = project_name self.api_base = api_base or "https://api.litellm.ai"