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(routing): budget sort parsed '40 RPM' as a 40M-token budget (#1292)

tashdroid8aa17cd74a0b
diff --git a/server/src/__tests__/lib/budget.test.ts b/server/src/__tests__/lib/budget.test.ts
index 7942f9b1..96fe543d 100644
--- a/server/src/__tests__/lib/budget.test.ts
+++ b/server/src/__tests__/lib/budget.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
-import { parseBudget } from '../../lib/budget.js';
+import { parseBudget, monthlyBudgetScore } from '../../lib/budget.js';
describe('parseBudget', () => {
it('parses token-count labels to their upper bound', () => {
@@ -27,3 +27,34 @@ describe('parseBudget', () => {
expect(parseBudget(undefined as unknown as string)).toBe(0);
});
});
+
+describe('monthlyBudgetScore', () => {
+ const score = (b: string, tpd: number | null = null) =>
+ monthlyBudgetScore({ monthly_token_budget: b, tpd_limit: tpd });
+
+ it('scores token labels by their parsed upper bound', () => {
+ expect(score('~25M')).toBe(25_000_000);
+ expect(score('~1-3M')).toBe(3_000_000);
+ expect(score('~500K')).toBe(500_000);
+ expect(score('~3M (1k credits)')).toBe(3_000_000);
+ });
+
+ it('scores rate-limit labels as 0 instead of inflating them (the "40 RPM" bug)', () => {
+ // The old per-route copy matched the bare 40 and applied the 'M' unit
+ // from "RPM", scoring 40,000,000 — above every real free-tier budget.
+ expect(score('free · 40 RPM')).toBe(0);
+ expect(score('free · 200/hr per IP')).toBe(0);
+ expect(score('credits-based')).toBe(0);
+ expect(score('free · 40 RPM')).toBeLessThan(score('~25M'));
+ });
+
+ it('prefers a concrete tpd_limit over any label', () => {
+ expect(score('~120M', 1_000_000)).toBe(30_000_000);
+ expect(score('', 100_000)).toBe(3_000_000);
+ });
+
+ it('keeps unlimited labels above every parsed budget', () => {
+ expect(score('unlimited')).toBe(Infinity);
+ expect(score('∞')).toBe(Infinity);
+ });
+});
diff --git a/server/src/__tests__/routes/fallback.test.ts b/server/src/__tests__/routes/fallback.test.ts
index f3625fbe..a8a4f047 100644
--- a/server/src/__tests__/routes/fallback.test.ts
+++ b/server/src/__tests__/routes/fallback.test.ts
@@ -387,6 +387,49 @@ describe('Fallback API', () => {
}
});
+ it('budget sort does not rank a rate-limit label above a real monthly budget', async () => {
+ // "free · 40 RPM" is a rate limit, not a 40M-token budget: the old
+ // per-route parser saw the 'M' in "RPM" and multiplied by 1e6, sorting
+ // the model to the top of the chain ahead of genuine ~25M budgets.
+ const { body: chain } = await request(app, 'GET', '/api/fallback');
+ const [rpmModel, bigModel, smallModel] = chain;
+ const db = getDb();
+ const orig = [rpmModel, bigModel, smallModel].map((m: any) =>
+ db.prepare('SELECT monthly_token_budget, tpd_limit FROM models WHERE id = ?').get(m.modelDbId) as any);
+
+ try {
+ db.prepare('UPDATE models SET tpd_limit = NULL, monthly_token_budget = ? WHERE id = ?')
+ .run('free · 40 RPM', rpmModel.modelDbId);
+ db.prepare('UPDATE models SET tpd_limit = NULL, monthly_token_budget = ? WHERE id = ?')
+ .run('~25M', bigModel.modelDbId);
+ db.prepare('UPDATE models SET tpd_limit = NULL, monthly_token_budget = ? WHERE id = ?')
+ .run('~500K', smallModel.modelDbId);
+
+ const { status } = await request(app, 'POST', '/api/fallback/sort/budget');
+ expect(status).toBe(200);
+
+ // Read priorities from the table the route writes for the active
+ // profile (the seeded DB has a default profile; fallback_config is the
+ // no-profile path). GET /api/fallback re-orders by intelligence rank,
+ // so it can't verify the priority the sort wrote.
+ const profileId = (db.prepare("SELECT value FROM settings WHERE key = 'active_profile_id'").get() as any)?.value;
+ const prio = (id: number) => Number(((profileId
+ ? db.prepare('SELECT priority FROM profile_models WHERE profile_id = ? AND model_db_id = ?').get(profileId, id)
+ : db.prepare('SELECT priority FROM fallback_config WHERE model_db_id = ?').get(id)
+ ) as any).priority);
+ // A rate-limit label is "no budget info" (0): the real ~25M budget must
+ // sort first, and even a small ~500K budget beats the inflated label.
+ // With the old parser the RPM label scored 40M and took priority 1.
+ expect(prio(bigModel.modelDbId)).toBeLessThan(prio(rpmModel.modelDbId));
+ expect(prio(smallModel.modelDbId)).toBeLessThan(prio(rpmModel.modelDbId));
+ expect(prio(bigModel.modelDbId)).toBeLessThan(prio(smallModel.modelDbId));
+ } finally {
+ const restore = db.prepare('UPDATE models SET monthly_token_budget = ?, tpd_limit = ? WHERE id = ?');
+ [rpmModel, bigModel, smallModel].forEach((m: any, i: number) =>
+ restore.run(orig[i].monthly_token_budget, orig[i].tpd_limit, m.modelDbId));
+ }
+ });
+
it('intelligence sort never places a weaker tier above a Frontier model (#135)', async () => {
await request(app, 'POST', '/api/fallback/sort/intelligence');
const { body } = await request(app, 'GET', '/api/fallback');
diff --git a/server/src/lib/budget.ts b/server/src/lib/budget.ts
index f22e645e..f047b6e4 100644
--- a/server/src/lib/budget.ts
+++ b/server/src/lib/budget.ts
@@ -17,3 +17,18 @@ export function parseBudget(s: string): number {
const unit = m[3] === 'M' ? 1_000_000 : 1_000;
return high * unit;
}
+
+// Sort score for the "budget" preset on the fallback/profiles dashboards.
+// Shares parseBudget so rate-limit labels can't masquerade as budgets: the
+// per-route copies this replaced multiplied the bare number in
+// "free · 40 RPM" by 1e6 because the 'M' in "RPM" hit the unit check, ranking
+// a 40-requests-per-minute model as a 40-million-token budget.
+// tpd_limit still wins when present (a concrete daily cap beats a "~" label),
+// and "unlimited"/"∞" labels keep sorting above every parsed budget.
+export function monthlyBudgetScore(m: { monthly_token_budget: string; tpd_limit: number | null }): number {
+ if (m.tpd_limit != null) return m.tpd_limit * 30;
+ const str = m.monthly_token_budget;
+ if (!str) return 0;
+ if (str.toLowerCase().includes('unlimited') || str.includes('∞')) return Infinity;
+ return parseBudget(str);
+}
diff --git a/server/src/routes/fallback.ts b/server/src/routes/fallback.ts
index b5777132..28e95ad3 100644
--- a/server/src/routes/fallback.ts
+++ b/server/src/routes/fallback.ts
@@ -9,7 +9,7 @@ import { z } from 'zod';
import { getDb } from '../db/index.js';
import { getAllPenalties, getRoutingScores, getRoutingStrategy, setRoutingStrategy, setCustomWeights, getExploreEnabled, setExploreEnabled, getPeakHoursConfig, setPeakHoursConfig, getActiveRoutingWeights, getKeySelectionStrategy, setKeySelectionStrategy } from '../services/router.js';
import { BANDIT_PRESETS, isValidTimezone, type RoutingStrategy } from '../services/scoring.js';
-import { parseBudget } from '../lib/budget.js';
+import { parseBudget, monthlyBudgetScore } from '../lib/budget.js';
import { getModelGroups } from '../services/model-groups.js';
import { getPenaltyInspector, clearRouterPressure } from '../services/penalty-inspector.js';
import { getCooldownCeilingMs, setCooldownCeilingMs, MIN_COOLDOWN_CEILING_MS, MAX_COOLDOWN_CEILING_MS } from '../services/ratelimit.js';
@@ -397,29 +397,6 @@ const SORT_PRESETS: Record<string, string> = {
speed: 'm.speed_rank ASC',
};
-function getBudgetScore(m: { monthly_token_budget: string; tpd_limit: number | null }): number {
- if (m.tpd_limit != null) return m.tpd_limit * 30;
-
- const str = m.monthly_token_budget;
- if (!str) return 0;
- if (str.toLowerCase().includes('unlimited') || str.includes('∞')) return Infinity;
-
- const cleanStr = str.split('(')[0];
- const matches = cleanStr.match(/[\d.]+/g);
- let maxNum = 0;
- if (matches) {
- maxNum = Math.max(...matches.map(mStr => parseFloat(mStr)));
- }
-
- let mult = 1;
- const upper = cleanStr.toUpperCase();
- if (upper.includes('B')) mult = 1_000_000_000;
- else if (upper.includes('M')) mult = 1_000_000;
- else if (upper.includes('K')) mult = 1_000;
-
- return maxNum * mult;
-}
-
fallbackRouter.post('/sort/:preset', (req: Request, res: Response) => {
const preset = String(req.params.preset);
const db = getDb();
@@ -428,7 +405,7 @@ fallbackRouter.post('/sort/:preset', (req: Request, res: Response) => {
if (preset === 'budget') {
const allModels = db.prepare(`SELECT id, monthly_token_budget, tpd_limit FROM models`).all() as any[];
- allModels.sort((a, b) => getBudgetScore(b) - getBudgetScore(a));
+ allModels.sort((a, b) => monthlyBudgetScore(b) - monthlyBudgetScore(a));
models = allModels.map(m => ({ id: m.id }));
} else {
const orderBy = SORT_PRESETS[preset];
diff --git a/server/src/routes/profiles.ts b/server/src/routes/profiles.ts
index 12cd3622..2bfb849b 100644
--- a/server/src/routes/profiles.ts
+++ b/server/src/routes/profiles.ts
@@ -8,6 +8,7 @@ import { Router } from 'express';
import type { Request, Response } from 'express';
import { z } from 'zod';
import { getDb } from '../db/index.js';
+import { monthlyBudgetScore } from '../lib/budget.js';
export const profilesRouter = Router();
@@ -385,35 +386,12 @@ const SORT_PRESETS: Record<string, string> = {
speed: 'm.speed_rank ASC',
};
-function getBudgetScore(m: { monthly_token_budget: string; tpd_limit: number | null }): number {
- if (m.tpd_limit != null) return m.tpd_limit * 30;
-
- const str = m.monthly_token_budget;
- if (!str) return 0;
- if (str.toLowerCase().includes('unlimited') || str.includes('∞')) return Infinity;
-
- const cleanStr = str.split('(')[0];
- const matches = cleanStr.match(/[\d.]+/g);
- let maxNum = 0;
- if (matches) {
- maxNum = Math.max(...matches.map(mStr => parseFloat(mStr)));
- }
-
- let mult = 1;
- const upper = cleanStr.toUpperCase();
- if (upper.includes('B')) mult = 1_000_000_000;
- else if (upper.includes('M')) mult = 1_000_000;
- else if (upper.includes('K')) mult = 1_000;
-
- return maxNum * mult;
-}
-
function sortProfileModels(db: any, profileId: number, preset: string) {
let models: { id: number }[] = [];
if (preset === 'budget') {
const allModels = db.prepare(`SELECT id, monthly_token_budget, tpd_limit FROM models`).all() as any[];
- allModels.sort((a, b) => getBudgetScore(b) - getBudgetScore(a));
+ allModels.sort((a, b) => monthlyBudgetScore(b) - monthlyBudgetScore(a));
models = allModels.map(m => ({ id: m.id }));
} else {
const orderBy = SORT_PRESETS[preset];