🙋♂️ 客户
跟踪客户支出,为客户设置预算。
跟踪客户信用
1. 使用客户ID进行LLM API调用
进行 /chat/completions 调用,传递 'user' - 首次调用成功
curl -X POST 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \ # 👈 你的代理密钥
--data ' {
"model": "azure-gpt-3.5",
"user": "ishaan3", # 👈 客户ID
"messages": [
{
"role": "user",
"content": "what time is it"
}
]
}'
客户ID将被插入数据库,同时记录新的支出。
如果客户ID已存在,支出将增加。
2. 获取客户支出
- 总支出
- 事件Webhook
调用 /customer/info 获取客户的总支出
curl -X GET 'http://0.0.0.0:4000/customer/info?end_user_id=ishaan3' \ # 👈 客户ID
-H 'Authorization: Bearer sk-1234' \ # 👈 你的代理密钥
预期响应:
{
"user_id": "ishaan3",
"blocked": false,
"alias": null,
"spend": 0.001413,
"allowed_model_region": null,
"default_model": null,
"litellm_budget_table": null
}
要在客户端数据库中更新支出,请将代理指向你的webhook。
例如,如果你的服务器是 https://webhook.site,并且你在监听 6ab090e8-c55f-4a23-b075-3209f5c57906
- 将webhook URL添加到你的代理环境:
export WEBHOOK_URL="https://webhook.site/6ab090e8-c55f-4a23-b075-3209f5c57906"
- 将 'webhook' 添加到 config.yaml
general_settings:
alerting: ["webhook"] # 👈 关键更改
- 测试它!
curl -X POST 'http://localhost: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": "krrish12"
}
'
预期响应
{
"spend": 0.0011120000000000001, # 👈 支出
"max_budget": null,
"token": "88dc28d0f030c55ed4ab77ed8faf098196cb1c05df778539800c9f1243fe6b4b",
"customer_id": "krrish12", # 👈 客户ID
"user_id": null,
"team_id": null,
"user_email": null,
"key_alias": null,
"projected_exceeded_date": null,
"projected_spend": null,
"event": "spend_tracked",
"event_group": "customer",
"event_message": "Customer spend tracked. Customer=krrish12, spend=0.0011120000000000001"
}
设置客户预算
在LiteLLM代理上设置客户预算(例如每月预算,tpm/rpm限制)
快速开始
创建/更新带有预算的客户
创建新客户并设置预算
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", # 👈 可以是浮点数
}'
测试它!
curl -X POST 'http://localhost: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
- API
- 转到UI上的“预算”选项卡。
- 点击“+ 创建预算”。
- 创建你的定价层级(例如,预算为4美元的“my-free-tier”)。这意味着该定价层级上的每个用户将有4美元的最大预算。
使用 /budget/new 端点创建新预算。API参考
curl -X POST 'http://localhost:4000/budget/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"budget_id": "my-free-tier",
"max_budget": 4
}
2. 将预算分配给客户
在你的应用程序代码中,创建新客户时分配预算。
只需使用创建预算时使用的 budget_id。在我们的示例中,这是 my-free-tier。
curl -X POST 'http://localhost:4000/customer/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-D '{
"user_id": "my-customer-id",
"budget_id": "my-free-tier" # 👈 关键更改
}
3. 测试它!
- curl
- OpenAI
curl -X POST 'http://localhost:4000/customer/new' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer sk-1234' \
-d '{
"user_id": "my-customer-id",
"budget_id": "my-free-tier" # 👈 关键更改
}
from openai import OpenAI
client = OpenAI(
base_url="<your_proxy_base_url>",
api_key="<your_proxy_key>"
)
completion = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"}
],
user="my-customer-id"
)
print(completion.choices[0].message)