Files
manual_slop/src/cost_tracker.py
2026-03-08 03:11:11 -04:00

62 lines
2.2 KiB
Python

"""
Cost Tracker - Token cost estimation for API calls.
This module provides cost estimation for different LLM providers based on per-token pricing.
It is used to display estimated costs in the MMA Dashboard.
Pricing Data (per 1M tokens):
- gemini-2.5-flash-lite: $0.075 input / $0.30 output
- gemini-3-flash-preview: $0.15 input / $0.60 output
- gemini-3.1-pro-preview: $3.50 input / $10.50 output
- claude-*-sonnet: $3.0 input / $15.0 output
- claude-*-opus: $15.0 input / $75.0 output
- deepseek-v3: $0.27 input / $1.10 output
Usage:
from src.cost_tracker import estimate_cost
total = estimate_cost("gemini-2.5-flash-lite", 50000, 10000)
# Returns: 0.007 (approx)
Accuracy:
- Pricing data may be outdated
- Uses regex matching for model identification
- Returns 0.0 for unknown models
Integration:
- Used by gui_2.py for MMA dashboard cost display
- Called after each API call
See Also:
- src/ai_client.py for token tracking
- docs/guide_mma.md for MMA dashboard documentation
"""
import re
# Pricing per 1M tokens in USD
MODEL_PRICING = [
(r"gemini-2\.5-flash-lite", {"input_per_mtok": 0.075, "output_per_mtok": 0.30}),
(r"gemini-2\.5-flash", {"input_per_mtok": 0.15, "output_per_mtok": 0.60}),
(r"gemini-3-flash-preview", {"input_per_mtok": 0.15, "output_per_mtok": 0.60}),
(r"gemini-3\.1-pro-preview", {"input_per_mtok": 3.50, "output_per_mtok": 10.50}),
(r"claude-.*-sonnet", {"input_per_mtok": 3.0, "output_per_mtok": 15.0}),
(r"claude-.*-opus", {"input_per_mtok": 15.0, "output_per_mtok": 75.0}),
(r"deepseek-v3", {"input_per_mtok": 0.27, "output_per_mtok": 1.10}),
]
def estimate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
"""
Estimate the cost of a model call based on input and output tokens.
Returns the total cost in USD.
"""
if not model:
return 0.0
for pattern, rates in MODEL_PRICING:
if re.search(pattern, model, re.IGNORECASE):
input_cost = (input_tokens / 1_000_000) * rates["input_per_mtok"]
output_cost = (output_tokens / 1_000_000) * rates["output_per_mtok"]
return input_cost + output_cost
return 0.0