← Back to all products
$19
LLM Cost Tracker
Python LLM cost tracker with token counting, per-request cost calculation, and budget alerts.
MarkdownPythonLLM
📄 Product Preview
Try the interactive reader and demo tools below, or get the full product with all content unlocked.
📖 Interactive Reader (Free Preview) ⚙ Try Demo Tools 📦 Download Free Sample📁 File Structure 11 files
llm-cost-tracker/
├── LICENSE
├── README.md
├── examples/
│ ├── basic_usage.py
│ └── sample_costs.jsonl
├── free-sample.zip
├── guide/
│ ├── 01_features.md
│ ├── 02_project-structure.md
│ ├── 03_usage-examples.md
│ └── 04_license.md
├── index.html
└── src/
└── llm_cost_tracker.py
📖 Documentation Preview README excerpt
LLM Cost Tracker
Python LLM cost tracker: token counting, per-request cost calculation, budget alerts, usage reports by model, and cost forecasting. Zero dependencies.
Part of the AI Toolkit collection by [CodeVault](https://ai-toolkit.codevault.dev).
Features
- Cost calculation — Automatic per-request cost from token counts and model pricing
- Pricing table — Built-in pricing for 14+ models (GPT-4o, Claude, Llama, Mixtral, etc.)
- Budget alerts — Real-time budget monitoring with ok/warning/critical/exceeded levels
- Usage reports — Per-model breakdown, daily cost charts, and top expensive requests
- Cost forecasting — Predict future costs based on recent usage patterns
- JSONL database — Append-only log for durability and easy integration
- Prefix matching — Automatically matches model versions (e.g., "gpt-4o-2025-01-01" → "gpt-4o")
- CLI interface — Log requests, check budgets, generate reports from the terminal
Quick Start
# Run demo with simulated API calls
python src/llm_cost_tracker.py --demo
# Show pricing table
python src/llm_cost_tracker.py --pricing
# Log a request
python src/llm_cost_tracker.py --log '{"model":"gpt-4o","input_tokens":500,"output_tokens":200}'
# Check budget
python src/llm_cost_tracker.py --budget 50.00 --db costs.jsonl
# Generate usage report
python src/llm_cost_tracker.py --report --db costs.jsonl
# Forecast next 30 days
python src/llm_cost_tracker.py --forecast 30 --db costs.jsonl
Project Structure
llm-cost-tracker/
├── README.md
├── LICENSE
├── src/
│ └── llm_cost_tracker.py # Core engine (~420 lines)
└── examples/
├── basic_usage.py # Programmatic usage example
└── sample_costs.jsonl # Sample cost data
CLI Reference
| Flag | Description |
|---|---|
--demo | Run demo with simulated data |
--pricing | Show pricing table |
--db FILE | Path to cost database (default: llm_costs.jsonl) |
--log JSON | Log a request with model, input_tokens, output_tokens |
... continues with setup instructions, usage examples, and more.
📄 Code Sample .py preview
examples/basic_usage.py#!/usr/bin/env python3
"""
Basic usage example for the LLM Cost Tracker.
Demonstrates:
- Logging API requests and calculating costs
- Checking budget alerts
- Generating usage reports
- Forecasting future costs
- Using the pricing table and cost calculator
"""
import json
import sys
import tempfile
from pathlib import Path
# Allow running from the examples/ directory
sys.path.insert(0, str(Path(__file__).resolve().parent.parent / "src"))
from llm_cost_tracker import (
PRICING,
CostTracker,
calculate_cost,
estimate_tokens_from_text,
)
def demo_cost_calculation() -> None:
"""Calculate costs for individual API calls."""
print("=== Cost Calculation ===\n")
# Compare costs across models for the same usage
models = ["gpt-4o", "gpt-4o-mini", "claude-sonnet-4", "claude-3-haiku", "llama-3-70b"]
input_tokens = 1000
output_tokens = 500
print(f" Cost for {input_tokens} input + {output_tokens} output tokens:\n")
print(f" {'Model':<25} {'Cost':>10}")
print(f" {'-' * 35}")
for model in models:
cost = calculate_cost(model, input_tokens, output_tokens)