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
import { describe, it, expect, beforeAll, beforeEach, afterEach, vi } from 'vitest';
import { initDb, getDb } from '../../db/index.js';
import {
recordQuotaObservation,
getQuotaStateForKeys,
parseQuotaObservationsFromResponse,
inferQuotaPoolKey,
} from '../../services/provider-quota.js';
import { pruneQuotaObservations } from '../../services/request-retention.js';
function insertState(row: {
platform: string;
keyId: number;
pool: string;
metric: string;
limit: number | null;
remaining: number | null;
resetAt: string | null;
}) {
getDb().prepare(`
INSERT INTO provider_quota_state
(platform, key_id, quota_pool_key, metric, limit_value, remaining_value, reset_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
`).run(row.platform, row.keyId, row.pool, row.metric, row.limit, row.remaining, row.resetAt);
}
function readState(platform: string, keyId: number, pool: string, metric: string) {
return getDb().prepare(`
SELECT limit_value AS lim, remaining_value AS remaining, reset_at AS resetAt
FROM provider_quota_state
WHERE platform = ? AND key_id = ? AND quota_pool_key = ? AND metric = ?
`).get(platform, keyId, pool, metric) as { lim: number | null; remaining: number | null; resetAt: string | null } | undefined;
}
describe('provider-quota: pool inference', () => {
beforeAll(() => {
process.env.ENCRYPTION_KEY = '0'.repeat(64);
initDb(':memory:');
});
it('buckets shared-pool providers per account and openrouter free vs account', () => {
expect(inferQuotaPoolKey('groq')).toBe('groq::account');
expect(inferQuotaPoolKey('electronhub', 'qwen3.8-flash')).toBe('electronhub::weekly-credit');
expect(inferQuotaPoolKey('electronhub', 'gpt-oss-120b')).toBe('electronhub::weekly-credit');
expect(inferQuotaPoolKey('electronhub', 'some-model:free')).toBe('electronhub::daily-free');
expect(inferQuotaPoolKey('experiential', 'glm-5.3')).toBe('experiential::monthly-credit');
expect(inferQuotaPoolKey('experiential', 'gpt-5.6-sol')).toBe('experiential::monthly-credit');
expect(inferQuotaPoolKey('router9', 'minimax/minimax-m3')).toBe('router9::monthly-credit');
expect(inferQuotaPoolKey('router9', 'another-model')).toBe('router9::monthly-credit');
expect(inferQuotaPoolKey('septor', 'qwen3-coder-free')).toBe('septor::daily-free');
expect(inferQuotaPoolKey('septor', 'minimax-m2.5-free')).toBe('septor::daily-free');
for (const model of ['first-model', 'another-model']) {
expect(inferQuotaPoolKey('aclide', model)).toBe('aclide::monthly-credit');
expect(inferQuotaPoolKey('speka', model)).toBe('speka::monthly-credit');
expect(inferQuotaPoolKey('plugsky', model)).toBe('plugsky::fair-use');
expect(inferQuotaPoolKey('clod', model)).toBe('clod::daily-free');
expect(inferQuotaPoolKey('blaze', model)).toBe('blaze::daily-free');
expect(inferQuotaPoolKey('speechify', model)).toBe('speechify::monthly-characters');
expect(inferQuotaPoolKey('lucidity', model)).toBe('lucidity::daily-free');
expect(inferQuotaPoolKey('airforce', model)).toBe('airforce::daily-free');
expect(inferQuotaPoolKey('dreamprompting', model)).toBe('dreamprompting::daily-free');
expect(inferQuotaPoolKey('waterfall', model)).toBe('waterfall::community-free');
expect(inferQuotaPoolKey('logfare', model)).toBe('logfare::fair-use');
}
expect(inferQuotaPoolKey('openrouter', 'meta-llama/llama-3.1-8b-instruct:free')).toBe('openrouter::free');
expect(inferQuotaPoolKey('openrouter', 'openai/gpt-4o')).toBe('openrouter::account');
// AnyAPI's 100K tokens/day is one account-wide budget, so every model on
// the platform shares a single pool.
expect(inferQuotaPoolKey('anyapi')).toBe('anyapi::free');
expect(inferQuotaPoolKey('anyapi', 'qwen/qwen3-coder:free')).toBe('anyapi::free');
expect(inferQuotaPoolKey('radeon', 'DeepSeek-V4-Flash')).toBe('radeon::daily-free');
expect(inferQuotaPoolKey('radeon', 'Qwen3.8-Flash-Next')).toBe('radeon::daily-free');
// Unknown platform falls back to platform::model or platform::account.
expect(inferQuotaPoolKey('acme' as any, 'x')).toBe('acme::x');
expect(inferQuotaPoolKey('acme' as any)).toBe('acme::account');
});
});
describe('provider-quota: record + read round-trip', () => {
beforeAll(() => {
process.env.ENCRYPTION_KEY = '0'.repeat(64);
initDb(':memory:');
});
beforeEach(() => {
getDb().prepare('DELETE FROM provider_quota_state').run();
getDb().prepare('DELETE FROM provider_quota_observations').run();
});
it('surfaces the newest observation per pool when the log holds many', () => {
// Older rows for the same pool must never win, and rows for a sibling pool
// must never bleed across. Mirrors the dashboard poll on a long-lived
// install whose log holds hundreds of thousands of rows per pool.
for (let i = 0; i < 25; i++) {
recordQuotaObservation({
platform: 'groq', keyId: 7, quotaPoolKey: 'groq::account', metric: 'tokens',
limit: 1000, remaining: 1000 - i, modelId: `old-${i}`, source: 'header',
observedAt: new Date(Date.UTC(2026, 0, 1, 0, i)).toISOString(),
});
}
recordQuotaObservation({
platform: 'groq', keyId: 7, quotaPoolKey: 'groq::account', metric: 'tokens',
limit: 1000, remaining: 5, modelId: 'newest', source: 'header',
observedAt: new Date(Date.UTC(2026, 0, 2)).toISOString(),
});
recordQuotaObservation({
platform: 'groq', keyId: 7, quotaPoolKey: 'groq::account', metric: 'requests',
limit: 30, remaining: 1, modelId: 'other-metric', source: 'header',
observedAt: new Date(Date.UTC(2026, 0, 3)).toISOString(),
});
const rows = getQuotaStateForKeys().filter(r => r.platform === 'groq' && r.keyId === 7);
expect(rows).toHaveLength(2);
expect(rows.find(r => r.metric === 'tokens')?.modelId).toBe('newest');
expect(rows.find(r => r.metric === 'tokens')?.remaining).toBe(5);
expect(rows.find(r => r.metric === 'requests')?.modelId).toBe('other-metric');
});
it('prunes the observation log by age and count without touching state', () => {
const db = getDb();
const insert = db.prepare(`
INSERT INTO provider_quota_observations
(id, platform, key_id, quota_pool_key, metric, observed_at, created_at)
VALUES (?, 'groq', 7, 'groq::account', 'tokens', ?, ?)
`);
const now = Date.UTC(2026, 8, 1);
const stamp = (ms: number) => new Date(ms).toISOString().slice(0, 19).replace('T', ' ');
for (let i = 0; i < 10; i++) {
// 5 rows older than 30 days, 5 fresh ones.
const at = stamp(now - (i < 5 ? 40 : 1) * 86_400_000 - i * 1000);
insert.run(`obs-${i}`, at, at);
}
insertState({ platform: 'groq', keyId: 7, pool: 'groq::account', metric: 'tokens', limit: 1000, remaining: 10, resetAt: null });
expect(pruneQuotaObservations(db, now)).toEqual({ deleted: 5, done: true });
expect(db.prepare('SELECT COUNT(*) AS n FROM provider_quota_observations').get()).toEqual({ n: 5 });
process.env.QUOTA_OBSERVATIONS_MAX_ROWS = '2';
try {
expect(pruneQuotaObservations(db, now)).toEqual({ deleted: 3, done: true });
} finally {
delete process.env.QUOTA_OBSERVATIONS_MAX_ROWS;
}
const left = db.prepare('SELECT id FROM provider_quota_observations ORDER BY created_at DESC').all() as { id: string }[];
expect(left.map(r => r.id)).toEqual(['obs-5', 'obs-6']);
expect(readState('groq', 7, 'groq::account', 'tokens')?.remaining).toBe(10);
});
it('stops a large sweep at its time budget and reports it unfinished', () => {
const db = getDb();
const insert = db.prepare(`
INSERT INTO provider_quota_observations
(id, platform, key_id, quota_pool_key, metric, observed_at, created_at)
VALUES (?, 'groq', 7, 'groq::account', 'tokens', ?, ?)
`);
const now = Date.UTC(2026, 8, 1);
const old = new Date(now - 60 * 86_400_000).toISOString().slice(0, 19).replace('T', ' ');
const tx = db.transaction(() => { for (let i = 0; i < 45_000; i++) insert.run(`o-${i}`, old, old); });
tx();
// A zero budget allows exactly one chunk before the check trips.
const first = pruneQuotaObservations(db, now, 0);
expect(first.done).toBe(false);
expect(first.deleted).toBe(5_000);
const rest = pruneQuotaObservations(db, now, 60_000);
expect(rest).toEqual({ deleted: 40_000, done: true });
});
it('records an observation and surfaces it via getQuotaStateForKeys', () => {
const rec = recordQuotaObservation({
platform: 'groq',
keyId: 7,
quotaPoolKey: 'groq::account',
metric: 'requests',
limit: 1000,
remaining: 950,
source: 'header',
});
expect(rec).not.toBeNull();
const states = getQuotaStateForKeys();
const row = states.find(s => s.platform === 'groq' && s.keyId === 7 && s.metric === 'requests');
expect(row).toBeDefined();
expect(row!.limit).toBe(1000);
expect(row!.remaining).toBe(950);
});
// #705: the panel rendered a bare "key #7", which names nothing an operator
// recognises once a provider holds several keys.
it('carries the label of the key the state belongs to', () => {
const db = getDb();
db.prepare(`
INSERT INTO api_keys (id, platform, label, encrypted_key, iv, auth_tag, status, enabled)
VALUES (41, 'groq', 'Work account', 'x', 'y', 'z', 'unknown', 1)
`).run();
recordQuotaObservation({
platform: 'groq', keyId: 41, quotaPoolKey: 'groq::account',
metric: 'requests', limit: 10, remaining: 1, source: 'header',
});
const row = getQuotaStateForKeys().find(s => s.keyId === 41);
expect(row!.keyLabel).toBe('Work account');
});
it('leaves the label null when the key row is gone', () => {
recordQuotaObservation({
platform: 'groq', keyId: 4242, quotaPoolKey: 'groq::account',
metric: 'requests', limit: 10, remaining: 1, source: 'header',
});
const row = getQuotaStateForKeys().find(s => s.keyId === 4242);
expect(row).toBeDefined();
expect(row!.keyLabel).toBeNull();
});
});
describe('provider-quota: parse from response headers (shared parseRetryAfterMs)', () => {
it('records Blaze token headers without fabricating a reset or per-model grant', () => {
const obs = parseQuotaObservationsFromResponse(new Response(null, { headers: {
'x-ratelimit-limit-tokens': '200000', 'x-ratelimit-remaining-tokens': '199499',
} }), { platform: 'blaze', modelId: 'test-model' });
expect(obs).toHaveLength(1);
expect(obs[0]).toMatchObject({ metric: 'tokens', limit: 200000, remaining: 199499, resetAt: null, quotaPoolKey: 'blaze::daily-free' });
});
it('records only CLōD\'s observed request window, not an invented daily token quota', () => {
const obs = parseQuotaObservationsFromResponse(new Response(null, { headers: {
'x-ratelimit-limit': '5', 'x-ratelimit-remaining': '4', 'x-ratelimit-reset': '1789230900',
} }), { platform: 'clod', modelId: 'test-model' });
expect(obs).toHaveLength(1);
expect(obs[0]).toMatchObject({ metric: 'requests', limit: 5, remaining: 4, quotaPoolKey: 'clod::daily-free' });
const speech = parseQuotaObservationsFromResponse(new Response(null), { platform: 'speechify' });
expect(speech.every(o => o.limit == null && o.remaining == null)).toBe(true);
});
beforeAll(() => {
process.env.ENCRYPTION_KEY = '0'.repeat(64);
initDb(':memory:');
});
it('parses Groq ratelimit headers into a requests observation', () => {
const resp = new Response(null, {
status: 200,
headers: {
'x-ratelimit-limit-requests': '100',
'x-ratelimit-remaining-requests': '90',
'x-ratelimit-reset-requests': '60',
},
});
const obs = parseQuotaObservationsFromResponse(resp, { platform: 'groq', keyId: 1 });
const requests = obs.find(o => o.metric === 'requests');
expect(requests).toBeDefined();
expect(requests!.limit).toBe(100);
expect(requests!.remaining).toBe(90);
});
it('reads Retry-After on a 429 via the shared parser (dedup of base.ts)', () => {
const resp = new Response(null, { status: 429, headers: { 'retry-after': '30' } });
const obs = parseQuotaObservationsFromResponse(resp, { platform: 'groq', keyId: 1 });
// The shared parseRetryAfterMs turns "30" seconds into 30000 ms.
expect(obs.some(o => o.retryAfterMs === 30_000)).toBe(true);
// A 429 always marks the pool as remaining 0.
expect(obs.some(o => o.remaining === 0)).toBe(true);
});
it('parses Radeon Cloud RPM and recurring daily allowance headers', () => {
const resp = new Response(null, {
status: 200,
headers: {
'x-ratelimit-limit-user-rpm': '30',
'x-ratelimit-remaining-user-rpm': '29',
'x-ratelimit-reset': '60',
'x-ratelimit-limit-user-daily-usd': '10',
'x-ratelimit-used-user-daily-usd': '2.5',
'x-ratelimit-remaining-user-daily-usd': '7.5',
'x-ratelimit-reset-user-daily-usd': '86400',
},
});
const obs = parseQuotaObservationsFromResponse(resp, { platform: 'radeon', keyId: 9 });
expect(obs.find(o => o.metric === 'requests')).toMatchObject({
quotaPoolKey: 'radeon::daily-free', limit: 30, remaining: 29,
});
expect(obs.find(o => o.metric === 'credits')).toMatchObject({
quotaPoolKey: 'radeon::daily-free', limit: 10, remaining: 7.5,
});
});
it('uses ElectronHub account headers without inventing per-model credit limits', () => {
const response = new Response(null, { headers: {
'x-ratelimit-limit': '5', 'x-ratelimit-remaining': '4', 'x-ratelimit-reset': '1788690000',
} });
const observations = parseQuotaObservationsFromResponse(response, {
platform: 'electronhub', keyId: 9, modelId: 'qwen3.8-flash',
});
expect(observations.find(o => o.metric === 'requests')).toMatchObject({
quotaPoolKey: 'electronhub::weekly-credit', limit: 5, remaining: 4,
resetAt: new Date(1788690000000).toISOString(),
});
expect(observations.some(o => o.metric === 'credits')).toBe(false);
});
it('keeps Router9 decimal credit observations separate from tokens and undocumented request windows', () => {
const obs = parseQuotaObservationsFromResponse(new Response(null, { headers: {
'x-credits-limit': '50000', 'x-credits-remaining': '49998.4484',
'x-ratelimit-limit-4h': '1000', 'x-ratelimit-limit-weekly': '100',
} }), { platform: 'router9', modelId: 'minimax/minimax-m3' });
expect(obs).toHaveLength(1);
expect(obs[0]).toMatchObject({ metric: 'credits', quotaPoolKey: 'router9::monthly-credit', limit: 50000, remaining: 49998.4484, resetAt: null });
});
it('observes Septor reported limits without treating signup credits as a monthly grant', () => {
const obs = parseQuotaObservationsFromResponse(new Response(null, { headers: {
'x-ratelimit-limit': '60', 'x-ratelimit-remaining': '53', 'x-ratelimit-reset': '1789074787',
} }), { platform: 'septor', modelId: 'qwen3-coder-free' });
expect(obs[0]).toMatchObject({ metric: 'requests', quotaPoolKey: 'septor::daily-free', limit: 60, remaining: 53 });
expect(obs.some(o => o.metric === 'credits')).toBe(false);
});
});
describe('provider-quota: reset headroom without overwriting observations', () => {
beforeAll(() => {
process.env.ENCRYPTION_KEY = '0'.repeat(64);
initDb(':memory:');
});
beforeEach(() => {
getDb().prepare('DELETE FROM provider_quota_state').run();
getDb().prepare('DELETE FROM provider_quota_observations').run();
});
const past = () => new Date(Date.now() - 60 * 60 * 1000).toISOString();
const future = () => new Date(Date.now() + 60 * 60 * 1000).toISOString();
it('displays replenished headroom without rewriting the reported balance', () => {
insertState({ platform: 'groq', keyId: 1, pool: 'groq::account', metric: 'requests', limit: 100, remaining: 0, resetAt: past() });
const states = getQuotaStateForKeys();
const row = states.find(s => s.platform === 'groq' && s.keyId === 1);
expect(row!.remaining).toBe(100); // replenished to the limit
expect(row!.resetAt).toBeNull(); // stale reset dropped
// Forecast readers retain the actual reported zero and its original reset.
const persisted = readState('groq', 1, 'groq::account', 'requests');
expect(persisted!.remaining).toBe(0);
expect(persisted!.resetAt).not.toBeNull();
});
it('clears remaining to unknown when the limit is unknown and reset_at passed', () => {
insertState({ platform: 'ollama', keyId: 2, pool: 'ollama::cloud', metric: 'requests', limit: null, remaining: 0, resetAt: past() });
const states = getQuotaStateForKeys();
const row = states.find(s => s.platform === 'ollama' && s.keyId === 2);
expect(row!.remaining).toBeNull(); // no known limit → clear the 0
expect(row!.resetAt).toBeNull();
const persisted = readState('ollama', 2, 'ollama::cloud', 'requests');
expect(persisted!.remaining).toBe(0);
});
it('leaves a still-active window (reset_at in the future) untouched', () => {
insertState({ platform: 'groq', keyId: 3, pool: 'groq::account', metric: 'requests', limit: 100, remaining: 0, resetAt: future() });
const states = getQuotaStateForKeys();
const row = states.find(s => s.platform === 'groq' && s.keyId === 3);
expect(row!.remaining).toBe(0); // still exhausted until it resets
expect(row!.resetAt).not.toBeNull();
});
});
describe('provider quota snapshot reliability', () => {
const now = Date.parse('2026-09-15T12:00:00Z');
beforeEach(() => { initDb(':memory:'); vi.spyOn(Date, 'now').mockReturnValue(now); });
afterEach(() => vi.restoreAllMocks());
it.each([
['2m59.56s', 179560], ['7.66s', 7660], ['1h2m3s500ms', 3723500],
['1d', 86400000], ['0s', 0], ['179.56', 179560],
[String(now / 1000 + 60), 60000], [String(now + 60000), 60000],
['2026-09-15T12:01:00Z', 60000], ['2026-09-15T14:01:00+02:00', 60000],
])('parses reset %s without dropping provider data', (reset, delay) => {
const response = new Response(null, {headers:{'x-ratelimit-limit-requests':'100','x-ratelimit-remaining-requests':'75','x-ratelimit-reset-requests':reset}});
expect(parseQuotaObservationsFromResponse(response,{platform:'groq'})[0].resetAt).toBe(new Date(now + delay).toISOString());
});
it.each(['', 'nonsense', '-5', 'Infinity', '1e99', '9999999999999999999', '2m invalid', 'NaNs'])('rejects invalid reset %j without throwing', reset => {
const response = new Response(null, {headers:{'x-ratelimit-limit-requests':'100','x-ratelimit-reset-requests':reset}});
expect(parseQuotaObservationsFromResponse(response,{platform:'groq'})[0].resetAt).toBeNull();
});
it('collects explicit compatible headers without a provider-specific mapping or fabricated limits', () => {
const response = new Response(null,{headers:{'x-ratelimit-limit-requests':'20','x-ratelimit-remaining-requests':'18','x-ratelimit-reset-requests':'1m'}});
expect(parseQuotaObservationsFromResponse(response,{platform:'kilo',keyId:4})[0]).toMatchObject({limit:20,remaining:18,resetAt:'2026-09-15T12:01:00.000Z'});
expect(parseQuotaObservationsFromResponse(new Response(null),{platform:'kilo'})[0]).toMatchObject({limit:null,remaining:null,resetAt:null});
});
it('does not replace valid quota headers with a generic 429 or mistake a 200 retry header for exhaustion', () => {
const headers={'x-ratelimit-limit-requests':'100','x-ratelimit-remaining-requests':'75','x-ratelimit-reset-requests':'2m','retry-after':'5'};
const rows=parseQuotaObservationsFromResponse(new Response(null,{status:429,headers}),{platform:'groq'});
expect(rows).toHaveLength(1);
expect(rows[0]).toMatchObject({remaining:75,source:'header',confidence:1,resetAt:'2026-09-15T12:02:00.000Z',retryAfterMs:5000});
expect(parseQuotaObservationsFromResponse(new Response(null,{headers:{'retry-after':'5'}}),{platform:'groq'}).some(row=>row.remaining===0)).toBe(false);
});
it('empty and limit-only probes cannot refresh a balance timestamp or borrow its confidence', () => {
const base={platform:'groq' as const,keyId:4};
recordQuotaObservation({...base,limit:100,remaining:50,resetAt:'2026-09-15T13:00:00Z',source:'header',observedAt:'2026-09-15T11:00:00Z'});
const before=getQuotaStateForKeys({normalizeExpired:false})[0];
const probe=recordQuotaObservation({...base,source:'probe',confidence:0.1,observedAt:'2026-09-15T12:00:00Z'});
recordQuotaObservation({...base,source:'header',limit:200,observedAt:'2026-09-15T12:01:00Z'});
expect(probe?.confidence).toBe(0.1);
expect(getQuotaStateForKeys({normalizeExpired:false})[0]).toMatchObject({limit:100,remaining:50,resetAt:before.resetAt,observedAt:before.observedAt,source:'header',confidence:1});
expect(getDb().prepare("SELECT confidence FROM provider_quota_observations WHERE source='probe'").get()).toEqual({confidence:0.1});
});
it('new balances replace the complete snapshot, including lower confidence and missing reset or limit', () => {
const base={platform:'groq' as const,keyId:4};
recordQuotaObservation({...base,limit:100,remaining:50,resetAt:'2026-09-15T13:00:00Z',source:'header',observedAt:'2026-09-15T11:00:00Z'});
recordQuotaObservation({...base,remaining:0,source:'error_body',confidence:0.55,observedAt:'2026-09-15T12:00:00Z'});
expect(getQuotaStateForKeys({normalizeExpired:false})[0]).toMatchObject({remaining:0,limit:null,resetAt:null,confidence:0.55,source:'error_body'});
recordQuotaObservation({...base,remaining:100,limit:100,source:'header',observedAt:'2026-09-15T10:00:00Z'});
expect(getQuotaStateForKeys({normalizeExpired:false})[0].remaining).toBe(0);
});
});