预算、速率限制
个人预算:创建没有 team_id 的虚拟密钥,用于设置个人支出限额
团队预算:向虚拟密钥添加 team_id,以利用团队的共享预算
团队成员预算:在团队共享预算内设置个人的支出限额
代理(Agent)预算:为代理设置速率限制(tpm/rpm)和会话级上限(迭代次数、美元预算) 跳转
如果密钥属于某个团队,则应用团队预算,而不是用户的个人预算。
要求
设置预算
全局代理
在代理的所有调用中应用预算
第 1 步。修改 config.yaml
general_settings:
master_key: sk-1234
litellm_settings:
# other litellm settings
max_budget: 0 # (float) sets max budget as $0 USD
budget_duration: 30d # (str) frequency of reset - You can set duration as seconds ("30s"), minutes ("30m"), hours ("30h"), days ("30d").
步骤 2. 启动代理
litellm /path/to/config.yaml
第 3 步。发送测试调用
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Autherization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
}'
团队
您可以
- 向团队添加预算
在此处获取关于在团队中设置、重置预算的分步教程(通过 API 或使用管理 UI)
向团队添加预算
curl --location 'https://:4000/team/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"team_alias": "my-new-team_4",
"members_with_roles": [{"role": "admin", "user_id": "5c4a0aa3-a1e1-43dc-bd87-3c2da8382a3a"}],
"rpm_limit": 99
}'
示例响应
{
"team_alias": "my-new-team_4",
"team_id": "13e83b19-f851-43fe-8e93-f96e21033100",
"admins": [],
"members": [],
"members_with_roles": [
{
"role": "admin",
"user_id": "5c4a0aa3-a1e1-43dc-bd87-3c2da8382a3a"
}
],
"metadata": {},
"tpm_limit": null,
"rpm_limit": 99,
"max_budget": null,
"models": [],
"spend": 0.0,
"max_parallel_requests": null,
"budget_duration": null,
"budget_reset_at": null
}
向团队添加预算周期
budget_duration:预算在指定周期结束时重置。如果不设置,预算永不重置。您可以将周期设置为秒("30s")、分钟("30m")、小时("30h")、天("30d")。
curl 'http://0.0.0.0:4000/team/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"team_alias": "my-new-team_4",
"members_with_roles": [{"role": "admin", "user_id": "5c4a0aa3-a1e1-43dc-bd87-3c2da8382a3a"}],
"budget_duration": "30s",
}'
团队成员
当您想要限制团队内某个用户的支出时,请使用此功能
第 1 步。创建用户
创建一个 user_id=ishaan 的用户
curl --location 'http://0.0.0.0:4000/user/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "ishaan"
}'
第 2 步。将用户添加到现有团队 - 设置 max_budget_in_team
将用户添加到团队时设置 max_budget_in_team。我们使用在第 1 步中设置的相同 user_id
curl -X POST 'http://0.0.0.0:4000/team/member_add' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{"team_id": "e8d1460f-846c-45d7-9b43-55f3cc52ac32", "max_budget_in_team": 0.000000000001, "member": {"role": "user", "user_id": "ishaan"}}'
第 3 步。为第 1 步中的团队成员创建密钥
设置第 1 步中的 user_id=ishaan
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"user_id": "ishaan",
"team_id": "e8d1460f-846c-45d7-9b43-55f3cc52ac32"
}'
来自 /key/generate 的响应
我们在第 4 步中使用此响应中的 key
{"key":"sk-RV-l2BJEZ_LYNChSx2EueQ", "models":[],"spend":0.0,"max_budget":null,"user_id":"ishaan","team_id":"e8d1460f-846c-45d7-9b43-55f3cc52ac32","max_parallel_requests":null,"metadata":{},"tpm_limit":null,"rpm_limit":null,"budget_duration":null,"allowed_cache_controls":[],"soft_budget":null,"key_alias":null,"duration":null,"aliases":{},"config":{},"permissions":{},"model_max_budget":{},"key_name":null,"expires":null,"token_id":null}%
第 4 步。为团队成员发出 /chat/completions 请求
使用第 3 步中的密钥进行此请求。在 2-3 次请求后,预期会看到以下错误:ExceededBudget: Crossed spend within team
curl --location 'https://:4000/chat/completions' \
--header 'Authorization: Bearer sk-RV-l2BJEZ_LYNChSx2EueQ' \
--header 'Content-Type: application/json' \
--data '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "tes4"
}
]
}'
内部用户
为内部用户(密钥所有者)在代理上可以进行的所有调用应用预算。
对于设置了 'team_id' 的密钥,将使用团队预算而不是用户的个人预算。
要为团队内的用户应用预算,请使用团队成员预算。
LiteLLM 提供了一个 /user/new 端点来为此创建预算。
您可以
默认情况下,max_budget 设置为 null,且不会对密钥进行检查
向用户添加预算
curl --location 'https://:4000/user/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{"models": ["azure-models"], "max_budget": 0, "user_id": "krrish3@berri.ai"}'
示例响应
{
"key": "sk-YF2OxDbrgd1y2KgwxmEA2w",
"expires": "2023-12-22T09:53:13.861000Z",
"user_id": "krrish3@berri.ai",
"max_budget": 0.0
}
向用户添加预算周期
budget_duration:预算在指定周期结束时重置。如果不设置,预算永不重置。您可以将周期设置为秒("30s")、分钟("30m")、小时("30h")、天("30d")。
curl 'http://0.0.0.0:4000/user/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"team_id": "core-infra", # [OPTIONAL]
"max_budget": 10,
"budget_duration": "30s",
}'
为现有用户创建新密钥
现在您只需使用该 user_id(例如 krrish3@berri.ai)调用 /key/generate,并且
- 预算检查:此密钥将检查 krrish3@berri.ai 的预算(例如 10 美元)
- 支出跟踪:此密钥的支出也会更新 krrish3@berri.ai 的支出情况
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data '{"models": ["azure-models"], "user_id": "krrish3@berri.ai"}'
虚拟密钥
在密钥上应用预算。
您可以
预期行为
- 每个密钥的成本会自动填充到
LiteLLM_VerificationToken表中 - 密钥超过其
max_budget后,请求将失败 - 如果设置了周期,支出会在周期结束时重置
默认情况下,max_budget 设置为 null,且不会对密钥进行检查
向密钥添加预算
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"team_id": "core-infra", # [OPTIONAL]
"max_budget": 10,
}'
当密钥超过预算时,对 /chat/completions 的示例请求
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <generated-key>' \
--data ' {
"model": "azure-gpt-3.5",
"user": "e09b4da8-ed80-4b05-ac93-e16d9eb56fca",
"messages": [
{
"role": "user",
"content": "respond in 50 lines"
}
],
}'
当密钥超过预算时,来自 /chat/completions 的预期响应
{
"detail":"Authentication Error, ExceededTokenBudget: Current spend for token: 7.2e-05; Max Budget for Token: 2e-07"
}
向密钥添加预算周期
budget_duration:预算在指定周期结束时重置。如果不设置,预算永不重置。您可以将周期设置为秒("30s")、分钟("30m")、小时("30h")、天("30d")。
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"team_id": "core-infra", # [OPTIONAL]
"max_budget": 10,
"budget_duration": "30s",
}'
在密钥上设置多个预算窗口
在同一个密钥上应用多个并发的、不同时间尺度的预算限制——例如,限制一个密钥为 每天 10 美元 且 每月 100 美元。
这有什么用?
单个 budget_duration 窗口无法防止某一天的不当使用耗尽您全月的预算。多个预算窗口允许您
- 阻止当天失控的使用激增,同时仍允许正常的月度支出。
- 为 Claude Code 的发布设置每日护栏(
24h)和月度上限(30d),这样单次高强度会话就不会耗尽全月的预算。 - 在周上限的基础上,为爆发式工作负载增加精细的小时级限制。
请参阅 用户预算文档,了解预算如何在密钥、团队和用户之间工作。
通过 API
将 budget_limits 作为一组 {budget_duration, max_budget} 对象列表传递
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"budget_limits": [
{"budget_duration": "24h", "max_budget": 10},
{"budget_duration": "30d", "max_budget": 100}
]
}'
每个窗口都被独立跟踪并按照各自的时间表重置
budget_duration | 重置 |
|---|---|
1h | 每小时 |
24h | 每天 UTC 时间午夜 |
7d | 每周日 UTC 时间午夜 |
30d | 每月 1 日 UTC 时间午夜 |
通过仪表板
打开 虚拟密钥 → 创建密钥 → 可选设置 → 预算窗口。

