Skip to main content

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 USD
  • request_count: int — Number of successful requests
  • history: list[dict] — All logged requests
  • budget: float | None — Configured budget
  • budget_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

Metadata

Release files for litellm-cost-tracker 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for litellm-cost-tracker 0.1.0
File Size Uploaded
litellm_cost_tracker-0.1.0.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for litellm-cost-tracker 0.1.0
File Interpreter ABI Platform
litellm_cost_tracker-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 11.9 kB

Release files / litellm_cost_tracker-0.1.0.tar.gz

Download URL litellm_cost_tracker-0.1.0.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
2d8e93b2ede7546e36509ab29f79a117d1ec7c44be2800fe5a67e844f35287e2
BLAKE2b-256 checksum
How to use checksums
7a82586904d2f28b7ffd77b4ec39521830c21b6bed5452af8a755f115622f8f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release files / litellm_cost_tracker-0.1.0-py3-none-any.whl

Download URL litellm_cost_tracker-0.1.0-py3-none-any.whl
Size 5.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd29cc9e68f5f63e2b45cf27b1a3d7fd9f5b64217e50bde021a4e28181a82014
BLAKE2b-256 checksum
How to use checksums
df087b93592be4be20f12f5f34b26572af1df6acd34de23cf23b7e9529a51546
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page