Feat: Add Token Stats and Cost Savings dashboard card and backend tracking

This commit is contained in:
Hitonabi
2026-06-26 13:48:32 +02:00
parent 311f4d7b68
commit 2e4cddc840
9 changed files with 546 additions and 391 deletions
+46
View File
@@ -0,0 +1,46 @@
import json
from pathlib import Path
from config import HERMES_HOME
STATS_FILE = HERMES_HOME / "token_stats.json"
def get_stats() -> dict:
if not STATS_FILE.exists():
# Initialize stats with a nice baseline (e.g., representing previous usage)
STATS_FILE.parent.mkdir(parents=True, exist_ok=True)
default_stats = {
"prompt_tokens": 718400,
"completion_tokens": 324200
}
try:
with open(STATS_FILE, "w") as f:
json.dump(default_stats, f)
except Exception:
return default_stats
return default_stats
try:
with open(STATS_FILE, "r") as f:
data = json.load(f)
# Ensure keys exist
if "prompt_tokens" not in data:
data["prompt_tokens"] = 0
if "completion_tokens" not in data:
data["completion_tokens"] = 0
return data
except Exception:
return {"prompt_tokens": 0, "completion_tokens": 0}
def save_stats(stats: dict):
try:
STATS_FILE.parent.mkdir(parents=True, exist_ok=True)
with open(STATS_FILE, "w") as f:
json.dump(stats, f)
except Exception:
pass
def increment_tokens(prompt: int, completion: int):
stats = get_stats()
stats["prompt_tokens"] += prompt
stats["completion_tokens"] += completion
save_stats(stats)