Skip to content

fatihkutlar/llm-cost-tracker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

llm-cost-tracker

Track the real cost of every LLM API call across multiple providers — with just 3 lines of code.

Built for teams and solo developers who want visibility into their AI spend without complex setup.

Features

  • Multi-provider support — OpenAI, Anthropic, Google Gemini, Cohere, Moonshot (Kimi)
  • Zero-config default — SQLite storage out of the box, no database setup needed
  • Sync & async — Works with both synchronous and asynchronous LLM calls
  • Auto-detection — Automatically identifies provider and model from API responses
  • CLI tools — Summary tables, top-N expensive calls, CSV/JSON export
  • Visual dashboard — Streamlit-powered cost breakdown charts
  • Accurate pricing — Built-in token pricing for 30+ models, updated for 2026

Supported Providers & Models

Provider Models
OpenAI gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, o3, o4-mini, o3-mini, gpt-4o, gpt-4o-mini
Anthropic claude-opus-4-6, claude-sonnet-4-6, claude-haiku-4-5, claude-sonnet-4-5, claude-opus-4-5, claude-opus-4-1, claude-sonnet-4, claude-opus-4
Google gemini-2.5-pro, gemini-2.5-flash, gemini-2.5-flash-lite, gemini-2.0-flash, gemini-2.0-flash-lite
Cohere command-r-plus, command-r, command-light, command
Moonshot kimi-k2.5

Installation

git clone https://github.com/fatihkutlar/llm-cost-tracker.git
cd llm-cost-tracker
pip install -e .

pip install llm-cost-tracker will be available once published to PyPI.

Quick Start

from llm_cost_tracker import track_cost, configure

# Optional: configure storage backend and budget
configure(storage='sqlite', budget_usd=50.0)

@track_cost(project='email-classifier', tags=['prod'])
def classify_email(text: str) -> dict:
    response = anthropic_client.messages.create(
        model="claude-sonnet-4-6",
        messages=[{"role": "user", "content": text}],
    )
    return response

# That's it. Every call to classify_email() is now tracked
# with token counts, cost, latency, and metadata.
result = classify_email("Is this email spam?")

Works with any LLM client that returns standard response objects — OpenAI, Anthropic, Moonshot/Kimi, and any OpenAI-compatible API.

Async Support

@track_cost(project='chatbot', tags=['async', 'prod'])
async def chat(prompt: str):
    response = await openai_client.chat.completions.create(
        model="gpt-4.1",
        messages=[{"role": "user", "content": prompt}],
    )
    return response

CLI

# Cost summary for the last 7 days
llm-cost summary --period 7d

# Top 10 most expensive calls
llm-cost top --n 10

# Export all records as CSV or JSON
llm-cost export --format csv
llm-cost export --format json

# Launch interactive Streamlit dashboard
llm-cost dashboard

Example Output

Provider    Model             Calls    Input Tokens    Output Tokens  Cost (USD)
----------  ---------------  -------  --------------  ---------------  ----------
anthropic   claude-sonnet-4-6     42          84000            21000  $0.5670
openai      gpt-4.1              18          36000             9000  $0.1440
moonshot    kimi-k2.5             7          14000             3500  $0.0280

Storage Backends

SQLite (default — zero config, works everywhere):

configure(storage='sqlite')
# Data stored in ./llm_costs.db

PostgreSQL (opt-in — for production/team use):

configure(storage='postgres', db_path='postgresql://user:pass@localhost/dbname')
# Or set the LLM_COST_TRACKER_PG_DSN environment variable

Data Model

Every tracked call stores a CostRecord with:

Field Type Description
timestamp datetime (UTC) When the call was made
provider str openai, anthropic, google, cohere, moonshot
model str Model identifier
project str | None Project name from @track_cost
tags list[str] Custom tags for filtering
input_tokens int Prompt/input token count
output_tokens int Completion/output token count
cost_usd float Calculated cost in USD
latency_ms int Call duration in milliseconds

How It Works

  1. The @track_cost decorator wraps your LLM function
  2. On each call, it measures latency and captures the response
  3. Token usage is extracted from the response (supports OpenAI and Anthropic formats)
  4. Cost is calculated using built-in per-model pricing tables
  5. A CostRecord is written to your configured storage backend

No monkey-patching required. No SDK modifications. Just a decorator.

Development

git clone https://github.com/fatihkutlar/llm-cost-tracker.git
cd llm-cost-tracker
pip install -e ".[dev]"
pytest tests/ -v

79 tests | 85% coverage | Python 3.10+

Roadmap

  • PyPI release
  • Budget alerting (v1.1)
  • Live price updates from provider APIs
  • DeepSeek, Mistral, Llama provider support
  • Per-project budget limits

License

MIT

About

Track the real cost of every LLM API call across multiple providers — with just 3 lines of code. Built for teams and solo developers who want visibility into their AI spend without complex setup.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages