Add API cost tracking to admin dashboard

Track estimated Anthropic API costs per request across all Claude API
call sites (recommender, analyze, artist-dive, generate-playlist, crate,
rabbit-hole, playlist-fix, timeline, compatibility). Log token usage and
estimated cost to the app logger. Aggregate costs in admin stats endpoint
and display total/today costs and token usage in the admin dashboard.
This commit is contained in:
root
2026-03-31 20:51:51 -05:00
parent 0ee8f9a144
commit f2b8dadbf8
7 changed files with 104 additions and 3 deletions

View File

@@ -1,5 +1,6 @@
import logging
import io
import re
from datetime import datetime, timezone, timedelta
from fastapi import APIRouter, Depends, HTTPException, Query
@@ -113,6 +114,28 @@ async def get_stats(
for row in user_stats_result.all()
]
# Calculate API costs from log buffer
total_cost = 0.0
today_cost = 0.0
total_tokens_in = 0
total_tokens_out = 0
for log in _log_buffer:
if 'API_COST' in log.get('message', ''):
msg = log['message']
cost_match = re.search(r'cost=\$([0-9.]+)', msg)
if cost_match:
c = float(cost_match.group(1))
total_cost += c
# Check if today
if log['timestamp'][:10] == now.strftime('%Y-%m-%d'):
today_cost += c
input_match = re.search(r'input=(\d+)', msg)
output_match = re.search(r'output=(\d+)', msg)
if input_match:
total_tokens_in += int(input_match.group(1))
if output_match:
total_tokens_out += int(output_match.group(1))
return {
"users": {
"total": total_users,
@@ -131,6 +154,12 @@ async def get_stats(
"saved": saved_recs,
"disliked": disliked_recs,
},
"api_costs": {
"total_estimated": round(total_cost, 4),
"today_estimated": round(today_cost, 4),
"total_input_tokens": total_tokens_in,
"total_output_tokens": total_tokens_out,
},
"user_breakdown": user_breakdown,
}