Skip to main content

A lightweight Python library for tracking LLM API costs via litellm's callback system

Project description

tokencost

A lightweight Python library for tracking LLM API costs via litellm's callback system, with budget alerts and spending limits.

Installation

pip install tokencost

Quick Start

import litellm
from tokencost 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
  • Async support for litellm.acompletion()
  • Per-model cost aggregation via cost_by_model property
  • Automatic exit summary — prints cost report when program ends
  • Thread-safe for concurrent usage
  • Uses litellm's pricing data for accurate costs (1600+ models)

Async Support

import asyncio
import litellm
from tokencost import CostTracker

async def main():
    tracker = CostTracker()
    litellm.callbacks = [tracker]

    # Works with async completions
    response = await litellm.acompletion(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )

    print(f"Cost: ${tracker.total_cost:.6f}")

asyncio.run(main())

Per-Model Cost Breakdown

tracker = CostTracker()
litellm.callbacks = [tracker]

# Make calls to different models...
litellm.completion(model="gpt-4", messages=[...])
litellm.completion(model="gpt-3.5-turbo", messages=[...])
litellm.completion(model="claude-3-sonnet", messages=[...])

# Get cost breakdown by model
for model, cost in tracker.cost_by_model.items():
    print(f"{model}: ${cost:.6f}")

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
    print_summary: bool = True             # Print summary on program exit
)

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
  • cost_by_model: dict[str, float] — Cost aggregated by model name

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

Exit Summary

When your program ends, a cost summary is automatically printed:

==================================================
LLM COST SUMMARY
==================================================
Total Cost:     $0.001959
Total Requests: 4
Budget:         $0.0100 (OK)
Remaining:      $0.008041
--------------------------------------------------
Requests:
  1. gpt-4: 7+18 tokens = $0.000750
  2. gpt-4: 13+17 tokens = $0.000900
  3. gpt-3.5-turbo: 8+82 tokens = $0.000166
  4. gpt-3.5-turbo: 10+58 tokens = $0.000143
==================================================

Disable with print_summary=False.

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/tokencost.git
cd tokencost
pip install -e ".[dev]"
pytest

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

llm_tokencost-0.3.0.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

llm_tokencost-0.3.0-py3-none-any.whl (7.3 kB view details)

Uploaded Python 3

File details

Details for the file llm_tokencost-0.3.0.tar.gz.

File metadata

  • Download URL: llm_tokencost-0.3.0.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for llm_tokencost-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c3d0423fa82748b7b24f30bf3aedef9fff038336df95e166da3740a79a05873e
MD5 c51283afe5746f7f6bf31e34c3532853
BLAKE2b-256 80a1b6b7269aa3b530c2cbeed98be7c811b3c7f61760ffa7e1caf76b456b56c7

See more details on using hashes here.

File details

Details for the file llm_tokencost-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: llm_tokencost-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for llm_tokencost-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b176e660e669885091f880a8e129bfc8abdd49ce462bacc3d907e706ffc00c0c
MD5 58c710505dfbd59c7c1b183d4809da8c
BLAKE2b-256 55f4680afd35d7b10d9eaa78ea0b29d8b1e55561682d7f2380dcf1e11a63e925

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page