← 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
src/llm_cost_tracker.py
#!/usr/bin/env python3
"""
LLM Cost Tracker — AI Toolkit (DataNest)
Track LLM API costs in real time: token counting, per-request cost
calculation, budget alerts, usage reports by model, and cost forecasting.
Zero external dependencies — Python 3.10+ stdlib only.
Usage:
python llm_cost_tracker.py --log '{"model":"gpt-4","input_tokens":500,"output_tokens":200}'
python llm_cost_tracker.py --report --db costs.jsonl
python llm_cost_tracker.py --budget 50.00 --db costs.jsonl
python llm_cost_tracker.py --forecast 30 --db costs.jsonl
python llm_cost_tracker.py --demo
"""
from __future__ import annotations
import argparse
import datetime
import json
import logging
import statistics
import sys
import time
from collections import defaultdict
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any
# ---------------------------------------------------------------------------
# Logging
# ---------------------------------------------------------------------------
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
)
logger = logging.getLogger("llm_cost_tracker")
# ---------------------------------------------------------------------------
# Pricing Table — cost per 1K tokens (USD)
# These are representative prices — update them to match your provider's
# current pricing. Prices change frequently; the tracker works with ANY
# values you configure here.
# ---------------------------------------------------------------------------
PRICING: dict[str, dict[str, float]] = {
# OpenAI models
"gpt-4o": {"input": 0.0025, "output": 0.0100},
"gpt-4o-mini": {"input": 0.000150, "output": 0.000600},
"gpt-4-turbo": {"input": 0.0100, "output": 0.0300},
"gpt-4": {"input": 0.0300, "output": 0.0600},
# ... 518 more lines ...