Skip to main content

💸 支出跟踪

跟踪100多个LLM的密钥、用户和团队的支出。

如何使用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": "这是一个测试请求,请写一首短诗"
}
],
user="palantir",
extra_body={
"metadata": {
"tags": ["jobID:214590dsff09fds", "taskName:run_page_classification"]
}
}
)

print(response)

步骤3 - 验证跟踪的支出 就是这样。现在验证您的支出是否被跟踪

期望在响应头中看到 x-litellm-response-cost,其中包含计算的费用

Getting Spend Reports - To Charge Other Teams, Customers, Users

Use the /global/spend/report endpoint to get spend reports

Example Request

👉 Key Change: Specify group_by=team

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

Example Response

[
{
"按天分组": "2024-04-30T00:00:00+00:00",
"团队": [
{
"团队名称": "产品团队",
"总支出": 0.0015265,
"元数据": [ # 按唯一(键 + 模型)查看支出
{
"模型": "gpt-4",
"支出": 0.00123,
"总令牌数": 28,
"API密钥": "88dc28.." # 哈希后的API密钥
},
{
"模型": "gpt-4",
"支出": 0.00123,
"总令牌数": 28,
"API密钥": "a73dc2.." # 哈希后的API密钥
},
{
"模型": "chatgpt-v-2",
"支出": 0.000214,
"总令牌数": 122,
"API密钥": "898c28.." # 哈希后的API密钥
},
{
"模型": "gpt-3.5-turbo",
"支出": 0.0000825,
"总令牌数": 85,
"API密钥": "84dc28.." # 哈希后的API密钥
}
]
}
]
}
]

允许非代理管理员访问 /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 'http://localhost:4000/global/spend/report?start_date=2024-04-01&end_date=2024-06-30' \
-H 'Authorization: Bearer sk-H16BKvrSNConSsBYLGc_7A'

重置团队、API 密钥支出 - 仅限主密钥

如果您想:

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

  • LiteLLM 将保留 LiteLLMSpendLogs 中的所有日志以供审计目的

请求

只有您设置的 LITELLM_MASTER_KEY 可以访问此路由

curl -X POST \
'http://localhost:4000/global/spend/reset' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json'
预期响应
{"message":"所有 API 密钥和团队的支出已成功重置","status":"success"}

Azure OpenAI 模型的支出跟踪

设置基本模型以跟踪 Azure 图像生成的成本

图像生成

model_list: 
- model_name: dall-e-3
litellm_params:
model: azure/dall-e-3-test
api_version: 2023-06-01-preview
api_base: https://openai-gpt-4-test-v-1.openai.azure.com/
api_key: os.environ/AZURE_API_KEY
base_model: dall-e-3 # 👈 将 dall-e-3 设置为基础模型
model_info:
mode: image_generation

聊天完成 / 嵌入

问题:当使用 azure/gpt-4-1106-preview 时,Azure 在响应中返回 gpt-4。这导致成本跟踪不准确

解决方案 ✅ :在您的配置中设置 base_model,以便 LiteLLM 使用正确的模型计算 Azure 成本

这里 获取基础模型名称

带有 base_model 的示例配置

model_list:
- model_name: azure-gpt-3.5
litellm_params:
model: azure/chatgpt-v-2
api_base: os.environ/AZURE_API_BASE
api_key: os.environ/AZURE_API_KEY
api_version: "2023-07-01-preview"
model_info:
base_model: azure/gpt-4-1106-preview

自定义输入/输出定价

👉 前往 自定义输入/输出定价 为您的模型设置自定义定价

✨ 自定义支出日志元数据

将特定的键值对作为支出日志元数据的一部分进行记录

info

在支出日志元数据中记录特定的键值对是一项企业级功能。查看此处

✨ 自定义标签

info

使用自定义标签跟踪支出是一项企业级功能。查看此处

优云智算