claude-usage-dashboard 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,52 @@
1
+ export const MODEL_PRICING = {
2
+ 'claude-opus-4-6': {
3
+ input_price_per_mtok: 15,
4
+ output_price_per_mtok: 75,
5
+ cache_read_price_per_mtok: 1.5,
6
+ cache_creation_price_per_mtok: 18.75,
7
+ },
8
+ 'claude-sonnet-4-6': {
9
+ input_price_per_mtok: 3,
10
+ output_price_per_mtok: 15,
11
+ cache_read_price_per_mtok: 0.30,
12
+ cache_creation_price_per_mtok: 3.75,
13
+ },
14
+ 'claude-haiku-4-5': {
15
+ input_price_per_mtok: 0.80,
16
+ output_price_per_mtok: 4,
17
+ cache_read_price_per_mtok: 0.08,
18
+ cache_creation_price_per_mtok: 1.0,
19
+ },
20
+ };
21
+
22
+ export const PLAN_DEFAULTS = {
23
+ pro: 20,
24
+ max5x: 100,
25
+ max20x: 200,
26
+ };
27
+
28
+ export function getModelPricing(modelId) {
29
+ return MODEL_PRICING[modelId] || null;
30
+ }
31
+
32
+ /**
33
+ * Calculate the API cost for a single usage record.
34
+ * Returns 0 for unknown models.
35
+ *
36
+ * In Claude Code logs, input_tokens is the non-cached input.
37
+ * cache_read_tokens and cache_creation_tokens are separate, additive fields.
38
+ * cost = input * input_rate + cache_read * read_rate + cache_creation * write_rate + output * output_rate
39
+ */
40
+ export function calculateRecordCost(record) {
41
+ const pricing = MODEL_PRICING[record.model];
42
+ if (!pricing) return 0;
43
+
44
+ const M = 1_000_000;
45
+
46
+ return (
47
+ (record.input_tokens / M) * pricing.input_price_per_mtok +
48
+ (record.cache_read_tokens / M) * pricing.cache_read_price_per_mtok +
49
+ (record.cache_creation_tokens / M) * pricing.cache_creation_price_per_mtok +
50
+ (record.output_tokens / M) * pricing.output_price_per_mtok
51
+ );
52
+ }
@@ -0,0 +1,87 @@
1
+ import { Router } from 'express';
2
+ import { parseLogDirectory } from '../parser.js';
3
+ import { filterByDateRange, autoGranularity, aggregateByTime, aggregateBySession, aggregateByProject, aggregateByModel, aggregateCache } from '../aggregator.js';
4
+ import { calculateRecordCost, PLAN_DEFAULTS } from '../pricing.js';
5
+
6
+ export function createApiRouter(logBaseDir) {
7
+ const router = Router();
8
+ let allRecords = [];
9
+ try {
10
+ allRecords = parseLogDirectory(logBaseDir);
11
+ console.log(`Parsed ${allRecords.length} records from ${logBaseDir}`);
12
+ } catch (err) {
13
+ console.error('Failed to parse log directory:', err.message);
14
+ }
15
+
16
+ function applyFilters(query) {
17
+ let records = filterByDateRange(allRecords, query.from, query.to);
18
+ if (query.project) records = records.filter(r => r.project === query.project);
19
+ if (query.model) records = records.filter(r => r.model === query.model);
20
+ return records;
21
+ }
22
+
23
+ router.get('/usage', (req, res) => {
24
+ try {
25
+ const records = applyFilters(req.query);
26
+ const granularity = req.query.granularity || autoGranularity(req.query.from, req.query.to);
27
+ const buckets = aggregateByTime(records, granularity);
28
+ const total = { input_tokens: 0, output_tokens: 0, cache_read_tokens: 0, cache_creation_tokens: 0, estimated_api_cost_usd: 0 };
29
+ for (const r of records) {
30
+ total.input_tokens += r.input_tokens; total.output_tokens += r.output_tokens;
31
+ total.cache_read_tokens += r.cache_read_tokens; total.cache_creation_tokens += r.cache_creation_tokens;
32
+ total.estimated_api_cost_usd += calculateRecordCost(r);
33
+ }
34
+ total.estimated_api_cost_usd = Math.round(total.estimated_api_cost_usd * 100) / 100;
35
+ res.json({ granularity, buckets, total });
36
+ } catch (err) {
37
+ res.status(500).json({ error: err.message, code: 'PARSE_ERROR' });
38
+ }
39
+ });
40
+
41
+ router.get('/models', (req, res) => { res.json({ models: aggregateByModel(applyFilters(req.query)) }); });
42
+ router.get('/projects', (req, res) => { res.json({ projects: aggregateByProject(applyFilters(req.query)) }); });
43
+
44
+ router.get('/sessions', (req, res) => {
45
+ const records = applyFilters(req.query);
46
+ let sessions = aggregateBySession(records);
47
+ const sort = req.query.sort || 'date';
48
+ const order = req.query.order || 'desc';
49
+ const sortFn = { date: (a, b) => new Date(b.startTime) - new Date(a.startTime), cost: (a, b) => b.estimated_cost_usd - a.estimated_cost_usd, tokens: (a, b) => b.total_tokens - a.total_tokens }[sort] || ((a, b) => new Date(b.startTime) - new Date(a.startTime));
50
+ sessions.sort(sortFn);
51
+ if (order === 'asc') sessions.reverse();
52
+ const totalTokens = sessions.reduce((sum, s) => sum + s.total_tokens, 0);
53
+ const totalCost = sessions.reduce((sum, s) => sum + s.estimated_cost_usd, 0);
54
+ const page = parseInt(req.query.page) || 1;
55
+ const limit = parseInt(req.query.limit) || 20;
56
+ const totalSessions = sessions.length;
57
+ const totalPages = Math.ceil(totalSessions / limit);
58
+ sessions = sessions.slice((page - 1) * limit, page * limit);
59
+ res.json({ sessions, pagination: { page, limit, total_sessions: totalSessions, total_pages: totalPages }, totals: { total_tokens: totalTokens, estimated_cost_usd: Math.round(totalCost * 100) / 100 } });
60
+ });
61
+
62
+ router.get('/cost', (req, res) => {
63
+ const records = applyFilters(req.query);
64
+ const plan = req.query.plan || 'max5x';
65
+ const customPrice = req.query.customPrice ? parseFloat(req.query.customPrice) : null;
66
+ const subscriptionCost = customPrice || PLAN_DEFAULTS[plan] || 100;
67
+ let apiCost = 0;
68
+ for (const r of records) apiCost += calculateRecordCost(r);
69
+ apiCost = Math.round(apiCost * 100) / 100;
70
+ const dayMap = new Map();
71
+ for (const r of records) {
72
+ const d = new Date(r.timestamp);
73
+ const day = `${d.getFullYear()}-${String(d.getMonth()+1).padStart(2,'0')}-${String(d.getDate()).padStart(2,'0')}`;
74
+ dayMap.set(day, (dayMap.get(day) || 0) + calculateRecordCost(r));
75
+ }
76
+ const costPerDay = Array.from(dayMap.entries()).sort(([a], [b]) => a.localeCompare(b)).map(([date, cost]) => {
77
+ const d = new Date(date);
78
+ const daysInMonth = new Date(d.getUTCFullYear(), d.getUTCMonth() + 1, 0).getUTCDate();
79
+ return { date, api_cost: Math.round(cost * 100) / 100, subscription_daily: Math.round((subscriptionCost / daysInMonth) * 100) / 100 };
80
+ });
81
+ const savings = apiCost - subscriptionCost;
82
+ res.json({ plan, subscription_cost_usd: subscriptionCost, api_equivalent_cost_usd: apiCost, savings_usd: Math.round(savings * 100) / 100, savings_percent: apiCost > 0 ? Math.round((savings / apiCost) * 1000) / 10 : 0, cost_per_day: costPerDay });
83
+ });
84
+
85
+ router.get('/cache', (req, res) => { res.json(aggregateCache(applyFilters(req.query))); });
86
+ return router;
87
+ }