|
| 1 | +import sqlite3 |
| 2 | +import time |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +SICILY_HOME = Path.home() / ".sicily" |
| 6 | +DB_PATH = SICILY_HOME / "Data" / "usage.db" |
| 7 | + |
| 8 | +# Prices per 1M tokens |
| 9 | +MODEL_PRICING = { |
| 10 | + "gpt-4o-mini": {"input": 0.15 / 1_000_000, "output": 0.60 / 1_000_000}, |
| 11 | + "gpt-5.4-mini": {"input": 0.75 / 1_000_000, "output": 4.50 / 1_000_000}, |
| 12 | + "gpt-5.4-nano": {"input": 0.20 / 1_000_000, "output": 1.25 / 1_000_000}, |
| 13 | + "gpt-4o-mini-transcribe": {"input": 1.25 / 1_000_000, "output": 5 / 1_000_000}, |
| 14 | +} |
| 15 | + |
| 16 | + |
| 17 | +def get_cost(model_name: str, input_tokens: int, output_tokens: int) -> float: |
| 18 | + # Use gpt-4o-mini rates as a fallback if the specific model isn't mapped |
| 19 | + rates = MODEL_PRICING.get(model_name, MODEL_PRICING["gpt-5.4-mini"]) |
| 20 | + return (input_tokens * rates["input"]) + (output_tokens * rates["output"]) |
| 21 | + |
| 22 | + |
| 23 | +def init_db(): |
| 24 | + DB_PATH.parent.mkdir(parents=True, exist_ok=True) |
| 25 | + with sqlite3.connect(DB_PATH) as conn: |
| 26 | + conn.execute(""" |
| 27 | + CREATE TABLE IF NOT EXISTS token_usage ( |
| 28 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 29 | + timestamp REAL, |
| 30 | + dimension TEXT, |
| 31 | + session_id TEXT, |
| 32 | + model_name TEXT, |
| 33 | + input_tokens INTEGER, |
| 34 | + output_tokens INTEGER, |
| 35 | + cost REAL, |
| 36 | + message_id TEXT UNIQUE |
| 37 | + ) |
| 38 | + """) |
| 39 | + |
| 40 | + |
| 41 | +def record_usage(dimension: str, session_id: str, model_name: str, input_tokens: int, output_tokens: int, message_id: str = None): |
| 42 | + init_db() |
| 43 | + cost = get_cost(model_name, input_tokens, output_tokens) |
| 44 | + with sqlite3.connect(DB_PATH) as conn: |
| 45 | + conn.execute( |
| 46 | + """ |
| 47 | + INSERT OR IGNORE INTO token_usage |
| 48 | + (timestamp, dimension, session_id, model_name, input_tokens, output_tokens, cost, message_id) |
| 49 | + VALUES (?, ?, ?, ?, ?, ?, ?, ?) |
| 50 | + """, |
| 51 | + (time.time(), dimension, session_id, model_name, input_tokens, output_tokens, cost, message_id) |
| 52 | + ) |
| 53 | + |
| 54 | + |
| 55 | +def cleanup_old_records(): |
| 56 | + """Keep only the last 30 days of usage.""" |
| 57 | + cutoff = time.time() - (30 * 24 * 60 * 60) |
| 58 | + with sqlite3.connect(DB_PATH) as conn: |
| 59 | + conn.execute("DELETE FROM token_usage WHERE timestamp < ?", (cutoff,)) |
| 60 | + |
| 61 | + |
| 62 | +def get_usage_report(timeframe="week") -> list[dict]: |
| 63 | + """ |
| 64 | + Returns aggregated usage data. |
| 65 | + timeframe can be: 'session', 'day', or 'week'. |
| 66 | + """ |
| 67 | + with sqlite3.connect(DB_PATH) as conn: |
| 68 | + conn.row_factory = sqlite3.Row |
| 69 | + cursor = conn.cursor() |
| 70 | + |
| 71 | + if timeframe == "session": |
| 72 | + cursor.execute("SELECT session_id FROM token_usage ORDER BY timestamp DESC LIMIT 1") |
| 73 | + row = cursor.fetchone() |
| 74 | + if not row: |
| 75 | + return [] |
| 76 | + |
| 77 | + cursor.execute(""" |
| 78 | + SELECT dimension, model_name, SUM(input_tokens) as in_tokens, SUM(output_tokens) as out_tokens, SUM(cost) as total_cost |
| 79 | + FROM token_usage |
| 80 | + WHERE session_id = ? |
| 81 | + GROUP BY dimension, model_name |
| 82 | + """, (row["session_id"],)) |
| 83 | + else: |
| 84 | + days = 1 if timeframe == "day" else 7 |
| 85 | + cutoff = time.time() - (days * 24 * 60 * 60) |
| 86 | + cursor.execute(""" |
| 87 | + SELECT dimension, model_name, SUM(input_tokens) as in_tokens, SUM(output_tokens) as out_tokens, SUM(cost) as total_cost |
| 88 | + FROM token_usage |
| 89 | + WHERE timestamp >= ? |
| 90 | + GROUP BY dimension, model_name |
| 91 | + """, (cutoff,)) |
| 92 | + |
| 93 | + return [dict(row) for row in cursor.fetchall()] |
0 commit comments