← Back to all products
$29
LLM Cost Optimizer
Token usage tracking, model routing for cost/quality tradeoffs, caching strategies, batch processing, and budget alerting.
MarkdownYAMLPythonLLMOpenAI
📄 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 29 files
llm-cost-optimizer/
├── LICENSE
├── README.md
├── configs/
│ └── pricing.yaml
├── free-sample.zip
├── guide/
│ ├── 01-getting-started.md
│ ├── 02-core-features.md
│ └── 03-advanced-features.md
├── index.html
├── requirements.txt
├── src/
│ ├── __init__.py
│ ├── __pycache__/
│ │ ├── __init__.cpython-312.pyc
│ │ ├── budget_alert.cpython-312.pyc
│ │ ├── cache.cpython-312.pyc
│ │ ├── model_router.cpython-312.pyc
│ │ ├── pricing.cpython-312.pyc
│ │ ├── report_generator.cpython-312.pyc
│ │ ├── token_tracker.cpython-312.pyc
│ │ └── tokenizer_estimate.cpython-312.pyc
│ ├── budget_alert.py
│ ├── cache.py
│ ├── model_router.py
│ ├── pricing.py
│ ├── report_generator.py
│ ├── token_tracker.py
│ └── tokenizer_estimate.py
└── tests/
├── __pycache__/
│ ├── conftest.cpython-312.pyc
│ └── test_optimizer.cpython-312.pyc
├── conftest.py
└── test_optimizer.py
📖 Documentation Preview README excerpt
LLM Cost Optimizer
Track, analyse, and reduce LLM API spending with token accounting, intelligent model routing, response caching, budget alerts, and cost reporting.
Most teams discover they're spending 3-5x more than necessary on LLM APIs. This toolkit gives you per-request cost tracking, a model router that picks the cheapest model for each quality tier, exact-match response caching (typically 20-40% savings), budget alerting, and a report generator that produces HTML dashboards and Markdown summaries — all with a stdlib-only token estimator so you can project costs before making any API calls.
What You Get
- Token usage tracker with per-request logging, model breakdowns, and JSONL persistence
- Pricing table for OpenAI and Anthropic models with YAML-driven updates (no code changes needed)
- Model router that classifies prompts by complexity and selects the cheapest qualifying model
- Response cache with SHA-256 exact-match, TTL expiration, LRU eviction, and JSON persistence
- Budget monitor with daily/weekly/monthly thresholds, percentage-based warnings, and custom alert callbacks
- Token estimator (stdlib-only) with content-type detection for English prose, code, technical, and multilingual text
- Report generator producing both Markdown tables and a styled HTML cost dashboard
- Comprehensive test suite covering pricing, tracking, routing, caching, budgeting, and estimation
File Tree
llm-cost-optimizer/
├── README.md
├── LICENSE
├── requirements.txt
├── src/
│ ├── __init__.py # Package init
│ ├── token_tracker.py # UsageEvent dataclass + TokenTracker
│ ├── pricing.py # ModelPricing + PricingTable (YAML/defaults)
│ ├── model_router.py # Heuristic tier classifier + cost-aware routing
│ ├── cache.py # ResponseCache with TTL + LRU + persistence
│ ├── budget_alert.py # BudgetMonitor with AlertLevel enum
│ ├── report_generator.py # Markdown + HTML dashboard reports
│ └── tokenizer_estimate.py # Stdlib-only token estimation
├── configs/
│ └── pricing.yaml # Per-model pricing (USD per 1K tokens)
└── tests/
├── conftest.py # Path setup
└── test_optimizer.py # Full test suite
Quick Start
1. Install Dependencies
pip install -r requirements.txt
Only PyYAML is required (for pricing config). The core modules work without it using built-in defaults.
2. Track API Calls
from src.pricing import PricingTable
from src.token_tracker import TokenTracker
pricing = PricingTable("configs/pricing.yaml")
tracker = TokenTracker(log_path="usage.jsonl", pricing=pricing)
*... continues with setup instructions, usage examples, and more.*
📄 Code Sample .py preview
src/budget_alert.py
"""
Budget Alerting — LLM Cost Optimizer
=======================================
Monitor spending against configurable budget thresholds and trigger
alerts when limits are approached or exceeded.
By Datanest Digital — support@datanest.dev
"""
from __future__ import annotations
import logging
from dataclasses import dataclass, field
from datetime import datetime, timezone
from enum import Enum
from typing import Any, Callable, Dict, List, Optional
logger = logging.getLogger(__name__)
class AlertLevel(Enum):
INFO = "info"
WARNING = "warning"
CRITICAL = "critical"
@dataclass
class BudgetAlert:
"""A triggered budget alert.
Attributes:
level: Severity of the alert.
message: Human-readable alert message.
current_spend: Current spending amount in USD.
budget_limit: The budget limit that was breached.
timestamp: When the alert was triggered.
"""
level: AlertLevel = AlertLevel.INFO
message: str = ""
current_spend: float = 0.0
budget_limit: float = 0.0
timestamp: str = field(
default_factory=lambda: datetime.now(timezone.utc).isoformat()
)
class BudgetMonitor:
"""Monitor LLM spending against budget thresholds.
Set daily, weekly, and monthly budgets. The monitor checks
spending after each recorded event and triggers callbacks when
# ... 85 more lines ...