跳至主要内容

计费

为内部团队、外部客户计费他们的使用情况

🚨 要求

步骤

  • 将代理连接到 Lago
  • 设置您要计费的 ID(客户、内部用户、团队)
  • 开始!

快速入门

为内部团队计费

1. 将代理连接到 Lago

在您的 proxy config.yaml 中将 'lago' 设置为回调

model_list:
- model_name: fake-openai-endpoint
litellm_params:
model: openai/fake
api_key: fake-key
api_base: https://exampleopenaiendpoint-production.up.railway.app/

litellm_settings:
callbacks: ["lago"] # 👈 KEY CHANGE

general_settings:
master_key: sk-1234

将您的 Lago 密钥添加到环境变量

export LAGO_API_BASE="https://:3000" # self-host - https://docs.getlago.com/guide/self-hosted/docker#run-the-app
export LAGO_API_KEY="3e29d607-de54-49aa-a019-ecf585729070" # Get key - https://docs.getlago.com/guide/self-hosted/docker#find-your-api-key
export LAGO_API_EVENT_CODE="openai_tokens" # name of lago billing code
export LAGO_API_CHARGE_BY="team_id" # 👈 Charges 'team_id' attached to proxy key

启动代理

litellm --config /path/to/config.yaml

2. 为内部团队创建密钥

curl 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data-raw '{"team_id": "my-unique-id"}' # 👈 Internal Team's ID

响应对象

{
"key": "sk-tXL0wt5-lOOVK9sfY2UacA",
}

3. 开始计费!

curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-tXL0wt5-lOOVK9sfY2UacA' \ # 👈 Team's Key
--data ' {
"model": "fake-openai-endpoint",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
}
'

在 Lago 上查看结果

高级 - Lago 日志对象

LiteLLM 将记录这些内容到 Lagos

{
"event": {
"transaction_id": "<generated_unique_id>",
"external_customer_id": <selected_id>, # either 'end_user_id', 'user_id', or 'team_id'. Default 'end_user_id'.
"code": os.getenv("LAGO_API_EVENT_CODE"),
"properties": {
"input_tokens": <number>,
"output_tokens": <number>,
"model": <string>,
"response_cost": <number>, # 👈 LITELLM CALCULATED RESPONSE COST - https://github.com/BerriAI/litellm/blob/d43f75150a65f91f60dc2c0c9462ce3ffc713c1f/litellm/utils.py#L1473
}
}
}

高级 - 计费客户、内部用户

对于

  • 客户(通过 /chat/completion 调用中的 'user' 参数传递的 ID)= 'end_user_id'
  • 内部用户(在 创建密钥 时设置)= 'user_id'
  • 团队(在 创建密钥 时设置)= 'team_id'
  1. 将 'LAGO_API_CHARGE_BY' 设置为 'end_user_id'
export LAGO_API_CHARGE_BY="end_user_id"
  1. 测试它!
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data ' {
"model": "gpt-4o",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
"user": "my_customer_id" # 👈 whatever your customer id is
}
'