跳至主要内容

客户/最终用户预算

跟踪消费,为您的客户设置预算。

跟踪客户消费

1. 使用客户 ID 进行 LLM API 调用

进行 /chat/completions 调用,传递 'user' - 首次调用有效

使用客户 ID 发送请求
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \ # 👈 YOUR PROXY KEY
--data ' {
"model": "azure-gpt-3.5",
"user": "ishaan3", # 👈 CUSTOMER ID
"messages": [
{
"role": "user",
"content": "what time is it"
}
]
}'

客户 ID 将被插入到数据库中,并更新消费金额。

如果客户 ID 已经存在,消费金额将会增加。

2. 获取客户消费

调用 /customer/info 获取客户的总消费。

获取客户消费
curl -X GET 'http://0.0.0.0:4000/customer/info?end_user_id=ishaan3' \ # 👈 CUSTOMER ID
-H 'Authorization: Bearer sk-1234' \ # 👈 YOUR PROXY KEY

预期响应

响应
{
"user_id": "ishaan3",
"blocked": false,
"alias": null,
"spend": 0.001413,
"allowed_model_region": null,
"default_model": null,
"litellm_budget_table": null
}

设置客户预算

在 LiteLLM 代理上设置客户预算(例如,每月预算、tpm/rpm 限制)。

所有客户的默认预算

将预算限制应用于没有明确预算的所有客户。这对于所有最终用户的速率限制和消费控制非常有用。

步骤 1:创建默认预算

创建默认预算
curl -X POST 'https://:4000/budget/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"max_budget": 10,
"rpm_limit": 2,
"tpm_limit": 1000
}'

步骤 2:配置默认预算 ID

config.yaml
litellm_settings:
max_end_user_budget_id: "budget_id_from_step_1"

步骤 3:测试它

使用客户 ID 发送请求
curl -X POST 'https://:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": "Hello"}],
"user": "my-customer-id"
}'

客户将受到默认预算限制(RPM、TPM 和美元预算)。具有明确预算的客户不受影响。

快速入门

创建/更新具有预算的客户

创建具有预算的新客户

创建具有预算的客户
curl -X POST 'http://0.0.0.0:4000/customer/new'         
-H 'Authorization: Bearer sk-1234'
-H 'Content-Type: application/json'
-d '{
"user_id" : "my-customer-id",
"max_budget": "0", # 👈 CAN BE FLOAT
}'

测试它!

测试客户预算
curl -X POST 'https://:4000/chat/completions' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"model": "mistral",
"messages": [
{
"role": "user",
"content": "What'\''s the weather like in Boston today?"
}
],
"user": "ishaan-jaff-48"
}

分配定价层级

创建并将客户分配到定价层级。

1. 创建预算

  • 转到 UI 上的“预算”选项卡。
  • 点击“+ 创建预算”。
  • 创建您的定价层级(例如,'my-free-tier',预算为 $4)。这意味着此定价层级上的每个用户都将拥有最高 $4 的预算。

2. 将预算分配给客户

在您的应用程序代码中,在创建新客户时分配预算。

只需使用创建预算时使用的 budget_id。在我们的示例中,这是 my-free-tier

将预算分配给客户
curl -X POST 'https://:4000/customer/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"user_id": "my-customer-id",
"budget_id": "my-free-tier" # 👈 KEY CHANGE
}

3. 测试它!

使用 curl 测试
curl -X POST 'https://:4000/customer/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"user_id": "my-customer-id",
"budget_id": "my-free-tier" # 👈 KEY CHANGE
}