跳到主要内容

💸 费用跟踪

跟踪跨 100+ LLM 的密钥、用户和团队费用。

LiteLLM 自动跟踪所有已知模型的费用。请参阅我们的 模型成本图

如何使用 LiteLLM 跟踪费用

步骤 1

👉 使用数据库设置 LiteLLM

步骤 2 发送 /chat/completions 请求

import openai
client = openai.OpenAI(
api_key="sk-1234",
base_url="http://0.0.0.0:4000"
)

response = client.chat.completions.create(
model="llama3",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
user="palantir", # OPTIONAL: pass user to track spend by user
extra_body={
"metadata": {
"tags": ["jobID:214590dsff09fds", "taskName:run_page_classification"] # ENTERPRISE: pass tags to track spend by tags
}
}
)

print(response)

步骤 3 - 验证费用是否已跟踪 就这么简单。现在验证您的费用是否已被跟踪

预期在响应头中看到包含计算成本的 x-litellm-response-cost

允许非代理管理员访问 /spend 端点

当您希望非代理管理员访问 /spend 端点时使用此功能

创建密钥

使用 permissions={"get_spend_routes": true} 创建密钥

curl --location 'http://0.0.0.0:4000/key/generate' \
--header 'Authorization: Bearer sk-1234' \
--header 'Content-Type: application/json' \
--data '{
"permissions": {"get_spend_routes": true}
}'
/spend 端点上使用生成的密钥

使用新生成的密钥访问费用路由

curl -X GET 'https://:4000/global/spend/report?start_date=2024-04-01&end_date=2024-06-30' \
-H 'Authorization: Bearer sk-H16BKvrSNConSsBYLGc_7A'

重置团队、API 密钥费用 - 仅限主密钥

如果您想,请使用 /global/spend/reset

  • 重置所有 API 密钥、团队的费用。LiteLLM_TeamTableLiteLLM_VerificationToken 中所有团队和密钥的 spend 将设置为 spend=0

  • LiteLLM 会将所有日志保存在 LiteLLMSpendLogs 中用于审计目的

请求

只有您设置的 LITELLM_MASTER_KEY 才能访问此路由

curl -X POST \
'https://:4000/global/spend/reset' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json'
预期响应
{"message":"Spend for all API Keys and Teams reset successfully","status":"success"}

日费用明细 API

通过一个端点检索用户的详细每日使用数据(按模型、提供商和 API 密钥)。

示例请求

日费用明细 API
curl -L -X GET 'https://:4000/user/daily/activity?start_date=2025-03-20&end_date=2025-03-27' \
-H 'Authorization: Bearer sk-...'
日费用明细 API 响应
{
"results": [
{
"date": "2025-03-27",
"metrics": {
"spend": 0.0177072,
"prompt_tokens": 111,
"completion_tokens": 1711,
"total_tokens": 1822,
"api_requests": 11
},
"breakdown": {
"models": {
"gpt-4o-mini": {
"spend": 1.095e-05,
"prompt_tokens": 37,
"completion_tokens": 9,
"total_tokens": 46,
"api_requests": 1
},
"providers": { "openai": { ... }, "azure_ai": { ... } },
"api_keys": { "3126b6eaf1...": { ... } }
}
}
],
"metadata": {
"total_spend": 0.7274667,
"total_prompt_tokens": 280990,
"total_completion_tokens": 376674,
"total_api_requests": 14
}
}

API 参考

有关 /user/daily/activity 端点的更多详细信息,请参阅我们的 Swagger API

✨ (企业版) 生成费用报告

使用此功能向其他团队、客户、用户收费

使用 /global/spend/report 端点获取费用报告

示例请求

👉 关键更改:指定 group_by=team

curl -X GET 'https://:4000/global/spend/report?start_date=2024-04-01&end_date=2024-06-30&group_by=team' \
-H 'Authorization: Bearer sk-1234'

示例响应

[
{
"group_by_day": "2024-04-30T00:00:00+00:00",
"teams": [
{
"team_name": "Prod Team",
"total_spend": 0.0015265,
"metadata": [ # see the spend by unique(key + model)
{
"model": "gpt-4",
"spend": 0.00123,
"total_tokens": 28,
"api_key": "88dc28.." # the hashed api key
},
{
"model": "gpt-4",
"spend": 0.00123,
"total_tokens": 28,
"api_key": "a73dc2.." # the hashed api key
},
{
"model": "chatgpt-v-2",
"spend": 0.000214,
"total_tokens": 122,
"api_key": "898c28.." # the hashed api key
},
{
"model": "gpt-3.5-turbo",
"spend": 0.0000825,
"total_tokens": 85,
"api_key": "84dc28.." # the hashed api key
}
]
}
]
}
]

✨ 自定义费用日志元数据

将特定的键值对记录为费用日志元数据的一部分

信息

在费用日志元数据中记录特定的键值对是一项企业版功能。请参阅此处

✨ 自定义标签

信息

使用自定义标签跟踪费用是一项企业版功能。请参阅此处