点击 + 添加预算窗口 添加一行,从下拉菜单中选择周期,并输入支出上限。

添加第二行以设置不同的时间周期(例如,在每日 10 美元的基础上,每月 100 美元)。

每个窗口都会在输入框下方显示重置时间表,因此始终清楚支出何时重置。

✨ 虚拟密钥(模型特定)
在密钥上应用特定于模型的预算。示例
- 对于
key = "sk-12345",gpt-4o在1d时间周期内的预算为 0.0000001 美元 - 对于
key = "sk-12345",gpt-4o-mini在30d时间周期内的预算为 10 美元
✨ 这是企业版专属功能 在此开始使用企业版
model_max_budget 的规范是 Dict[str, GenericBudgetInfo]
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"model_max_budget": {"gpt-4o": {"budget_limit": "0.0000001", "time_period": "1d"}}
}'
进行测试请求
我们预计第一次请求会成功,而第二次请求会失败,因为我们超过了虚拟密钥上 gpt-4o 的预算
- 成功调用
- 失败调用
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <sk-generated-key>' \
--data ' {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "testing request"
}
]
}
'
预计此请求会失败,因为我们超过了虚拟密钥上 model=gpt-4o 的预算
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer <sk-generated-key>' \
--data ' {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "testing request"
}
]
}
'
失败时的预期响应
{
"error": {
"message": "LiteLLM Virtual Key: 9769f3f6768a199f76cc29xxxx, key_alias: None, exceeded budget for model=gpt-4o",
"type": "budget_exceeded",
"param": null,
"code": "400"
}
}
代理
为在 LiteLLM 代理网关上注册的代理设置预算和速率限制。您可以控制
- 每代理速率限制:代理本身的
tpm_limit和rpm_limit - 每会话速率限制:按会话应用的
session_tpm_limit和session_rpm_limit - 每会话迭代上限:代理
litellm_params中的max_iterations - 每会话预算上限:代理
litellm_params中的max_budget_per_session
- 代理速率限制
- 会话速率限制
- 会话预算
在代理上设置 tpm_limit 和 rpm_limit 以限制所有会话的总吞吐量。
curl -X POST 'https://:4000/v1/agents' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"agent_name": "my-research-agent",
"agent_card_params": {
"name": "my-research-agent",
"description": "A research agent",
"url": "http://my-agent:8080",
"version": "1.0.0"
},
"tpm_limit": 100000,
"rpm_limit": 100
}'
设置 session_tpm_limit 和 session_rpm_limit 以限制每个单独会话的吞吐量。
curl -X POST 'https://:4000/v1/agents' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"agent_name": "my-research-agent",
"agent_card_params": {
"name": "my-research-agent",
"description": "A research agent",
"url": "http://my-agent:8080",
"version": "1.0.0"
},
"session_tpm_limit": 50000,
"session_rpm_limit": 50
}'
在代理 litellm_params 中设置 max_iterations 和 max_budget_per_session 以限制单个会话。需要 require_trace_id_on_calls_by_agent,以便 LiteLLM 可以跟踪每个会话的调用。
curl -X POST 'https://:4000/v1/agents' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"agent_name": "my-research-agent",
"agent_card_params": {
"name": "my-research-agent",
"description": "A research agent",
"url": "http://my-agent:8080",
"version": "1.0.0"
},
"litellm_params": {
"require_trace_id_on_calls_by_agent": true,
"max_iterations": 25,
"max_budget_per_session": 5.00
}
}'
当会话超过限制时,请求会收到 429 Too Many Requests 响应。
有关完整详情,请参阅 代理迭代预算 指南。
您还可以使用 PATCH /v1/agents/{agent_id} 更新现有代理的速率限制
curl -X PATCH 'https://:4000/v1/agents/<agent_id>' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"tpm_limit": 200000,
"rpm_limit": 200,
"session_tpm_limit": 50000,
"session_rpm_limit": 50
}'
客户
使用此功能可为传递给 /chat/completions 的 user 设置预算,无需为每个用户创建一个密钥
第 1 步。修改 config.yaml 定义 litellm.max_end_user_budget
general_settings:
master_key: sk-1234
litellm_settings:
max_end_user_budget: 0.0001 # budget for 'user' passed to /chat/completions
- 发出 /chat/completions 调用,传递 'user' - 第一次调用有效
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-zi5onDRdHGD24v0Zdn7VBA' \
--data ' {
"model": "azure-gpt-3.5",
"user": "ishaan3",
"messages": [
{
"role": "user",
"content": "what time is it"
}
]
}'
- 发出 /chat/completions 调用,传递 'user' - 调用失败,因为 'ishaan3' 超出预算
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-zi5onDRdHGD24v0Zdn7VBA' \
--data ' {
"model": "azure-gpt-3.5",
"user": "ishaan3",
"messages": [
{
"role": "user",
"content": "what time is it"
}
]
}'
错误
{"error":{"message":"Budget has been exceeded: User ishaan3 has exceeded their budget. Current spend: 0.0008869999999999999; Max Budget: 0.0001","type":"auth_error","param":"None","code":401}}%
重置预算
重置跨密钥/内部用户/团队/客户的预算
budget_duration:预算在指定周期结束时重置。如果不设置,预算永不重置。您可以将周期设置为秒("30s")、分钟("30m")、小时("30h")、天("30d")。
- 内部用户
- 密钥
- 团队
curl 'http://0.0.0.0:4000/user/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"max_budget": 10,
"budget_duration": "30s", # 👈 KEY CHANGE
}'
curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"max_budget": 10,
"budget_duration": "30s", # 👈 KEY CHANGE
}'
curl 'http://0.0.0.0:4000/team/new' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data-raw '{
"max_budget": 10,
"budget_duration": "30s", # 👈 KEY CHANGE
}'
注意:默认情况下,服务器每 10 分钟检查一次重置情况,以最小化数据库调用。
要更改此设置,请设置 proxy_budget_rescheduler_min_time 和 proxy_budget_rescheduler_max_time
例如:每 1 秒检查一次
general_settings:
proxy_budget_rescheduler_min_time: 1
proxy_budget_rescheduler_max_time: 1
设置速率限制
您可以设置
- tpm 限制(每分钟 Token 数)
- rpm 限制(每分钟请求数)
- 最大并行请求数
- 给定密钥或团队的每个模型的 rpm / tpm 限制
TPM 速率限制类型(输入/输出/总计)
默认情况下,TPM(每分钟 Token 数)速率限制计算总 Token 数(输入 + 输出)。您可以将其配置为仅计算输入 Token 或仅计算输出 Token。
在您的 config.yaml 中设置 token_rate_limit_type
general_settings:
master_key: sk-1234
token_rate_limit_type: "output" # Options: "input", "output", "total" (default)
| 值 | 描述 |
|---|---|
total | 计算总 Token 数(提示词 + 完成词)。默认行为。 |
input | 仅计算提示词/输入 Token |
output | 仅计算完成词/输出 Token |
此设置全局适用于所有 TPM 速率限制检查(密钥、用户、团队等)。
- 按团队
- 按团队按模型
- 按内部用户
- 按密钥
- 按 API 密钥按模型
- 按代理
- 针对客户
使用 /team/new 或 /team/update,以便在团队的多个密钥中持久化速率限制。
curl --location 'http://0.0.0.0:4000/team/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"team_id": "my-prod-team", "max_parallel_requests": 10, "tpm_limit": 20, "rpm_limit": 4}'
预期响应
{
"key": "sk-sA7VDkyhlQ7m8Gt77Mbt3Q",
"expires": "2024-01-19T01:21:12.816168",
"team_id": "my-prod-team",
}
为团队设置按模型的速率限制
使用 model_rpm_limit 和 model_tpm_limit 为属于团队的所有密钥设置按模型的速率限制。这些限制适用于团队中的所有密钥,并由密钥继承,除非在密钥级别被覆盖。
使用 /team/new 或 /team/update,并将 model_rpm_limit 和 model_tpm_limit 作为字典映射模型名称及其限制
curl --location 'http://0.0.0.0:4000/team/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"team_id": "my-prod-team",
"model_rpm_limit": {"gpt-4": 100, "gpt-3.5-turbo": 200},
"model_tpm_limit": {"gpt-4": 10000, "gpt-3.5-turbo": 20000}
}'
更新具有按模型限制的现有团队
curl --location 'http://0.0.0.0:4000/team/update' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"team_id": "my-prod-team",
"model_rpm_limit": {"gpt-4": 100, "gpt-3.5-turbo": 200},
"model_tpm_limit": {"gpt-4": 10000, "gpt-3.5-turbo": 20000}
}'
替代方案:使用元数据
您还可以通过 metadata 字段传递按模型的限制
curl --location 'http://0.0.0.0:4000/team/update' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"team_id": "my-prod-team",
"metadata": {
"model_rpm_limit": {"gpt-4": 100, "gpt-3.5-turbo": 200},
"model_tpm_limit": {"gpt-4": 10000, "gpt-3.5-turbo": 20000}
}
}'
解析顺序:当密钥属于团队时,速率限制解析如下:密钥元数据 > 密钥 model_max_budget > 团队元数据。密钥可以使用自己的 model_rpm_limit 或 model_tpm_limit 覆盖团队级的按模型限制。
验证:发出 /chat/completions 请求,并检查响应头 x-litellm-key-remaining-requests-{model} 和 x-litellm-key-remaining-tokens-{model} 以获取模型特定的限制。
使用 /user/new 或 /user/update,以便在内部用户的多个密钥中持久化速率限制。
curl --location 'http://0.0.0.0:4000/user/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"user_id": "krrish@berri.ai", "max_parallel_requests": 10, "tpm_limit": 20, "rpm_limit": 4}'
预期响应
{
"key": "sk-sA7VDkyhlQ7m8Gt77Mbt3Q",
"expires": "2024-01-19T01:21:12.816168",
"user_id": "krrish@berri.ai",
}
如果您只想针对该密钥进行限制,请使用 /key/generate。
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"max_parallel_requests": 10, "tpm_limit": 20, "rpm_limit": 4}'
预期响应
{
"key": "sk-ulGNRXWtv7M0lFnnsQk0wQ",
"expires": "2024-01-18T20:48:44.297973",
"user_id": "78c2c8fc-c233-43b9-b0c3-eb931da27b84" // 👈 auto-generated
}
为每个 API 密钥设置按模型的速率限制
设置 model_rpm_limit 和 model_tpm_limit 以设置每个 API 密钥按模型的速率限制
此处 gpt-4 是设置在 litellm config.yaml 上的 model_name
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"model_rpm_limit": {"gpt-4": 2}, "model_tpm_limit": {"gpt-4":}}'
预期响应
{
"key": "sk-ulGNRXWtv7M0lFnnsQk0wQ",
"expires": "2024-01-18T20:48:44.297973",
}
验证为此密钥正确设置的模型速率限制
发出 /chat/completions 请求并检查是否返回 x-litellm-key-remaining-requests-gpt-4
curl -i https://:4000/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-ulGNRXWtv7M0lFnnsQk0wQ" \
-d '{
"model": "gpt-4",
"messages": [
{"role": "user", "content": "Hello, Claude!ss eho ares"}
]
}'
预期标头
x-litellm-key-remaining-requests-gpt-4: 1
x-litellm-key-remaining-tokens-gpt-4: 179
这些标头表明
- key=
sk-ulGNRXWtv7M0lFnnsQk0wQ的 GPT-4 模型剩余 1 次请求 - key=
sk-ulGNRXWtv7M0lFnnsQk0wQ的 GPT-4 模型剩余 179 个 Token
在注册到 代理网关 的代理上设置速率限制。
代理级限制限制所有会话的总吞吐量
curl -X POST 'http://0.0.0.0:4000/v1/agents' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"agent_name": "my-agent", "agent_card_params": {"name": "my-agent", "description": "My agent", "url": "http://my-agent:8080", "version": "1.0.0"}, "tpm_limit": 100000, "rpm_limit": 100}'
会话级限制限制每个单独会话的吞吐量
curl -X POST 'http://0.0.0.0:4000/v1/agents' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{"agent_name": "my-agent", "agent_card_params": {"name": "my-agent", "description": "My agent", "url": "http://my-agent:8080", "version": "1.0.0"}, "session_tpm_limit": 50000, "session_rpm_limit": 50}'
您还可以通过 litellm_params 设置每会话的 max_iterations(调用次数上限)和 max_budget_per_session(美元上限)。详见 代理迭代预算。
您还可以在 UI 的“速率限制”选项卡下为客户创建预算 ID。
使用此功能可为传递给 /chat/completions 的 user 设置速率限制,无需为每个用户创建一个密钥
第 1 步。创建预算
在预算上设置 tpm_limit(如果需要,您也可以传递 rpm_limit)
curl --location 'http://0.0.0.0:4000/budget/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"budget_id" : "free-tier",
"tpm_limit": 5
}'
第 2 步。创建带有预算的 Customer
在创建此新客户时,我们使用第 1 步中的 budget_id="free-tier"
curl --location 'http://0.0.0.0:4000/customer/new' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"user_id" : "palantir",
"budget_id": "free-tier"
}'
第 3 步。在 /chat/completions 请求中传递 user_id
传递第 2 步中的 user_id 作为 user="palantir"
curl --location 'https://:4000/chat/completions' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"model": "llama3",
"user": "palantir",
"messages": [
{
"role": "user",
"content": "gm"
}
]
}'
为所有内部用户设置默认预算
使用此功能可为拥有密钥的用户设置默认预算。
当用户拥有 user_role="internal_user"(通过 /user/new 或 /user/update 设置)时,这将适用。
如果密钥有 team_id,则此设置不适用(此时适用团队预算)。告诉我们如何改进这一点!
- 在 config.yaml 中定义最大预算
model_list:
- model_name: "gpt-3.5-turbo"
litellm_params:
model: gpt-3.5-turbo
api_key: os.environ/OPENAI_API_KEY
litellm_settings:
max_internal_user_budget: 0 # amount in USD
internal_user_budget_duration: "1mo" # reset every month
- 为用户创建密钥
curl -L -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{}'
预期响应
{
...
"key": "sk-X53RdxnDhzamRwjKXR4IHg"
}
- 测试它!
curl -L -X POST 'http://0.0.0.0:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-X53RdxnDhzamRwjKXR4IHg' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hey, how's it going?"}]
}'
预期响应
{
"error": {
"message": "ExceededBudget: User=<user_id> over budget. Spend=3.7e-05, Budget=0.0",
"type": "budget_exceeded",
"param": null,
"code": "400"
}
}
多实例速率限制
重要提示
- 速率限制不适用于代理管理员用户。
- 测试速率限制时,请使用内部用户角色(非管理员),以确保限制按预期执行。
变更
- 这在更新当前请求/Token 时改为使用 async_increment 而不是 async_set_cache。
- 内存缓存每 0.01 秒与 Redis 同步一次,以避免为每个请求调用 Redis。
- 测试发现,这比之前的实现快 2 倍,并且在高流量(3 个实例上 100 RPS)下,预期失败与实际失败之间的漂移减少到最多 10 个请求。
授予对新模型的访问权限
使用模型访问组为用户授予对选定模型的访问权限,并随着时间推移向其添加新模型(例如 mistral, llama-2 等)。
用 /key/generate 与 /user/new 做这件事有什么区别?如果您在 /user/new 上操作,它将在为该用户生成的多个密钥中持久存在。
第 1 步。在 config.yaml 中分配模型、访问组
model_list:
- model_name: text-embedding-ada-002
litellm_params:
model: azure/azure-embedding-model
api_base: "os.environ/AZURE_API_BASE"
api_key: "os.environ/AZURE_API_KEY"
api_version: "2023-07-01-preview"
model_info:
access_groups: ["beta-models"] # 👈 Model Access Group
第 2 步。使用访问组创建密钥
curl --location 'https://:4000/user/new' \
-H 'Authorization: Bearer <your-master-key>' \
-H 'Content-Type: application/json' \
-d '{"models": ["beta-models"], # 👈 Model Access Group
"max_budget": 0}'
为现有内部用户创建新密钥
只需在 /key/generate 请求中包含 user_id 即可。
curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer <your-master-key>' \
--header 'Content-Type: application/json' \
--data '{"models": ["azure-models"], "user_id": "krrish@berri.ai"}'
API 规范
GenericBudgetInfo
一个定义预算信息(包含时间周期和限额)的 Pydantic 模型。
class GenericBudgetInfo(BaseModel):
budget_limit: float # The maximum budget amount in USD
time_period: str # Duration string like "1d", "30d", etc.
字段:
budget_limit(float):以美元为单位的最高预算金额time_period(str):指定预算时间周期的持续时间字符串。支持的格式- 秒:"30s"
- 分钟:"30m"
- 小时:"30h"
- 天:"30d"
示例:
{
"budget_limit": "0.0001",
"time_period": "1d"
}