Skip to main content

💰 设置团队预算

跟踪支出,为内部团队设置预算

设置每月团队预算

1. 创建一个团队

  • 设置 max_budget=000000001(团队允许支出的金额)
  • 设置 budget_duration="1d"(预算更新的频率)

创建一个新团队并设置 max_budgetbudget_duration

curl -X POST 'http://0.0.0.0:4000/team/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"team_alias": "QA Prod Bot",
"max_budget": 0.000000001,
"budget_duration": "1d"
}'

响应

{
"team_alias": "QA Prod Bot",
"team_id": "de35b29e-6ca8-4f47-b804-2b79d07aa99a",
"max_budget": 0.0001,
"budget_duration": "1d",
"budget_reset_at": "2024-06-14T22:48:36.594000Z"
}

budget_duration 的可能值

budget_duration预算重置时间
budget_duration="1s"每1秒
budget_duration="1m"每1分钟
budget_duration="1h"每1小时
budget_duration="1d"每1天
budget_duration="1mo"每1个月

2. 为团队创建一个密钥

为团队 QA Prod Botteam_id="de35b29e-6ca8-4f47-b804-2b79d07aa99a" 创建一个密钥,来自步骤1

💡 团队 "QA Prod Bot" 的预算将适用于此团队

curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{"team_id": "de35b29e-6ca8-4f47-b804-2b79d07aa99a"}'

响应

{"team_id":"de35b29e-6ca8-4f47-b804-2b79d07aa99a", "key":"sk-5qtncoYjzRcxMM4bDRktNQ"}

3. 测试它

使用步骤2中的密钥并运行此请求两次

curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Authorization: Bearer sk-mso-JSykEGri86KyOvgxBw' \
-H 'Content-Type: application/json' \
-d ' {
"model": "llama3",
"messages": [
{
"role": "user",
"content": "hi"
}
]
}'

在第二次响应中 - 预期会看到以下异常

{
"error": {
"message": "预算已超出!当前成本: 3.5e-06, 最大预算: 1e-09",
"type": "auth_error",
"param": null,
"code": 400
}
}

高级

剩余预算的 Prometheus 指标

关于 Prometheus 指标的更多信息在这里

你需要在代理的 config.yaml 中包含以下内容

litellm_settings:
success_callback: ["prometheus"]
failure_callback: ["prometheus"]

预期会在 Prometheus 上看到这个指标,以跟踪团队的剩余预算

litellm_remaining_team_budget_metric{team_alias="QA Prod Bot",team_id="de35b29e-6ca8-4f47-b804-2b79d07aa99a"} 9.699999999999992e-06

动态 TPM/RPM 分配

防止项目占用过多 TPM/RPM。

根据该分钟内的活动密钥,动态分配 TPM/RPM 配额给 API 密钥。查看代码

  1. 设置 config.yaml
model_list: 
- model_name: my-fake-model
litellm_params:
model: gpt-3.5-turbo
api_key: my-fake-key
mock_response: hello-world
tpm: 60

litellm_settings:
callbacks: ["dynamic_rate_limiter"]

general_settings:
master_key: sk-1234 # 或者在你的 .env 中设置 `LITELLM_MASTER_KEY=".."`
database_url: postgres://.. # 或者在你的 .env 中设置 `DATABASE_URL=".."`
  1. 启动代理
litellm --config /path/to/config.yaml
  1. 测试它!
"""
- 运行两个并发团队调用相同模型
- 模型有60 TPM(每分钟令牌数)
- 模拟响应返回每次请求30个总令牌
- 每个团队每分钟只能发出一次请求
"""

import requests
from openai import OpenAI, RateLimitError

def create_key(api_key: str, base_url: str):
response = requests.post(
url="{}/key/generate".format(base_url),
json={},
headers={
"Authorization": "Bearer {}".format(api_key)
}
)

_response = response.json()

return _response["key"]

key_1 = create_key(api_key="sk-1234", base_url="http://0.0.0.0:4000")
key_2 = create_key(api_key="sk-1234", base_url="http://0.0.0.0:4000")

# 使用key 1调用代理 - 成功
openai_client_1 = OpenAI(api_key=key_1, base_url="http://0.0.0.0:4000")

response = openai_client_1.chat.completions.with_raw_response.create(
model="my-fake-model", messages=[{"role": "user", "content": "Hello world!"}],
)

print("调用1的响应头 - {}".format(response.headers))
_response = response.parse()
print("调用1的总令牌数 - {}".format(_response.usage.total_tokens))


# 使用key 2调用代理 - 成功
openai_client_2 = OpenAI(api_key=key_2, base_url="http://0.0.0.0:4000")

response = openai_client_2.chat.completions.with_raw_response.create(
model="my-fake-model", messages=[{"role": "user", "content": "Hello world!"}],
)

print("调用2的响应头 - {}".format(response.headers))
_response = response.parse()
print("调用2的总令牌数 - {}".format(_response.usage.total_tokens))
# 使用key 2调用代理 - 失败
try:
openai_client_2.chat.completions.with_raw_response.create(model="my-fake-model", messages=[{"role": "user", "content": "Hey, how's it going?"}])
raise Exception("这应该失败!")
except RateLimitError as e:
print("这被限流了,因为 - {}".format(str(e)))

预期响应

这是由于速率限制导致的 - 错误代码:429 - {'error': {'message': {'error': '键=<哈希化令牌>超出可用TPM=0。模型TPM=0,活跃键=2'}, 'type': 'None', 'param': 'None', 'code': 429}}

[测试版] 设置优先级 / 预留配额

为生产环境中的项目预留TPM/RPM容量。

tip

基于优先级为键预留TPM/RPM的功能是一项高级特性。请获取企业许可证以使用此功能。

  1. 设置config.yaml
model_list:
- model_name: gpt-3.5-turbo
litellm_params:
model: "gpt-3.5-turbo"
api_key: os.environ/OPENAI_API_KEY
rpm: 100

litellm_settings:
callbacks: ["dynamic_rate_limiter"]
priority_reservation: {"dev": 0, "prod": 1}

general_settings:
master_key: sk-1234 # 或在您的.env中设置`LITELLM_MASTER_KEY=".."`
database_url: postgres://.. # 或在您的.env中设置`DATABASE_URL=".."`

priority_reservation:

  • Dict[str, float]
    • str: 可以是任何字符串
    • float: 从0到1。指定为该优先级的键预留的TPM/RPM百分比。

启动代理

litellm --config /path/to/config.yaml
  1. 创建具有该优先级的键
curl -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer <your-master-key>' \
-H 'Content-Type: application/json' \
-D '{
"metadata": {"priority": "dev"} # 👈 键更改
}'

预期响应

{
...
"key": "sk-.."
}
  1. 测试一下!
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: sk-...' \ # 👈 来自步骤2的键
-D '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "你是什么llm"
}
],
}'

预期响应

键=...超出可用RPM=0。模型RPM=100,活跃键=None