A lightweight Python library for tracking LLM API costs via litellm's callback system
Project description
LLM Cost Tracker
A lightweight Python library for tracking LLM API costs via litellm's callback system, with budget alerts and spending limits.
Installation
pip install litellm-cost-tracker
Quick Start
import litellm
from llm_cost import CostTracker, BudgetExceededError
# Create a tracker with a $5 budget
def alert(tracker):
print(f"Budget exceeded! Spent ${tracker.total_cost:.2f}")
tracker = CostTracker(
budget=5.00,
on_budget_exceeded=alert,
raise_on_budget=True
)
# Register with litellm
litellm.callbacks = [tracker]
# Make LLM calls as usual
try:
response = litellm.completion(
model="gpt-4",
messages=[{"role": "user", "content": "Hello!"}]
)
except BudgetExceededError as e:
print(f"Stopped at ${e.total_cost:.2f} (budget: ${e.budget:.2f})")
# Check usage
print(f"Total: ${tracker.total_cost:.4f} across {tracker.request_count} requests")
Features
- Real-time cost tracking during LLM calls
- Budget alerts via callback and/or exception
- In-memory storage (no persistence needed)
- Uses litellm's pricing data for accurate costs
API Reference
CostTracker
CostTracker(
budget: float | None = None, # Spending limit in USD
on_budget_exceeded: Callable | None = None, # Callback when exceeded
raise_on_budget: bool = False # Raise exception when exceeded
)
Properties:
total_cost: float— Running total in USDrequest_count: int— Number of successful requestshistory: list[dict]— All logged requestsbudget: float | None— Configured budgetbudget_exceeded: bool— Whether budget has been exceeded
Methods:
reset()— Clear all tracked data
BudgetExceededError
Raised when budget is exceeded (if raise_on_budget=True).
class BudgetExceededError(Exception):
budget: float # Configured budget
total_cost: float # Actual spend when exceeded
History Entry Format
Each request is logged with:
{
"model": "gpt-4",
"prompt_tokens": 150,
"completion_tokens": 50,
"cost": 0.0123,
"timestamp": "2026-02-22T10:30:00Z"
}
Development
git clone https://github.com/Paawan13/litellm-cost-tracker.git
cd litellm-cost-tracker
pip install -e ".[dev]"
pytest
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file litellm_cost_tracker-0.1.0.tar.gz.
File metadata
- Download URL: litellm_cost_tracker-0.1.0.tar.gz
- Upload date:
- Size: 6.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d8e93b2ede7546e36509ab29f79a117d1ec7c44be2800fe5a67e844f35287e2
|
|
| MD5 |
d4c0fc25bf42def02a04b35c5c8795f1
|
|
| BLAKE2b-256 |
7a82586904d2f28b7ffd77b4ec39521830c21b6bed5452af8a755f115622f8f1
|
File details
Details for the file litellm_cost_tracker-0.1.0-py3-none-any.whl.
File metadata
- Download URL: litellm_cost_tracker-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dd29cc9e68f5f63e2b45cf27b1a3d7fd9f5b64217e50bde021a4e28181a82014
|
|
| MD5 |
3a47b8d0e123d1bae26870bd13391f8a
|
|
| BLAKE2b-256 |
df087b93592be4be20f12f5f34b26572af1df6acd34de23cf23b7e9529a51546
|