-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusage_tracker.py
More file actions
147 lines (126 loc) · 5.07 KB
/
Copy pathusage_tracker.py
File metadata and controls
147 lines (126 loc) · 5.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import sqlite3
import time
from pathlib import Path
SICILY_HOME = Path.home() / ".sicily"
DB_PATH = SICILY_HOME / "Data" / "usage.db"
# Prices per 1M tokens (including cached input where available)
MODEL_PRICING = {
"gpt-4o-mini": {
"input": 0.15 / 1_000_000,
"cached_input": 0.075 / 1_000_000,
"output": 0.60 / 1_000_000
},
"gpt-5.4-mini": {
"input": 0.75 / 1_000_000,
"cached_input": 0.075 / 1_000_000,
"output": 4.50 / 1_000_000
},
"gpt-5.4-nano": {
"input": 0.20 / 1_000_000,
"cached_input": 0.020 / 1_000_000,
"output": 1.25 / 1_000_000
},
"gpt-5.6-luna": {
"input": 0.20 / 1_000_000,
"cached_input": 0.020 / 1_000_000,
"output": 1.20 / 1_000_000
},
"gpt-5-nano": {
"input": 0.05 / 1_000_000,
"cached_input": 0.010 / 1_000_000,
"output": 0.40 / 1_000_000
},
"gpt-4o-mini-transcribe": {
"input": 1.25 / 1_000_000,
"output": 5 / 1_000_000
},
}
def get_cost(model_name: str, input_tokens: int, output_tokens: int, cached_input_tokens: int = 0) -> float:
"""Calculate cost with support for cached input tokens."""
rates = MODEL_PRICING.get(model_name, MODEL_PRICING.get("gpt-5.4-mini", {}))
input_cost = (input_tokens - cached_input_tokens) * rates.get("input", 0.75 / 1_000_000)
cached_cost = cached_input_tokens * rates.get("cached_input", 0.075 / 1_000_000)
output_cost = output_tokens * rates.get("output", 4.50 / 1_000_000)
return input_cost + cached_cost + output_cost
def init_db():
"""Initialize DB and ensure all columns exist for backward compatibility."""
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
with sqlite3.connect(DB_PATH) as conn:
# Create table if it doesn't exist
conn.execute("""
CREATE TABLE IF NOT EXISTS token_usage (
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp REAL,
dimension TEXT,
session_id TEXT,
model_name TEXT,
input_tokens INTEGER,
output_tokens INTEGER,
cached_input_tokens INTEGER DEFAULT 0,
cost REAL,
message_id TEXT UNIQUE
)
""")
try:
conn.execute("ALTER TABLE token_usage ADD COLUMN cached_input_tokens INTEGER DEFAULT 0")
print("Added missing column: cached_input_tokens")
except sqlite3.OperationalError as e:
if "duplicate column name" not in str(e).lower():
print(f"Warning adding column: {e}")
conn.commit()
def record_usage(dimension: str, session_id: str, model_name: str,
input_tokens: int, output_tokens: int,
cached_input_tokens: int = 0, message_id: str = None):
init_db()
cost = get_cost(model_name, input_tokens, output_tokens, cached_input_tokens)
with sqlite3.connect(DB_PATH) as conn:
conn.execute(
"""
INSERT OR IGNORE INTO token_usage
(timestamp, dimension, session_id, model_name,
input_tokens, output_tokens, cached_input_tokens, cost, message_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
""",
(time.time(), dimension, session_id, model_name,
input_tokens, output_tokens, cached_input_tokens, cost, message_id)
)
def cleanup_old_records():
"""Keep only the last 30 days of usage."""
cutoff = time.time() - (30 * 24 * 60 * 60)
with sqlite3.connect(DB_PATH) as conn:
conn.execute("DELETE FROM token_usage WHERE timestamp < ?", (cutoff,))
def get_usage_report(timeframe="week") -> list[dict]:
"""
Returns aggregated usage data.
timeframe can be: 'session', 'day', or 'week'.
"""
with sqlite3.connect(DB_PATH) as conn:
conn.row_factory = sqlite3.Row
cursor = conn.cursor()
if timeframe == "session":
cursor.execute("SELECT session_id FROM token_usage ORDER BY timestamp DESC LIMIT 1")
row = cursor.fetchone()
if not row:
return []
cursor.execute("""
SELECT dimension, model_name,
SUM(input_tokens) as in_tokens,
SUM(output_tokens) as out_tokens,
SUM(cost) as total_cost
FROM token_usage
WHERE session_id = ?
GROUP BY dimension, model_name
""", (row["session_id"],))
else:
days = 1 if timeframe == "day" else 7
cutoff = time.time() - (days * 24 * 60 * 60)
cursor.execute("""
SELECT dimension, model_name,
SUM(input_tokens) as in_tokens,
SUM(output_tokens) as out_tokens,
SUM(cost) as total_cost
FROM token_usage
WHERE timestamp >= ?
GROUP BY dimension, model_name
""", (cutoff,))
return [dict(row) for row in cursor.fetchall()]