消费跟踪
跟踪跨 100 多个大模型(LLM)的密钥、用户和团队的支出情况。
LiteLLM 会自动跟踪所有已知模型的支出。请参阅我们的 模型成本映射表
当响应包含层级元数据时,系统会自动应用特定于提供商的成本跟踪(例如:Vertex AI PayGo / 优先级定价、Bedrock 服务层级、Azure 基准模型映射)。
从 GitHub 同步模型定价数据,以确保成本跟踪的准确性。
请使用 成本差异调试 中的分步工作流:校准时间范围,比较令牌类别(包括缓存),然后判断差异是源于摄入量、公式还是模型映射定价。
如何使用 LiteLLM 跟踪支出
步骤 1
步骤 2 发送 /chat/completions 请求
- OpenAI Python v1.0.0+
- Curl 请求
- Langchain
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)
将 metadata 作为请求主体的一部分传递
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--data '{
"model": "llama3",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
"user": "palantir", # OPTIONAL: pass user to track spend by user
"metadata": {
"tags": ["jobID:214590dsff09fds", "taskName:run_page_classification"] # ENTERPRISE: pass tags to track spend by tags
}
}'
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
import os
os.environ["OPENAI_API_KEY"] = "sk-1234"
chat = ChatOpenAI(
openai_api_base="http://0.0.0.0:4000",
model = "llama3",
user="palantir",
extra_body={
"metadata": {
"tags": ["jobID:214590dsff09fds", "taskName:run_page_classification"] # ENTERPRISE: pass tags to track spend by tags
}
}
)
messages = [
SystemMessage(
content="You are a helpful assistant that im using to make a test request to."
),
HumanMessage(
content="test from litellm. tell me why it's amazing in 1 sentence"
),
]
response = chat(messages)
print(response)
步骤 3 - 验证支出跟踪 完成了。现在验证您的支出是否已被成功跟踪
- 响应头
- 数据库 + UI
预期的响应头中应包含 x-litellm-response-cost 以及计算出的成本
以下支出会被记录在 LiteLLM_SpendLogs 表中
{
"api_key": "fe6b0cab4ff5a5a8df823196cc8a450*****", # Hash of API Key used
"user": "default_user", # Internal User (LiteLLM_UserTable) that owns `api_key=sk-1234`.
"team_id": "e8d1460f-846c-45d7-9b43-55f3cc52ac32", # Team (LiteLLM_TeamTable) that owns `api_key=sk-1234`
"request_tags": ["jobID:214590dsff09fds", "taskName:run_page_classification"],# Tags sent in request
"end_user": "palantir", # Customer - the `user` sent in the request
"model_group": "llama3", # "model" passed to LiteLLM
"api_base": "https://api.groq.com/openai/v1/", # "api_base" of model used by LiteLLM
"spend": 0.000002, # Spend in $
"total_tokens": 100,
"completion_tokens": 80,
"prompt_tokens": 20,
}
导航至 LiteLLM UI 上的“Usage”(使用情况)选项卡(位于 https://your-proxy-endpoint/ui),并确认您可以在 Usage 下看到被跟踪的支出。
允许非代理管理员访问 /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 密钥支出 - 仅限主密钥 (MASTER KEY)
如果您希望执行以下操作,请使用 /global/spend/reset
-
重置所有 API 密钥和团队的支出。
LiteLLM_TeamTable和LiteLLM_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"}
每个用户的总支出
假设您一直在为终端用户颁发密钥,并已在密钥上设置了 user_id,您可以检查他们的使用情况。
curl -L -X GET 'https://:4000/user/info?user_id=jane_smith' \
-H 'Authorization: Bearer sk-...'
{
"user_id": "jane_smith",
"user_info": {
"spend": 0.1
},
"keys": [
{
"token": "6e952b0efcafbb6350240db25ed534b4ec6011b3e1ba1006eb4f903461fd36f6",
"key_name": "sk-...KE_A",
"key_alias": "user-01882d6b-e090-776a-a587-21c63e502670-01983ddb-872f-71a3-8b3a-f9452c705483",
"soft_budget_cooldown": false,
"spend": 0.1,
"expires": "2025-07-31T19:14:13.968000+00:00",
"models": [],
"aliases": {},
"config": {},
"user_id": "01982d6b-e090-776a-a587-21c63e502660",
"team_id": "f2044fde-2293-482f-bf35-a8dab4e85c5f",
"permissions": {},
"max_parallel_requests": null,
"metadata": {},
"blocked": null,
"tpm_limit": null,
"rpm_limit": null,
"max_budget": null,
"budget_duration": null,
"budget_reset_at": null,
"allowed_cache_controls": [],
"allowed_routes": [],
"model_spend": {},
"model_max_budget": {},
"budget_id": null,
"organization_id": null,
"object_permission_id": null,
"created_at": "2025-07-24T19:14:13.970000Z",
"created_by": "582b168f-fc11-4e14-ad6a-cf4bb3656ddc",
"updated_at": "2025-07-24T19:14:13.970000Z",
"updated_by": "582b168f-fc11-4e14-ad6a-cf4bb3656ddc",
"litellm_budget_table": null,
"litellm_organization_table": null,
"object_permission": null,
"team_alias": null
}
],
"teams": []
}
警告 终端用户可以在其请求体中提供 user 参数,这样做会增加通过 /customer/info?end_user_id=self-declared-user 报告的成本,而不是该 API 报告的密钥所有者的成本。这意味着用户可以通过这种方式“规避”他们的支出跟踪。这意味着如果您需要跟踪用户支出,且正在向终端用户发放 API 密钥,您必须在创建 API 密钥时始终设置 user_id,并在后端服务代表他们进行 LLM 调用时始终使用为该用户发放的密钥。这样才能准确跟踪他们的支出。
每日支出明细 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-...'
{
"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 参考
请参阅我们的 Swagger API,了解有关 /user/daily/activity 终端点的更多详细信息
自定义标签
有关所有标签选项(包括 x-litellm-tags 请求头、请求体 tags 以及基于配置的标签)的完整文档,请参阅专门的 请求标签 (Request Tags) 页面。
要求
- 应设置虚拟密钥和数据库,请参阅 虚拟密钥
注意: 默认情况下,LiteLLM 会将 User-Agent 作为成本跟踪的自定义标签。这使得您可以查看 Claude Code、Gemini CLI 等工具的使用情况。
客户端支出标签
- 在密钥上设置
- 在团队上设置
- OpenAI Python v1.0.0+
- OpenAI JS
- Curl 请求
- Langchain
curl -L -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"tags": ["tag1", "tag2", "tag3"]
}
}
'
curl -L -X POST 'http://0.0.0.0:4000/team/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"tags": ["tag1", "tag2", "tag3"]
}
}
'
将 extra_body={"metadata": { }} 设置为要传递的 metadata
import openai
client = openai.OpenAI(
api_key="anything",
base_url="http://0.0.0.0:4000"
)
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
extra_body={
"metadata": {
"tags": ["model-anthropic-claude-v2.1", "app-ishaan-prod"] # 👈 Key Change
}
}
)
print(response)
const openai = require("openai");
async function runOpenAI() {
const client = new openai.OpenAI({
apiKey: "sk-1234",
baseURL: "http://0.0.0.0:4000",
});
try {
const response = await client.chat.completions.create({
model: "gpt-3.5-turbo",
messages: [
{
role: "user",
content: "this is a test request, write a short poem",
},
],
metadata: {
tags: ["model-anthropic-claude-v2.1", "app-ishaan-prod"], // 👈 Key Change
},
});
console.log(response);
} catch (error) {
console.log("got this exception from server");
console.error(error);
}
}
// Call the asynchronous function
runOpenAI();
将 metadata 作为请求主体的一部分传递
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
"metadata": {"tags": ["model-anthropic-claude-v2.1", "app-ishaan-prod"]}
}'
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
chat = ChatOpenAI(
openai_api_base="http://0.0.0.0:4000",
model = "gpt-3.5-turbo",
temperature=0.1,
extra_body={
"metadata": {
"tags": ["model-anthropic-claude-v2.1", "app-ishaan-prod"]
}
}
)
messages = [
SystemMessage(
content="You are a helpful assistant that im using to make a test request to."
),
HumanMessage(
content="test from litellm. tell me why it's amazing in 1 sentence"
),
]
response = chat(messages)
print(response)
为支出跟踪添加自定义请求头
您可以向请求添加自定义标头,以跟踪支出和使用情况。
litellm_settings:
extra_spend_tag_headers:
- "x-custom-header"
禁用 User-Agent 跟踪
您可以将 litellm_settings.disable_add_user_agent_to_request_tags 设置为 true 来禁用 User-Agent 跟踪。
litellm_settings:
disable_add_user_agent_to_request_tags: true
✨ (企业版) 生成支出报告
使用此功能向其他团队、客户和用户收费
使用 /global/spend/report 终端点获取支出报告
- 各团队支出
- 各客户支出
- 特定 API 密钥支出
- 内部用户(密钥所有者)支出
示例请求
👉 关键更改:指定 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'
响应示例
- 预期响应
- 解析响应的脚本 (Python)
[
{
"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
}
]
}
]
}
]
import requests
url = 'https://:4000/global/spend/report'
params = {
'start_date': '2023-04-01',
'end_date': '2024-06-30'
}
headers = {
'Authorization': 'Bearer sk-1234'
}
# Make the GET request
response = requests.get(url, headers=headers, params=params)
spend_report = response.json()
for row in spend_report:
date = row["group_by_day"]
teams = row["teams"]
for team in teams:
team_name = team["team_name"]
total_spend = team["total_spend"]
metadata = team["metadata"]
print(f"Date: {date}")
print(f"Team: {team_name}")
print(f"Total Spend: {total_spend}")
print("Metadata: ", metadata)
print()
脚本输出
# Date: 2024-05-11T00:00:00+00:00
# Team: local_test_team
# Total Spend: 0.003675099999999999
# Metadata: [{'model': 'gpt-3.5-turbo', 'spend': 0.003675099999999999, 'api_key': 'b94d5e0bc3a71a573917fe1335dc0c14728c7016337451af9714924ff3a729db', 'total_tokens': 3105}]
# Date: 2024-05-13T00:00:00+00:00
# Team: Unassigned Team
# Total Spend: 3.4e-05
# Metadata: [{'model': 'gpt-3.5-turbo', 'spend': 3.4e-05, 'api_key': '9569d13c9777dba68096dea49b0b03e0aaf4d2b65d4030eda9e8a2733c3cd6e0', 'total_tokens': 50}]
# Date: 2024-05-13T00:00:00+00:00
# Team: central
# Total Spend: 0.000684
# Metadata: [{'model': 'gpt-3.5-turbo', 'spend': 0.000684, 'api_key': '0323facdf3af551594017b9ef162434a9b9a8ca1bbd9ccbd9d6ce173b1015605', 'total_tokens': 498}]
# Date: 2024-05-13T00:00:00+00:00
# Team: local_test_team
# Total Spend: 0.0005715000000000001
# Metadata: [{'model': 'gpt-3.5-turbo', 'spend': 0.0005715000000000001, 'api_key': 'b94d5e0bc3a71a573917fe1335dc0c14728c7016337451af9714924ff3a729db', 'total_tokens': 423}]
示例请求
👉 关键更改:指定 group_by=customer
curl -X GET 'https://:4000/global/spend/report?start_date=2024-04-01&end_date=2024-06-30&group_by=customer' \
-H 'Authorization: Bearer sk-1234'
示例响应
[
{
"group_by_day": "2024-04-30T00:00:00+00:00",
"customers": [
{
"customer": "palantir",
"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
}
]
}
]
}
]
👉 关键更改:指定 api_key=sk-1234
curl -X GET 'https://:4000/global/spend/report?start_date=2024-04-01&end_date=2024-06-30&api_key=sk-1234' \
-H 'Authorization: Bearer sk-1234'
示例响应
[
{
"api_key": "example-api-key-123",
"total_cost": 0.3201286305151999,
"total_input_tokens": 36.0,
"total_output_tokens": 1593.0,
"model_details": [
{
"model": "dall-e-3",
"total_cost": 0.31999939051519993,
"total_input_tokens": 0,
"total_output_tokens": 0
},
{
"model": "llama3-8b-8192",
"total_cost": 0.00012924,
"total_input_tokens": 36,
"total_output_tokens": 1593
}
]
}
]
内部用户(密钥所有者):这是调用 /key/generate 时传递的 user_id 的值
👉 关键更改:指定 internal_user_id=ishaan
curl -X GET 'https://:4000/global/spend/report?start_date=2024-04-01&end_date=2024-12-30&internal_user_id=ishaan' \
-H 'Authorization: Bearer sk-1234'
示例响应
[
{
"api_key": "example-api-key-123",
"total_cost": 0.00013132,
"total_input_tokens": 105.0,
"total_output_tokens": 872.0,
"model_details": [
{
"model": "gpt-3.5-turbo-instruct",
"total_cost": 5.85e-05,
"total_input_tokens": 15,
"total_output_tokens": 18
},
{
"model": "llama3-8b-8192",
"total_cost": 7.282000000000001e-05,
"total_input_tokens": 90,
"total_output_tokens": 854
}
]
},
{
"api_key": "151e85e46ab8c9c7fad090793e3fe87940213f6ae665b543ca633b0b85ba6dc6",
"total_cost": 5.2699999999999993e-05,
"total_input_tokens": 26.0,
"total_output_tokens": 27.0,
"model_details": [
{
"model": "gpt-3.5-turbo",
"total_cost": 5.2499999999999995e-05,
"total_input_tokens": 24,
"total_output_tokens": 27
},
{
"model": "text-embedding-ada-002",
"total_cost": 2e-07,
"total_input_tokens": 2,
"total_output_tokens": 0
}
]
},
{
"api_key": "60cb83a2dcbf13531bd27a25f83546ecdb25a1a6deebe62d007999dc00e1e32a",
"total_cost": 9.42e-06,
"total_input_tokens": 30.0,
"total_output_tokens": 99.0,
"model_details": [
{
"model": "llama3-8b-8192",
"total_cost": 9.42e-06,
"total_input_tokens": 30,
"total_output_tokens": 99
}
]
}
]
📊 支出日志 API - 单笔交易日志
/spend/logs 终端点现在支持 summarize 参数,以便在使用日期过滤器时控制数据格式。
关键参数
| 参数 | 描述 |
|---|---|
summarize | 新参数:true(默认)= 聚合数据,false = 单笔交易日志 |
示例
获取单笔交易日志
curl -X GET "https://:4000/spend/logs?start_date=2024-01-01&end_date=2024-01-02&summarize=false" \
-H "Authorization: Bearer sk-1234"
获取摘要数据(默认)
curl -X GET "https://:4000/spend/logs?start_date=2024-01-01&end_date=2024-01-02" \
-H "Authorization: Bearer sk-1234"
用例
summarize=false:分析仪表板、ETL 流程、详细审计追踪summarize=true:每日支出报告、高层级成本跟踪(旧版行为)
✨ 自定义支出日志元数据
将特定的键值对作为支出日志元数据的一部分进行记录
在支出日志元数据中记录特定键值对是一项企业级功能。
要求
- 应设置虚拟密钥和数据库,请参阅 虚拟密钥
用法 - 带有特殊支出日志元数据的 /chat/completions 请求
- 在密钥上设置
- 在团队上设置
- OpenAI Python v1.0.0+
- OpenAI JS
- Curl 请求
- 使用请求头
- Langchain
curl -L -X POST 'http://0.0.0.0:4000/key/generate' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"spend_logs_metadata": {
"hello": "world"
}
}
}
'
curl -L -X POST 'http://0.0.0.0:4000/team/new' \
-H 'Authorization: Bearer sk-1234' \
-H 'Content-Type: application/json' \
-d '{
"metadata": {
"spend_logs_metadata": {
"hello": "world"
}
}
}
'
将 extra_body={"metadata": { }} 设置为要传递的 metadata
import openai
client = openai.OpenAI(
api_key="anything",
base_url="http://0.0.0.0:4000"
)
# request sent to model set on litellm proxy, `litellm --model`
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
extra_body={
"metadata": {
"spend_logs_metadata": {
"hello": "world"
}
}
}
)
print(response)
使用请求头
import openai
client = openai.OpenAI(
api_key="sk-1234",
base_url="http://0.0.0.0:4000"
)
# Pass spend logs metadata via headers
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages = [
{
"role": "user",
"content": "this is a test request, write a short poem"
}
],
extra_headers={
"x-litellm-spend-logs-metadata": '{"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}'
}
)
print(response)
const openai = require('openai');
async function runOpenAI() {
const client = new openai.OpenAI({
apiKey: 'sk-1234',
baseURL: 'http://0.0.0.0:4000'
});
try {
const response = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: "this is a test request, write a short poem"
},
],
metadata: {
spend_logs_metadata: { // 👈 Key Change
hello: "world"
}
}
});
console.log(response);
} catch (error) {
console.log("got this exception from server");
console.error(error);
}
}
// Call the asynchronous function
runOpenAI();
使用请求头
const openai = require('openai');
async function runOpenAI() {
const client = new openai.OpenAI({
apiKey: 'sk-1234',
baseURL: 'http://0.0.0.0:4000'
});
try {
const response = await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{
role: 'user',
content: "this is a test request, write a short poem"
},
]
}, {
headers: {
'x-litellm-spend-logs-metadata': '{"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}'
}
});
console.log(response);
} catch (error) {
console.log("got this exception from server");
console.error(error);
}
}
// Call the asynchronous function
runOpenAI();
将 metadata 作为请求主体的一部分传递
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
],
"metadata": {
"spend_logs_metadata": {
"hello": "world"
}
}
}'
将 x-litellm-spend-logs-metadata 作为包含 JSON 字符串的请求头传递
curl --location 'http://0.0.0.0:4000/chat/completions' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer sk-1234' \
--header 'x-litellm-spend-logs-metadata: {"user_id": "12345", "project_id": "proj_abc", "request_type": "chat_completion"}' \
--data '{
"model": "gpt-3.5-turbo",
"messages": [
{
"role": "user",
"content": "what llm are you"
}
]
}'
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
SystemMessagePromptTemplate,
)
from langchain.schema import HumanMessage, SystemMessage
chat = ChatOpenAI(
openai_api_base="http://0.0.0.0:4000",
model = "gpt-3.5-turbo",
temperature=0.1,
extra_body={
"metadata": {
"spend_logs_metadata": {
"hello": "world"
}
}
}
)
messages = [
SystemMessage(
content="You are a helpful assistant that im using to make a test request to."
),
HumanMessage(
content="test from litellm. tell me why it's amazing in 1 sentence"
),
]
response = chat(messages)
print(response)
查看带有自定义元数据的支出
/spend/logs 请求格式
curl -X GET "http://0.0.0.0:4000/spend/logs?request_id=<your-call-id" \ # e.g.: chatcmpl-9ZKMURhVYSi9D6r6PJ9vLcayIK0Vm
-H "Authorization: Bearer sk-1234"
/spend/logs 响应格式
[
{
"request_id": "chatcmpl-9ZKMURhVYSi9D6r6PJ9vLcayIK0Vm",
"call_type": "acompletion",
"metadata": {
"user_api_key": "example-api-key-123",
"user_api_key_alias": null,
"spend_logs_metadata": { # 👈 LOGGED CUSTOM METADATA
"hello": "world"
},
"user_api_key_team_id": null,
"user_api_key_user_id": "116544810872468347480",
"user_api_key_team_alias": null
},
}
]