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

feat(keys): context, price and vision hints on fetched custom models (#729)

suantea04a28ef55b45
diff --git a/client/src/components/keys/discover-models-dialog.tsx b/client/src/components/keys/discover-models-dialog.tsx
index 08e12ec7..1501b725 100644
--- a/client/src/components/keys/discover-models-dialog.tsx
+++ b/client/src/components/keys/discover-models-dialog.tsx
@@ -5,6 +5,7 @@ import { Button } from '@/components/ui/button'
import { Dialog, DialogPopup, DialogTitle } from '@/components/ui/dialog'
import { useI18n } from '@/i18n'
import { toast } from '@/lib/toast'
+import { formatContext } from '@/lib/routing'
// #488: relays add and drop models weekly, so keeping a custom endpoint's model
// list current by hand means re-running `curl .../v1/models | jq` every so
@@ -15,6 +16,15 @@ export interface DiscoveredModel {
id: string
ownedBy: string | null
registered: boolean
+ /** Approximate context window in tokens when the upstream advertises one. */
+ contextWindow?: number
+ /** Human-readable price hint ("free", "$1.25/M in $2/M out") when present (#685). */
+ priceNote?: string
+ /** The server's verdict on whether the note means free — the only thing this
+ * picker badges green, so "$10/M in" can never read as free. */
+ isFree?: boolean
+ /** True when the upstream advertises image input. */
+ vision?: boolean
}
interface DiscoverResponse {
@@ -153,6 +163,30 @@ export function DiscoverModelsDialog({
className="size-4 accent-primary"
/>
<code className="min-w-0 flex-1 truncate font-mono" title={model.id}>{model.id}</code>
+ {/* Same chips as the models table, so a discovered row reads
+ the way a registered one will. */}
+ {model.vision && (
+ <span title={t('models.visionTitle')} className="shrink-0 rounded-full bg-cyan-600/15 px-1.5 py-0.5 text-[10px] text-cyan-700 dark:bg-cyan-400/15 dark:text-cyan-400">
+ {t('models.vision')}
+ </span>
+ )}
+ {model.contextWindow !== undefined && model.contextWindow > 0 && (
+ <span title={t('models.ctxTitle')} className="shrink-0 rounded-full bg-muted px-1.5 py-0.5 text-[10px] text-muted-foreground tabular-nums">
+ {t('models.ctxBadge', { size: formatContext(model.contextWindow) })}
+ </span>
+ )}
+ {model.priceNote && (
+ <span
+ className={`shrink-0 max-w-[140px] truncate rounded-full px-1.5 py-0.5 text-[10px] ${
+ model.isFree
+ ? 'bg-emerald-600/15 text-emerald-700 dark:bg-emerald-400/15 dark:text-emerald-400'
+ : 'bg-muted text-muted-foreground'
+ }`}
+ title={model.priceNote}
+ >
+ {model.priceNote}
+ </span>
+ )}
{model.ownedBy && (
<span className="shrink-0 text-[11px] text-muted-foreground">{model.ownedBy}</span>
)}
diff --git a/server/src/__tests__/services/model-discovery.test.ts b/server/src/__tests__/services/model-discovery.test.ts
index be5b51f8..453dc540 100644
--- a/server/src/__tests__/services/model-discovery.test.ts
+++ b/server/src/__tests__/services/model-discovery.test.ts
@@ -62,6 +62,80 @@ describe('parseModelCatalog tolerance (#488)', () => {
expect(byId.get('d')).toBeNull();
});
+ // #685 details are aimed at OpenRouter first — it is what people actually
+ // point a custom base_url at — so the fixtures below are shaped like the real
+ // /api/v1/models rows: prices are STRINGS of USD per TOKEN, and the modality
+ // signal is buried one level down under `architecture`.
+ it('reads an OpenRouter row: per-token string prices and nested modalities (#685)', () => {
+ const [m] = parseModelCatalog({
+ data: [
+ {
+ id: 'anthropic/claude-3.5-sonnet',
+ name: 'Anthropic: Claude 3.5 Sonnet',
+ context_length: 200000,
+ architecture: {
+ input_modalities: ['text', 'image'],
+ output_modalities: ['text'],
+ modality: 'text+image->text',
+ },
+ pricing: { prompt: '0.00000125', completion: '0.000002', request: '0', image: '0' },
+ },
+ ],
+ });
+ expect(m).toEqual({
+ id: 'anthropic/claude-3.5-sonnet',
+ ownedBy: null,
+ contextWindow: 200000,
+ priceNote: '$1.25/M in $2/M out',
+ isFree: false,
+ vision: true,
+ });
+ });
+
+ it('calls an OpenRouter zero-priced model free rather than "$0" (#685)', () => {
+ const [m] = parseModelCatalog({
+ data: [
+ {
+ id: 'deepseek/deepseek-r1:free',
+ context_length: 163840,
+ architecture: { input_modalities: ['text'], modality: 'text->text' },
+ pricing: { prompt: '0', completion: '0', request: '0' },
+ },
+ ],
+ });
+ expect(m).toEqual({
+ id: 'deepseek/deepseek-r1:free',
+ ownedBy: null,
+ contextWindow: 163840,
+ priceNote: 'free',
+ isFree: true,
+ vision: false,
+ });
+ });
+
+ 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({
+ models: [{ name: 'qwen3-4b', ctx_len: 32768, price: 'Free' }],
+ });
+ expect(m).toEqual({ id: 'qwen3-4b', ownedBy: null, contextWindow: 32768, priceNote: 'free', isFree: true });
+ });
+
+ it('caps a chatty price string so it cannot crowd out the model id (#685)', () => {
+ const [m] = parseModelCatalog({
+ data: [{ id: 'chatty', price: 'promo: $0.15 per million in, $0.60 per million out' }],
+ });
+ expect(m.priceNote!.length).toBeLessThanOrEqual(40);
+ expect(m.priceNote!.endsWith('…')).toBe(true);
+ expect(m.isFree).toBe(false);
+ });
+
+ it('keeps a minimal envelope shape when details are absent (#685)', () => {
+ const [m] = parseModelCatalog({ data: [{ id: 'bare' }] });
+ expect(m).toEqual({ id: 'bare', ownedBy: null });
+ expect(Object.keys(m).sort()).toEqual(['id', 'ownedBy']);
+ });
+
it('drops blanks, non-strings and duplicates', () => {
expect(parseModelCatalog({
data: ['dup', { id: 'dup' }, { id: ' ' }, { id: 42 }, null, 'kept'],
diff --git a/server/src/services/model-discovery.ts b/server/src/services/model-discovery.ts
index d3bff511..ed777115 100644
--- a/server/src/services/model-discovery.ts
+++ b/server/src/services/model-discovery.ts
@@ -30,6 +30,18 @@ const MAX_MODEL_ID_LENGTH = 256;
export interface DiscoveredModel {
id: string;
ownedBy: string | null;
+ /** Approximate context window in tokens when the upstream advertises one
+ * (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). */
+ 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
+ * picker badges green — the note itself is never pattern-matched. */
+ isFree?: boolean;
+ /** True when the upstream advertises image input (modalities/vision). */
+ vision?: boolean;
}
/** Carries the HTTP status the route should answer with, so a relay's 401 stays
@@ -50,6 +62,26 @@ const LIST_KEYS = ['data', 'models', 'result', 'results', 'items'] as const;
// slug spellings assorted relays ship.
const ID_KEYS = ['id', 'name', 'model', 'model_id', 'modelId', 'slug'] as const;
const OWNER_KEYS = ['owned_by', 'ownedBy', 'organization', 'owner', 'provider', 'publisher'] as const;
+// Context-window spellings seen on OpenAI-style /models (OpenRouter), Ollama
+// /api/tags (ctx_len), and assorted relays. Only present on some envelopes;
+// absent means "unknown", which the picker renders as nothing.
+const CONTEXT_KEYS = ['context_length', 'context_window', 'max_model_len', 'max_context_length', 'max_context_tokens', 'ctx_len', 'contextWindow'] as const;
+// Price hint spellings: OpenRouter nests { prompt, completion }, others ship a
+// plain string. Absent means "unknown price".
+const PRICE_KEYS = ['price', 'pricing'] as const;
+const VISION_KEYS = ['vision', 'supports_vision', 'supportsVision', 'image_input', 'multimodal'] as const;
+// Modality spellings. OpenRouter keeps the real signal one level down, under
+// `architecture`, as an ["text","image"] array AND as a "text+image->text"
+// string; flatter relays put an array at the top level.
+const MODALITY_KEYS = ['modalities', 'input_modalities', 'inputModalities', 'modality'] as const;
+// A modality entry (or a substring of the modality string) that means images.
+const VISION_MODALITIES = ['image', 'vision', 'image-input'] as const;
+// A price hint sits in a chip next to the model id, so a chatty relay must not
+// be able to squeeze the id out of the row.
+const MAX_PRICE_NOTE_LENGTH = 40;
+// Below a cent per unit the figure can only be per-token (OpenRouter quotes
+// "0.00000125"); at or above it the relay already quoted per million.
+const PER_TOKEN_CEILING = 0.01;
function asRecord(value: unknown): Record<string, unknown> | null {
return value && typeof value === 'object' && !Array.isArray(value)
@@ -82,6 +114,97 @@ function firstString(record: Record<string, unknown>, keys: readonly string[]):
return null;
}
+function firstNumber(record: Record<string, unknown>, keys: readonly string[]): number | undefined {
+ for (const key of keys) {
+ const value = record[key];
+ if (typeof value === 'number' && Number.isFinite(value) && value > 0) return value;
+ }
+ return undefined;
+}
+
+/** 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;
+ return value > 0 && value < PER_TOKEN_CEILING ? value * 1_000_000 : value;
+}
+
+/** A tidy USD figure: at most four decimals, and no `1.2500000000000002`
+ * float noise from the per-token scaling. */
+function formatUsd(value: number): string {
+ return `$${Number(value.toFixed(4))}`;
+}
+
+function capNote(note: string): string {
+ return note.length > MAX_PRICE_NOTE_LENGTH
+ ? `${note.slice(0, MAX_PRICE_NOTE_LENGTH - 1).trimEnd()}…`
+ : note;
+}
+
+/** Price hint out of OpenRouter-style `pricing: { prompt, completion }` or a
+ * 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 {
+ for (const key of PRICE_KEYS) {
+ const value = record[key];
+ if (typeof value === 'string') {
+ const text = value.trim();
+ if (text.length === 0) 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 };
+ return { note: capNote(text), isFree: false };
+ }
+ const pricing = asRecord(value);
+ if (!pricing) 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 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 };
+ }
+ return undefined;
+}
+
+/** Image input out of a modality array (["text", "image"]) or a modality
+ * string ("text+image->text", where only the input side counts). */
+function modalityVision(record: Record<string, unknown>): boolean | undefined {
+ for (const key of MODALITY_KEYS) {
+ const value = record[key];
+ if (Array.isArray(value)) {
+ return value.some(m => (VISION_MODALITIES as readonly string[]).includes(String(m).toLowerCase()));
+ }
+ if (typeof value === 'string' && value.trim().length > 0) {
+ const input = value.toLowerCase().split('->')[0];
+ return VISION_MODALITIES.some(modality => input.includes(modality));
+ }
+ }
+ return undefined;
+}
+
+/** Vision support: a boolean `vision`/`multimodal` flag, an OpenAI-style
+ * `modalities: ["text", "image"]`, or OpenRouter's `architecture` object one
+ * level down. Absent is "unknown" (no badge). */
+function visionOf(record: Record<string, unknown>): boolean | undefined {
+ for (const key of VISION_KEYS) {
+ const value = record[key];
+ if (typeof value === 'boolean') return value;
+ }
+ const top = modalityVision(record);
+ if (top !== undefined) return top;
+ const architecture = asRecord(record['architecture']);
+ return architecture ? modalityVision(architecture) : undefined;
+}
+
function toDiscovered(entry: unknown): DiscoveredModel | null {
if (typeof entry === 'string') {
const id = entry.trim();
@@ -90,7 +213,21 @@ function toDiscovered(entry: unknown): DiscoveredModel | null {
const record = asRecord(entry);
if (!record) return null;
const id = firstString(record, ID_KEYS);
- return id ? { id, ownedBy: firstString(record, OWNER_KEYS) } : null;
+ if (!id) return null;
+
+ const model: DiscoveredModel = { id, ownedBy: firstString(record, OWNER_KEYS) };
+ // Optional detail fields are only set when the upstream actually advertises
+ // them, so a minimal `{ data: [{ id }] }` envelope keeps an identical shape.
+ const contextWindow = firstNumber(record, CONTEXT_KEYS);
+ if (contextWindow !== undefined) model.contextWindow = contextWindow;
+ const price = priceHintOf(record);
+ if (price !== undefined) {
+ model.priceNote = price.note;
+ model.isFree = price.isFree;
+ }
+ const vision = visionOf(record);
+ if (vision !== undefined) model.vision = vision;
+ return model;
}
/** Whether this payload carries a model list at all — the difference between