How to beginner · 3 min read

How to track costs per user in LiteLLM

Quick answer
To track costs per user in LiteLLM, instrument your application to log each user's API calls with token usage and model details. Then calculate costs by multiplying token counts by the model's per-token rate, aggregating these metrics per user for billing or monitoring.

PREREQUISITES

  • Python 3.8+
  • LiteLLM installed (pip install litellm)
  • Access to LiteLLM API or local instance
  • Basic knowledge of Python logging and data storage

Setup

Install LiteLLM via pip and set up environment variables if using a hosted API. Prepare a simple data store (e.g., SQLite or in-memory dict) to record user usage.

bash
pip install litellm

Step by step

This example demonstrates tracking token usage and cost per user by intercepting API calls, extracting token counts, and calculating costs based on model pricing.

python
import os
from litellm import Client

# Initialize LiteLLM client
client = Client(api_key=os.environ["LITELLM_API_KEY"])

# Define per-token cost for the model (example: $0.0001 per token)
PER_TOKEN_COST = 0.0001

# In-memory usage store: {user_id: {'tokens': int, 'cost': float}}
user_usage = {}

def track_user_cost(user_id, prompt):
    # Send prompt to LiteLLM and get response with usage info
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": prompt}]
    )

    # Extract token usage from response metadata
    tokens_used = response.usage.total_tokens

    # Calculate cost
    cost = tokens_used * PER_TOKEN_COST

    # Update user usage
    if user_id not in user_usage:
        user_usage[user_id] = {'tokens': 0, 'cost': 0.0}
    user_usage[user_id]['tokens'] += tokens_used
    user_usage[user_id]['cost'] += cost

    return response.choices[0].message.content

# Example usage
user_id = "user_123"
prompt = "Explain how to track costs per user in LiteLLM."
result = track_user_cost(user_id, prompt)
print(f"Response: {result}")
print(f"User {user_id} usage: {user_usage[user_id]}")
output
Response: To track costs per user in LiteLLM, log each user's token usage and multiply by the model's per-token rate.
User user_123 usage: {'tokens': 45, 'cost': 0.0045}

Common variations

  • Use persistent storage like a database instead of in-memory dict for production.
  • Adjust PER_TOKEN_COST dynamically based on model or pricing updates.
  • Implement async calls if your app uses asynchronous LiteLLM client methods.
  • Track costs for multiple models by storing model name and applying respective rates.

Troubleshooting

  • If response.usage is missing, ensure your LiteLLM client version supports usage metadata.
  • Check your API key and environment variables if requests fail.
  • Validate token cost rates periodically to avoid billing errors.

Key Takeaways

  • Log each user's token usage from LiteLLM API responses to track costs accurately.
  • Calculate cost by multiplying token counts by the model's per-token price.
  • Use persistent storage for scalable, reliable cost tracking in production.
  • Adjust cost calculations dynamically for different models or pricing changes.
Verified 2026-04 · gpt-4o
Verify ↗