Skip to main content

llm-spend

Track and limit LLM API spending in real-time. Drop-in wrapper for OpenAI, Anthropic, Google, and any OpenAI-compatible API. Know exactly where your money goes. Set budgets. Get alerts before you go broke.

The Pain

You're burning $50/day on LLM APIs and have no idea which feature, model, or prompt is responsible. You find out when the invoice arrives.

Install

pip install llm-spend

Quick Start

Wrap your OpenAI client

from llm_spend import track
import openai

client = openai.OpenAI()
tracked = track(client, budget=50.0)  # $50 budget

# Use exactly like normal - spending is tracked automatically
response = tracked.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello"}]
)

# Check spending anytime
print(tracked.spending.report())
# Total: $0.42 | gpt-4o: $0.38 (12 calls) | gpt-3.5-turbo: $0.04 (45 calls)

Set budgets with alerts

tracked = track(client,
    budget=100.0,           # Hard limit - raises BudgetExceeded at $100
    warn_at=0.8,            # Callback at 80%
    on_warn=lambda s: print(f"⚠️ {s.total_cost:.2f}/{s.budget}"),
    on_budget=lambda s: notify_slack(s),  # Custom handler
    reset="daily",          # Reset budget daily/weekly/monthly
)

Track by label (know WHERE money goes)

response = tracked.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    extra_headers={"X-LLM-Spend-Label": "summarizer"},  # tag this call
)

# Later:
for label, cost in tracked.spending.by_label().items():
    print(f"  {label}: ${cost:.2f}")
# summarizer: $12.50
# classifier: $3.20
# chatbot: $45.00

CLI dashboard

# Show live spending from log file
llm-spend report --file spending.jsonl

# Set environment budget (works with any wrapped client)
export LLM_SPEND_BUDGET=50
export LLM_SPEND_WARN=0.8
export LLM_SPEND_RESET=daily

Cost Database

Built-in pricing for 50+ models (updated regularly):

Provider Models
OpenAI GPT-4o, GPT-4o-mini, GPT-4-Turbo, GPT-3.5-Turbo, o1, o1-mini, o3-mini
Anthropic Claude 4 Opus/Sonnet, Claude 3.5 Sonnet/Haiku, Claude 3 Opus/Sonnet/Haiku
Google Gemini 2.0/1.5 Pro/Flash
Meta Llama 3.x (via API providers)
Mistral Mistral Large/Medium/Small

Custom model costs:

tracked = track(client, custom_costs={
    "my-fine-tuned-model": (0.005, 0.015),  # (input_per_1k, output_per_1k)
})

API Reference

# Core
tracked = track(client, budget=None, warn_at=0.8, reset=None)
tracked.spending.total_cost        # float - total $ spent
tracked.spending.by_model()        # dict[str, float]
tracked.spending.by_label()        # dict[str, float]
tracked.spending.call_count         # int
tracked.spending.report()          # str - formatted report
tracked.spending.reset()           # manually reset counters
tracked.spending.to_jsonl(path)    # export log

# Exceptions
from llm_spend import BudgetExceeded

Features

  • Zero config — wrap client, get tracking
  • Accurate pricing — 50+ models with per-token costs
  • Budget enforcement — hard limits that prevent overspend
  • Labels — attribute costs to features/teams/users
  • CLI — terminal dashboard and reports
  • Async support — works with async OpenAI client
  • No dependencies — pure Python, no external packages
  • Thread-safe — safe for concurrent use

License

MIT

Metadata

Release files for llm-spend-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 llm-spend-tracker 0.1.0
File Size Uploaded Provenance
llm_spend_tracker-0.1.0.tar.gz 8.4 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llm-spend-tracker 0.1.0
File Interpreter ABI Platform
llm_spend_tracker-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 17.6 kB

Release files / llm_spend_tracker-0.1.0.tar.gz

Download URL llm_spend_tracker-0.1.0.tar.gz
Size 8.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8aae108aaa7afea492673e4d3e6ca406937516edcaface94b424da8a6ef11c4b
BLAKE2b-256 checksum
How to use checksums
db6b300bb68c4f22b84ad2553f1ddcc1da161a7cf57fd2d4c09bae85545f6cc9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

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

Download URL llm_spend_tracker-0.1.0-py3-none-any.whl
Size 9.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a7761ad2232115956a94fc42b63b9642522d5985106c031abdd18b0bddf6a013
BLAKE2b-256 checksum
How to use checksums
4a4ec17e1c94e067d1982044f876ef2c88411ca167216faea382afdbda214807
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.8

Security for llm-spend-tracker 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

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