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
Codex/fix model discovery pricing main review (#1528)
Omansh Bhatnagar10d8b0066d44
diff --git a/server/src/__tests__/services/model-discovery.test.ts b/server/src/__tests__/services/model-discovery.test.tsindex 97da8e40..cfdffe02 100644--- a/server/src/__tests__/services/model-discovery.test.ts+++ b/server/src/__tests__/services/model-discovery.test.ts@@ -113,6 +113,78 @@ describe('parseModelCatalog tolerance (#488)', () => {});});+ it.each([0, '0.000', '0e-400', '0e400'])('preserves explicit zero pricing (%s)', zero => {+ expect(parseModelCatalog({+ data: [{ id: 'zero', pricing: { prompt: zero, completion: zero, request: zero, image: zero } }],+ })).toEqual([{ id: 'zero', ownedBy: null, priceNote: 'free', isFree: true }]);+ });++ it.each([+ ['request', '0.000001', '$0.000001/request'],+ ['image', '0.005', '$0.005/image'],+ ])('keeps a positive %s charge in its advertised unit', (component, price, priceNote) => {+ expect(parseModelCatalog({+ data: [{ id: 'charged', pricing: { prompt: '0', completion: '0', [component]: price } }],+ })).toEqual([{ id: 'charged', ownedBy: null, priceNote, isFree: false }]);+ });++ it('preserves a positive request-only price without token pricing', () => {+ expect(parseModelCatalog({+ data: [{ id: 'per-request', pricing: { request: 0.01 } }],+ })).toEqual([{ id: 'per-request', ownedBy: null, priceNote: '$0.01/request', isFree: false }]);+ });++ it.each(['request', 'image'])('does not let a literal free alias hide an advertised %s charge', component => {+ for (const [freeKey, pricingKey] of [['price', 'pricing'], ['pricing', 'price']]) {+ expect(parseModelCatalog({+ data: [{ id: 'charged', [freeKey]: 'Free', [pricingKey]: { prompt: '0', completion: '0', [component]: '0.01' } }],+ })).toEqual([{ id: 'charged', ownedBy: null, priceNote: `$0.01/${component}`, isFree: false }]);+ }+ });++ it.each([+ ['incomplete token prices', { prompt: '0' }],+ ['invalid completion', { prompt: '0', completion: 'unknown' }],+ ['boolean pricing', false],+ ['numeric pricing', 0],+ ['null pricing', null],+ ['array pricing', []],+ ['blank pricing', ' '],+ ['empty pricing', {}],+ ])('leaves a free hint with conflicting %s unknown', (_name, pricing) => {+ for (const [freeKey, pricingKey] of [['price', 'pricing'], ['pricing', 'price']]) {+ expect(parseModelCatalog({+ data: [{ id: 'unknown', [freeKey]: 'Free', [pricingKey]: pricing }],+ })).toEqual([{ id: 'unknown', ownedBy: null }]);+ }+ });++ it.each([+ ['request underflow', { prompt: '0', completion: '0', request: '1e-400' }],+ ['negative completion underflow', { prompt: '0', completion: '-1e-400' }],+ ])('leaves %s unknown instead of accepting a rounded zero', (_name, pricing) => {+ expect(parseModelCatalog({ data: [{ id: 'unknown', pricing }] })).toEqual([+ { id: 'unknown', ownedBy: null },+ ]);+ });++ it.each([+ ['missing completion', { prompt: '0' }],+ ['invalid completion', { prompt: '0', completion: 'unknown' }],+ ['null completion', { prompt: '0', completion: null }],+ ['blank completion', { prompt: '0', completion: ' ' }],+ ['negative completion', { prompt: '0', completion: -1 }],+ ['nonfinite completion', { prompt: '0', completion: Infinity }],+ ['boolean completion', { prompt: '0', completion: false }],+ ['missing prompt', { completion: '0' }],+ ['invalid extra charge', { prompt: '0', completion: '0', request: 'unknown' }],+ ['unsupported extra charge', { prompt: '0', completion: '0', web_search: '0.01' }],+ ])('leaves %s pricing unknown instead of claiming free', (_name, pricing) => {+ expect(parseModelCatalog({ data: [{ id: 'unknown', pricing }] })).toEqual([+ { id: 'unknown', ownedBy: null },+ ]);+ });+it('accepts a relay that ships ctx_len and a plain "free" price string (#685)', () => {// A generic relay, not Ollama: real /api/tags ships neither field.const [m] = parseModelCatalog({diff --git a/server/src/services/model-discovery.ts b/server/src/services/model-discovery.tsindex 04f2ca17..3dcd3b09 100644--- a/server/src/services/model-discovery.ts+++ b/server/src/services/model-discovery.ts@@ -39,7 +39,8 @@ export interface DiscoveredModel {* (OpenRouter's context_length, Ollama's ctx_len, max_model_len, ...). */contextWindow?: number;/** Human-readable price hint ("free", "$1.25/M in $2/M out") when the- * upstream ships one — normalized to USD per MILLION tokens (#685). */+ * upstream ships one. Token prices are normalized to USD per MILLION+ * tokens (#685); request/image charges retain their per-unit prices. */priceNote?: string;/** True when every price component the upstream advertises is zero, or it* plainly says "free". Set only alongside priceNote, and the only thing the@@ -136,15 +137,26 @@ function firstNumber(record: Record<string, unknown>, keys: readonly string[]):return undefined;}+/** A finite nonnegative price, in the unit the upstream advertised. */+function priceValue(raw: unknown): number | null {+ if (typeof raw !== 'number' && typeof raw !== 'string') return null;+ if (typeof raw === 'string' && raw.trim().length === 0) return null;+ const value = Number(raw);+ if (!Number.isFinite(value) || value < 0) return null;+ // Number() can round a nonzero string to zero through underflow. Only an+ // explicit decimal zero is evidence of a free price component.+ if (value === 0 && typeof raw === 'string'+ && !/^[+-]?(?:0+(?:\.0*)?|\.0+)(?:e[+-]?\d+)?$/i.test(raw.trim())) return null;+ return value;+}+/** One price component in USD per MILLION tokens, or null when the upstream* did not ship a usable figure. OpenRouter quotes strings ("0.00000125"),* others quote numbers, and a few already quote per-million — so parse* string-or-number and scale by magnitude rather than by source. */function perMillionTokens(raw: unknown): number | null {- if (typeof raw !== 'number' && typeof raw !== 'string') return null;- if (typeof raw === 'string' && raw.trim().length === 0) return null;- const value = Number(raw);- if (!Number.isFinite(value) || value < 0) return null;+ const value = priceValue(raw);+ if (value === null) return null;return value > 0 && value < PER_TOKEN_CEILING ? value * 1_000_000 : value;}@@ -164,29 +176,44 @@ function capNote(note: string): string {* plain `price: "free"` string, plus the free/not-free verdict the picker* badges on. Anything unrecognizable is left alone — no chip at all. */function priceHintOf(record: Record<string, unknown>): { note: string; isFree: boolean } | undefined {+ let hasFreePrice = false;+ let hasUnknownPrice = false;for (const key of PRICE_KEYS) {const value = record[key];if (typeof value === 'string') {const text = value.trim();- if (text.length === 0) continue;+ if (text.length === 0) { hasUnknownPrice = true; continue; }// Only the literal word counts as free — never a pattern over the note,// which is how "$10/M in" ends up painted green.- if (text.toLowerCase() === 'free') return { note: 'free', isFree: true };+ if (text.toLowerCase() === 'free') { hasFreePrice = true; continue; }return { note: capNote(text), isFree: false };}const pricing = asRecord(value);- if (!pricing) continue;+ if (!pricing) {+ if (value !== undefined) hasUnknownPrice = true;+ continue;+ }const prompt = perMillionTokens(pricing.prompt);const completion = perMillionTokens(pricing.completion);- if (prompt === null && completion === null) continue;- // OpenRouter's ":free" slugs ship a flat {"prompt":"0","completion":"0"}.- if ((prompt ?? 0) === 0 && (completion ?? 0) === 0) return { note: 'free', isFree: true };+ const request = priceValue(pricing.request);+ const image = priceValue(pricing.image);const parts: string[] = [];if (prompt) parts.push(`${formatUsd(prompt)}/M in`);if (completion) parts.push(`${formatUsd(completion)}/M out`);- return { note: capNote(parts.join(' ')), isFree: false };+ // Request and image charges are per unit, never per million tokens.+ if (request) parts.push(`$${request}/request`);+ if (image) parts.push(`$${image}/image`);+ if (parts.length > 0) return { note: capNote(parts.join(' ')), isFree: false };++ // Missing or invalid token prices are unknown, not zero. Any additional+ // advertised component must also be explicitly zero before claiming free.+ if (prompt === 0 && completion === 0 && Object.values(pricing).every(raw => priceValue(raw) === 0)) {+ hasFreePrice = true;+ } else {+ hasUnknownPrice = true;+ }}- return undefined;+ return hasFreePrice && !hasUnknownPrice ? { note: 'free', isFree: true } : undefined;}/** Image input out of a modality array (["text", "image"]) or a modality