Skip to content

7.4 billion tokens per month. 34 free LLM providers. 635 free model endpoints. All behind one /v1 endpoint, plus any custom OpenAI-compatible endpoint. Smart routing, automatic failover, encrypted keys. Personal experimentation only.

TypeScript

fix(budget): request counts and daily caps are not monthly token budgets (#1415)

Tashfeen263598703b49
diff --git a/server/src/__tests__/lib/budget.test.ts b/server/src/__tests__/lib/budget.test.ts
index 96fe543d..1d8b9c5e 100644
--- a/server/src/__tests__/lib/budget.test.ts
+++ b/server/src/__tests__/lib/budget.test.ts
@@ -26,6 +26,40 @@ describe('parseBudget', () => {
expect(parseBudget('')).toBe(0);
expect(parseBudget(undefined as unknown as string)).toBe(0);
});
+
+ it('never reads a request count as tokens (live catalog labels)', () => {
+ // These three labels are on real catalog rows today. The old parse turned
+ // the first two into bogus token budgets (14,400 and 1,000 tokens/month),
+ // which pinned those models to the headroom floor after a handful of
+ // requests; the third is a request rate whose K belongs to "K rpd".
+ expect(parseBudget('free · 14.4K rpd')).toBe(0);
+ expect(parseBudget('1K calls/mo shared')).toBe(0);
+ expect(parseBudget('~1,000 calls/mo shared trial key')).toBe(0);
+ expect(parseBudget('10K neurons/day (shared)')).toBe(0);
+ });
+
+ it('scales a per-day token cap to the monthly column (live catalog labels)', () => {
+ // The column is a MONTHLY budget; a pool that refills every day renews 30
+ // times a month. Read as-is, these looked exhausted on day one and the
+ // headroom guardrail sat the model at the floor for the other 29 days.
+ expect(parseBudget('free · 500K tokens/day (1M with Telegram)')).toBe(15_000_000);
+ expect(parseBudget('free · 1M tokens/24h')).toBe(30_000_000);
+ expect(parseBudget('free · 7M/day shared')).toBe(210_000_000);
+ expect(parseBudget('free · 20k tok/day')).toBe(600_000);
+ });
+
+ it('never reads a time or month unit as a magnitude', () => {
+ expect(parseBudget('free 5min window')).toBe(0);
+ expect(parseBudget('1mo trial')).toBe(0);
+ expect(parseBudget('free · 20k tok/day')).toBe(600_000);
+ });
+
+ it('keeps monthly labels unaffected by the daily scaling', () => {
+ expect(parseBudget('~3M (1k credits)')).toBe(3_000_000);
+ expect(parseBudget('free · $10/month shared API credits')).toBe(0);
+ expect(parseBudget('$0.10/mo shared credit')).toBe(0);
+ expect(parseBudget('~120M/mo, 1M/day max')).toBe(120_000_000);
+ });
});
describe('monthlyBudgetScore', () => {
diff --git a/server/src/lib/budget.ts b/server/src/lib/budget.ts
index f047b6e4..186f9d74 100644
--- a/server/src/lib/budget.ts
+++ b/server/src/lib/budget.ts
@@ -2,20 +2,54 @@
// '~12M', or '~500K'. Parse the upper bound to an absolute token count for
// quota math (headroom guardrail, token-usage bar). Returns 0 for unknown/empty
// labels, which callers treat as "no budget info".
+//
+// Two label shapes the live catalog carries (checked against the signed
+// catalog at api.freellmapi.co/v1/latest, 2026-10-04) need more than the bare
+// number+unit match:
+//
+// - request counts that ride on a K/M unit: 'free · 14.4K rpd',
+// '1K calls/mo shared'. These count requests, not tokens. The old parse
+// read 'free · 14.4K rpd' as a 14,400-TOKEN monthly budget, so
+// headroomFactor reported the model spent after a handful of requests and
+// pinned it to the score floor for the rest of the month — the same class
+// of bug the "40 RPM" guard below already kills, just with the unit letter
+// glued to the number instead of inside an acronym.
+// - daily token caps: 'free · 500K tokens/day', 'free · 1M tokens/24h',
+// 'free · 7M/day shared', 'free · 20k tok/day'. The column is a MONTHLY
+// budget, so a daily cap scales by the days it renews in a month. Read as
+// monthly, a 500K/day pool looked exhausted on day one and sat at the
+// headroom floor for the other 29 days although it refilled every night.
export function parseBudget(s: string): number {
if (!s) return 0;
- // Require a magnitude unit (M/K). A bare number with no unit is a rate limit
- // or placeholder, not a monthly token budget — "free · 40 RPM",
+ const label = String(s).toLowerCase();
+ // Drop parenthetical rate hints first ('~2M (60-100/hr)', '~3M (1k
+ // credits)'): they describe a side limit, never the budget the head number
+ // states, and their unit letters are what a naive scan misreads.
+ const head = label.replace(/\([^)]*\)/g, '').trim();
+ // Require a magnitude unit (M/K, either case). A bare number with no unit is
+ // a rate limit or placeholder, not a monthly token budget — "free · 40 RPM",
// "free · 200/hr per IP", "promo (trial)", "~? (anon)" — so treat those as
// "no budget info" (0), per this function's contract. Without the required
- // unit the old regex parsed "free · 40 RPM" as 40 tokens, which showed a bogus
- // budget and made the headroom guardrail penalize the model after one request.
- const m = s.match(/~?([\d.]+)(?:-([\d.]+))?([MK])/);
+ // unit the old regex parsed "free · 40 RPM" as 40 tokens, which showed a
+ // bogus budget and made the headroom guardrail penalize the model after one
+ // request. The unit must end the token ('5min', '1mo' are not 5M/1M): the
+ // lowercased scan would otherwise read a time unit as a magnitude.
+ const m = head.match(/~?([\d.]+)(?:-([\d.]+))?([mk])(?![a-z])/);
if (!m) return 0;
const high = parseFloat(m[2] ?? m[1]);
if (Number.isNaN(high)) return 0;
- const unit = m[3] === 'M' ? 1_000_000 : 1_000;
- return high * unit;
+ // The magnitude must measure tokens. When the unit is immediately followed
+ // by a request-shaped word, the number counts requests/calls/messages, not
+ // tokens, and has no place in a token budget.
+ const tail = head.slice((m.index ?? 0) + m[0].length);
+ if (/^\s*(?:rpd|rpm|rph|rps|req|requests?|calls?|messages?|prompts?|neurons?)\b/.test(tail)) return 0;
+ const tokens = high * (m[3] === 'm' ? 1_000_000 : 1_000);
+ // A per-day cap is a daily allowance; the column asks for the monthly one,
+ // and the pool renews every day (×30). Only a day/24h period qualifies, and
+ // only when it qualifies THIS number ('~120M/mo, 1M/day max' is monthly).
+ const period = tail.split(/[,·;]/)[0];
+ const perDay = /\b(?:per\s*)?days?\b|\bdaily\b|\b24\s*h(?:rs?|ours?)?\b/.test(period);
+ return perDay ? tokens * 30 : tokens;
}
// Sort score for the "budget" preset on the fallback/profiles dashboards.