From 5fedcad624559e67296ff52d81a73c7b06ed0dab Mon Sep 17 00:00:00 2001 From: Andrew Peltekci Date: Sun, 22 Mar 2026 19:14:01 -0700 Subject: [PATCH] fix(usage): track lifetime request total beyond history cap (#366) --- src/lib/usageDb.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/lib/usageDb.js b/src/lib/usageDb.js index ded59b5e..c3a6a3c3 100644 --- a/src/lib/usageDb.js +++ b/src/lib/usageDb.js @@ -66,7 +66,8 @@ if (!isCloud && fs && typeof fs.existsSync === "function") { // Default data structure const defaultData = { - history: [] + history: [], + totalRequestsLifetime: 0 }; // Singleton instance @@ -240,10 +241,14 @@ export async function saveRequestUsage(entry) { if (!Array.isArray(db.data.history)) { db.data.history = []; } + if (typeof db.data.totalRequestsLifetime !== "number") { + db.data.totalRequestsLifetime = db.data.history.length; + } const entryCost = await calculateCost(entry.provider, entry.model, entry.tokens); entry.cost = entryCost; db.data.history.push(entry); + db.data.totalRequestsLifetime += 1; // Cap history to prevent unbounded memory/disk growth const MAX_HISTORY = 10000; @@ -521,8 +526,12 @@ export async function getUsageStats(period = "all") { }) .slice(0, 20); + const lifetimeTotalRequests = typeof db.data.totalRequestsLifetime === "number" + ? db.data.totalRequestsLifetime + : history.length; + const stats = { - totalRequests: history.length, + totalRequests: lifetimeTotalRequests, totalPromptTokens: 0, totalCompletionTokens: 0, totalCost: 0,