设置团队预算
先决条件
- 您必须设置一个 Postgres 数据库(例如 Supabase、Neon 等)
- 要启用团队成员速率限制,请在启动代理服务器之前设置环境变量
EXPERIMENTAL_MULTI_INSTANCE_RATE_LIMITING=true。 否则,团队成员的速率限制将不会被强制执行。
自动生成 JWT 团队的默认预算
在使用 team_id_upsert: true 的 JWT 身份验证时,您可以自动为任何新创建的团队分配默认预算。
这在您的 config.yaml 中的 default_team_settings 中配置。
示例
# in your config.yaml
litellm_jwtauth:
team_id_upsert: true
team_id_jwt_field: "team_id"
# ... other jwt settings
litellm_settings:
default_team_settings:
- team_id: "default-settings"
max_budget: 100.0
跟踪支出,为您的内部团队设置预算
设置月度团队预算
1. 创建一个团队
- 设置
max_budget=000000001(团队允许花费的金额) - 设置
budget_duration="1d"(预算应该更新的频率)
- API
- Admin UI
创建一个新团队并设置 max_budget 和 budget_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" | 每天 |
budget_duration="30d" | 每月 |
2. 为 team 创建一个密钥
为 Team=QA Prod Bot 和来自步骤 1 的 team_id="de35b29e-6ca8-4f47-b804-2b79d07aa99a" 创建一个密钥
- API
- Admin UI
💡 预算将应用于此团队 Team="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 中的密钥并运行此请求两次
- API
- Admin UI
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": "Budget has been exceeded! Current cost: 3.5e-06, Max budget: 1e-09",
"type": "auth_error",
"param": null,
"code": 400
}
}

高级
remaining_budget 的 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