Spend Tracking
import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import Image from '@theme/IdealImage';
Overview
Spend Tracking
Track spend for keys, users, and teams across 100+ LLMs.
LiteLLM automatically tracks spend for all known models. See our model cost map
Provider-specific cost tracking (e.g., Vertex AI PayGo / priority pricing, Bedrock service tiers, Azure base model mapping) is applied automatically when the response includes tier metadata.
:::tip Keep Pricing Data Updated Sync model pricing data from GitHub to ensure accurate cost tracking. :::
:::info Cost does not match your provider bill? Use the step-by-step workflow in Debugging a cost discrepancy: align time ranges, compare token categories (including cache), then decide whether the gap is ingestion, formula, or model-map pricing. :::
How to Track Spend with LiteLLM
Step 1
š Setup LiteLLM with a Database
Step2 Send /chat/completions request
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)
Pass metadata as part of the request body
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
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)
Step3 - Verify Spend Tracked That's IT. Now Verify your spend was tracked
Expect to see x-litellm-response-cost in the response headers with calculated cost
The following spend gets tracked in Table 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,
}
Navigate to the Usage Tab on the LiteLLM UI (found on https://your-proxy-endpoint/ui) and verify you see spend tracked under Usage
Allowing Non-Proxy Admins to access /spend endpoints
Use this when you want non-proxy admins to access /spend endpoints
:::info
Schedule a meeting with us to get your Enterprise License
:::
Create Key
Create Key with with 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}
}'
Use generated key on /spend endpoints
Access spend Routes with newly generate keys
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'
Reset Team, API Key Spend - MASTER KEY ONLY
Use /global/spend/reset if you want to:
-
Reset the Spend for all API Keys, Teams. The
spendfor ALL Teams and Keys inLiteLLM_TeamTableandLiteLLM_VerificationTokenwill be set tospend=0 -
LiteLLM will maintain all the logs in
LiteLLMSpendLogsfor Auditing Purposes
Request
Only the LITELLM_MASTER_KEY you set can access this route
curl -X POST \\
'http://localhost:4000/global/spend/reset' \\
-H 'Authorization: Bearer sk-1234' \\
-H 'Content-Type: application/json'
Expected Responses
{"message":"Spend for all API Keys and Teams reset successfully","status":"success"}
Total spend per user
Assuming you have been issuing keys for end users, and setting their user_id on the key, you can check their usage.
curl -L -X GET 'http://localhost: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": []
}
Warning
End users can provide the user parameter in their request bodies, doing this will increment the cost reported via /customer/info?end_user_id=self-declared-user, and not for the user that owns the key as reported by that API. This means users could "avoid" having their spend tracked, through their method.
This means if you need to track user spend, and are giving end users API keys, you must always set user_id when creating their api keys, and use keys issued for that user every time you're making LLM calls on their behalf in backend services. This will track their spend.
Daily Spend Breakdown API
Retrieve granular daily usage data for a user (by model, provider, and API key) with a single endpoint.
Example Request:
curl -L -X GET 'http://localhost: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 Reference
See our Swagger API for more details on the /user/daily/activity endpoint
Custom Tags
:::tip See Full Request Tags Documentation
For comprehensive documentation on all tag options including x-litellm-tags header, request body tags, and config-based tags, see the dedicated Request Tags page.
:::
Requirements:
- Virtual Keys & a database should be set up, see virtual keys
Note: By default, LiteLLM will track User-Agent as a custom tag for cost tracking. This enables viewing usage for tools like Claude Code, Gemini CLI, etc.
Client-side spend tag
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"]
}
}
'
Set extra_body={"metadata": { }} to metadata you want to pass
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();
Pass metadata as part of the request body
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)
Add custom headers to spend tracking
You can add custom headers to the request to track spend and usage.
litellm_settings:
extra_spend_tag_headers: