From cb6c2a8b5f57a1fea883a3e00f92628d2ad7e017 Mon Sep 17 00:00:00 2001 From: dtzp555 Date: Sun, 12 Apr 2026 09:44:18 +1000 Subject: [PATCH] fix: fallback to stale cache on usage API 429 + extend cache to 15min When the /api/oauth/usage endpoint returns 429 (rate limit), fall back to the most recent cached data instead of returning an error. Also extended cache TTL from 5min to 15min to reduce API calls. Co-Authored-By: Claude Opus 4.6 --- server.mjs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/server.mjs b/server.mjs index 1ed3e52..0222bf1 100644 --- a/server.mjs +++ b/server.mjs @@ -664,7 +664,7 @@ function completionResponse(res, id, model, content) { // Caches the result for 5 minutes to avoid excessive API calls. let usageCache = { data: null, fetchedAt: 0 }; -const USAGE_CACHE_TTL = 300000; // 5 min +const USAGE_CACHE_TTL = 900000; // 15 min const OAUTH_CLIENT_ID = "9d1c250a-e61b-44d9-88ed-5944d1962f5e"; const OAUTH_TOKEN_URL = "https://platform.claude.com/v1/oauth/token"; const OAUTH_BETA_HEADER = "oauth-2025-04-20"; @@ -853,6 +853,9 @@ async function handleUsage(_req, res) { data = await fetchUsageFromApi(); if (!data.error) { usageCache = { data, fetchedAt: now }; + } else if (usageCache.data) { + // Fallback to stale cache on error (e.g. 429 rate limit) + data = { ...usageCache.data, _stale: true, _fetchError: data.error }; } } // Always attach live model stats and proxy stats (not cached) @@ -922,7 +925,11 @@ async function handleStatus(_req, res) { usage = usageCache.data; } else { usage = await fetchUsageFromApi(); - if (!usage.error) usageCache = { data: usage, fetchedAt: now }; + if (!usage.error) { + usageCache = { data: usage, fetchedAt: now }; + } else if (usageCache.data) { + usage = { ...usageCache.data, _stale: true }; + } } // Auth