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(models): flat, smartest-first monthly budget legend (no provider grouping) (#1243)

Tashfeen4b38e367cac3
diff --git a/client/src/components/token-usage-bar.tsx b/client/src/components/token-usage-bar.tsx
index 0efdba4f..ac7339a4 100644
--- a/client/src/components/token-usage-bar.tsx
+++ b/client/src/components/token-usage-bar.tsx
@@ -1,76 +1,43 @@
-import { Fragment, useEffect, useRef, useState } from 'react'
-import { useQuery } from '@tanstack/react-query'
+import { useEffect, useRef, useState } from 'react'
import { ChevronDown } from 'lucide-react'
import { useI18n } from '@/i18n'
-import { apiFetch } from '@/lib/api'
+import { buildBudgetLegend } from '@/lib/budget-legend'
import { formatPercent, formatTokens, platformColors, type TokenUsageData } from '@/lib/routing'
-import {
- buildLegendGroups,
- poolScopeWords,
- type FreeTierPool,
- type FreeTierResponse,
- type LegendGroup,
-} from '@/lib/pool-legend'
-// Legend rows visible while collapsed (~6 rows: 6 × 16px line + 5 × 6px gap).
const LEGEND_COLLAPSED_PX = 126
-const METRIC_KEYS: Record<string, string> = {
- tokens: 'freeTier.metricTokens',
- requests: 'freeTier.metricRequests',
- credits: 'freeTier.metricCredits',
- neurons: 'freeTier.metricNeurons',
-}
-
-// Stacked monthly token-budget bar with a collapsible per-model legend,
-// extracted from FallbackPage.
+// The monthly token budget: a stacked bar of every configured model's remaining
+// allowance, and under it one flat legend of models, smartest first. No
+// provider headers — the provider pool grouping (#1010) made the first thing on
+// the Models page read as "arranged by provider"; pool quota readings live on
+// each model's own page instead.
export function TokenUsageBar({ data }: { data: TokenUsageData }) {
const { t } = useI18n()
const { totalBudget, totalUsed, models } = data
const remaining = Math.max(0, totalBudget - totalUsed)
const remainingPct = totalBudget > 0 ? formatPercent(remaining / totalBudget) : '0%'
-
- const modelsWithWidth = models.map(m => {
- const usedTokens = m.used ?? 0
- const remainingTokens = Math.max(0, m.budget - usedTokens)
- return {
- ...m,
- usedTokens,
- remainingTokens,
- widthPct: totalBudget > 0 ? (remainingTokens / totalBudget) * 100 : 0,
- }
- })
const usedPct = totalBudget > 0 ? Math.min(100, (totalUsed / totalBudget) * 100) : 0
- // Provider pools (#905): many models on one platform share a single free
- // allowance, so the legend groups its rows under the pool they draw from
- // rather than repeating the same numbers in a second table of its own.
- const { data: freeTier } = useQuery({
- queryKey: ['free-tier'],
- queryFn: () => apiFetch<FreeTierResponse>('/api/free-tier'),
- })
-
- // A model with no published monthly quota has nothing to say in a
- // remaining/budget legend, and one provider can contribute a hundred of them
- // (#887) — drowning the models that do have a budget. Count them instead.
- const { groups, unpublishedCount, rowCount } = buildLegendGroups(modelsWithWidth, freeTier?.pools)
- const hasPoolHeaders = groups.some(g => g.pool !== null)
+ const { rows, unpublishedCount } = buildBudgetLegend(models, totalBudget)
- // Collapse the per-model legend to a few rows; the chevron reveals the rest.
- // The toggle only appears when the legend actually overflows the collapsed
- // height (column count — and so row count — depends on viewport width).
const [expanded, setExpanded] = useState(false)
const [collapsible, setCollapsible] = useState(false)
+ // The legend's full height, measured in the effect (refs are not read during
+ // render) so the expanded max-height animates to the real size.
+ const [fullHeight, setFullHeight] = useState<number>()
const legendRef = useRef<HTMLDivElement>(null)
useEffect(() => {
const el = legendRef.current
if (!el) return
- const check = () => setCollapsible(el.scrollHeight > LEGEND_COLLAPSED_PX + 1)
+ const check = () => {
+ setFullHeight(el.scrollHeight)
+ setCollapsible(el.scrollHeight > LEGEND_COLLAPSED_PX + 1)
+ }
check()
const ro = new ResizeObserver(check)
ro.observe(el)
return () => ro.disconnect()
- }, [rowCount, groups.length, unpublishedCount])
+ }, [rows.length, unpublishedCount])
return (
<section className="rounded-3xl border bg-card p-5">
@@ -94,9 +61,9 @@ export function TokenUsageBar({ data }: { data: TokenUsageData }) {
</div>
<div className="flex h-2.5 rounded-full overflow-hidden bg-muted">
- {modelsWithWidth.map((m, i) => (
+ {rows.map(m => (
<div
- key={i}
+ key={`${m.platform}:${m.modelId ?? m.displayName}`}
title={`${m.displayName} (${m.platform}): ${formatTokens(m.remainingTokens)} ${t('models.remaining')}, ${formatTokens(m.usedTokens)} ${t('models.used')}`}
style={{
width: `${m.widthPct}%`,
@@ -116,44 +83,26 @@ export function TokenUsageBar({ data }: { data: TokenUsageData }) {
<div
ref={legendRef}
className="mt-4 overflow-hidden transition-[max-height] duration-300 ease-in-out"
- style={collapsible ? { maxHeight: expanded ? legendRef.current?.scrollHeight : LEGEND_COLLAPSED_PX } : undefined}
+ style={collapsible ? { maxHeight: expanded ? fullHeight : LEGEND_COLLAPSED_PX } : undefined}
>
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-x-5 gap-y-1.5 text-xs tabular-nums">
- {groups.map(group => (
- <Fragment key={group.pool?.poolKey ?? 'independent'}>
- {group.pool ? (
- <PoolHeader pool={group.pool} />
- ) : (
- // Only worth naming once something else in the list IS a pool;
- // on its own it is just the flat legend.
- hasPoolHeaders && (
- <div className="col-span-full text-muted-foreground pt-1.5 first:pt-0">
- {t('freeTier.independentBudgets')}
- </div>
- )
- )}
- {group.models.map((m, i) => (
- <div
- key={`${m.platform}:${m.modelId ?? i}`}
- className={`flex items-center gap-2 min-w-0 ${group.pool || hasPoolHeaders ? 'pl-4' : ''}`}
- >
- <span
- className="size-2 rounded-sm flex-shrink-0"
- style={{ backgroundColor: platformColors[m.platform] ?? '#94a3b8' }}
- />
- <span className="truncate">{m.displayName}</span>
- <span className="flex-1" />
- {/* remaining / budget: a bare remaining figure gives no sense of
- how much of the allowance is gone (#887). */}
- <span
- className="font-mono text-muted-foreground"
- title={t('models.legendRemainingTitle', { name: m.displayName, platform: m.platform })}
- >
- {formatTokens(m.remainingTokens)}<span className="mx-0.5">/</span>{formatTokens(m.budget)}
- </span>
- </div>
- ))}
- </Fragment>
+ {rows.map(m => (
+ <div key={`${m.platform}:${m.modelId ?? m.displayName}`} className="flex items-center gap-2 min-w-0">
+ <span
+ className="size-2 rounded-sm flex-shrink-0"
+ style={{ backgroundColor: platformColors[m.platform] ?? '#94a3b8' }}
+ />
+ <span className="truncate">{m.displayName}</span>
+ <span className="flex-1" />
+ {/* remaining / budget: a bare remaining figure gives no sense of
+ how much of the allowance is gone (#887). */}
+ <span
+ className="font-mono text-muted-foreground"
+ title={t('models.legendRemainingTitle', { name: m.displayName, platform: m.platform })}
+ >
+ {formatTokens(m.remainingTokens)}<span className="mx-0.5">/</span>{formatTokens(m.budget)}
+ </span>
+ </div>
))}
</div>
</div>
@@ -171,68 +120,10 @@ export function TokenUsageBar({ data }: { data: TokenUsageData }) {
onClick={() => setExpanded(e => !e)}
className="mt-2 flex w-full items-center justify-center gap-1 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
- {expanded ? t('models.showLess') : t('models.showAllModels', { count: rowCount })}
+ {expanded ? t('models.showLess') : t('models.showAllModels', { count: rows.length })}
<ChevronDown className={`size-3.5 transition-transform duration-300 ${expanded ? 'rotate-180' : ''}`} />
</button>
)}
</section>
)
}
-
-// One pool of models, headed by everything the separate pools table used to
-// show: what the pool is, how many models sit in it, the documented allowance
-// scaled by usable keys, and the live reading with its metric named.
-function PoolHeader({ pool }: { pool: NonNullable<LegendGroup['pool']> }) {
- const { t } = useI18n()
- const scope = poolScopeWords(pool.poolKey)
- const label = scope
- ? t('freeTier.poolLabel', { platform: pool.platform, scope })
- : t('freeTier.poolLabelBare', { platform: pool.platform })
-
- return (
- <div className="col-span-full flex flex-wrap items-center gap-x-2 gap-y-0.5 pt-1.5 first:pt-0">
- <span
- className="size-2 rounded-sm flex-shrink-0"
- style={{ backgroundColor: platformColors[pool.platform] ?? '#94a3b8' }}
- />
- <span className="font-medium truncate" title={pool.poolKey}>{label}</span>
- <span className="text-muted-foreground">
- {t('freeTier.poolModels', { count: pool.modelCount })}
- {pool.disabledModelCount > 0 && ` ${t('freeTier.disabledModels', { count: pool.disabledModelCount })}`}
- </span>
- <span className="flex-1" />
- <PoolQuota pool={pool} />
- {pool.documentedBudget > 0 ? (
- <span
- className="font-mono text-muted-foreground"
- title={t('freeTier.poolBudgetTitle', { count: Math.max(1, pool.keyCount) })}
- >
- {t('freeTier.poolBudget', { budget: formatTokens(pool.documentedBudget) })}
- </span>
- ) : (
- <span className="text-muted-foreground">
- {pool.kind === 'credits' ? t('freeTier.kindCredits') : t('freeTier.poolNoQuota')}
- </span>
- )}
- </div>
- )
-}
-
-// The live reading, with the metric spelled out: a request counter next to a
-// token budget reads as tokens otherwise.
-function PoolQuota({ pool }: { pool: FreeTierPool }) {
- const { t } = useI18n()
- if (!pool.quota || pool.quota.remaining == null) return null
- const metric = t(METRIC_KEYS[pool.quota.metric] ?? 'freeTier.metricOther')
- return (
- <span className="text-muted-foreground whitespace-nowrap">
- <span className="font-mono">{formatTokens(pool.quota.remaining)}</span> {metric} {t('models.remaining')}
- {pool.quota.resetAt && (
- <>
- <span className="mx-1.5">·</span>
- {t('freeTier.poolResets', { time: new Date(pool.quota.resetAt).toLocaleString() })}
- </>
- )}
- </span>
- )
-}
diff --git a/client/src/lib/budget-legend.test.ts b/client/src/lib/budget-legend.test.ts
new file mode 100644
index 00000000..91da2c65
--- /dev/null
+++ b/client/src/lib/budget-legend.test.ts
@@ -0,0 +1,76 @@
+import { describe, it, expect } from 'vitest'
+import { buildBudgetLegend, sortByIntelligence, type LegendModel } from './budget-legend'
+
+function model(platform: string, modelId: string, intelligenceRank: number | undefined, budget: number, used = 0): LegendModel {
+ return { displayName: `${platform}/${modelId}`, platform, modelId, intelligenceRank, budget, used }
+}
+
+describe('sortByIntelligence', () => {
+ it('orders smartest first regardless of the incoming (chain-priority) order', () => {
+ // Chain priority is seeded provider by provider, so the server used to hand
+ // the legend groq, groq, google, google… — the "arranged by provider" look.
+ const chainOrder = [
+ model('groq', 'llama-8b', 40, 1),
+ model('groq', 'llama-70b', 12, 1),
+ model('google', 'gemini-flash', 8, 1),
+ model('google', 'gemma', 30, 1),
+ model('cerebras', 'qwen-235b', 5, 1),
+ ]
+ expect(sortByIntelligence(chainOrder).map(m => m.modelId)).toEqual([
+ 'qwen-235b', 'gemini-flash', 'llama-70b', 'gemma', 'llama-8b',
+ ])
+ })
+
+ it('keeps equal ranks in their incoming order and puts unranked rows last', () => {
+ const rows = [
+ model('a', 'unranked-first', undefined, 1),
+ model('b', 'rank3-first', 3, 1),
+ model('c', 'rank3-second', 3, 1),
+ model('d', 'rank1', 1, 1),
+ model('e', 'unranked-second', undefined, 1),
+ ]
+ expect(sortByIntelligence(rows).map(m => m.modelId)).toEqual([
+ 'rank1', 'rank3-first', 'rank3-second', 'unranked-first', 'unranked-second',
+ ])
+ })
+
+ it('does not mutate its input', () => {
+ const rows = [model('a', 'x', 2, 1), model('b', 'y', 1, 1)]
+ const before = rows.map(m => m.modelId)
+ sortByIntelligence(rows)
+ expect(rows.map(m => m.modelId)).toEqual(before)
+ })
+})
+
+describe('buildBudgetLegend', () => {
+ it('is one flat list of budgeted models, smartest first, with no provider grouping', () => {
+ const legend = buildBudgetLegend([
+ model('groq', 'llama-70b', 12, 1_000_000, 250_000),
+ model('google', 'gemini-flash', 8, 2_000_000),
+ model('groq', 'llama-8b', 40, 500_000),
+ ], 3_500_000)
+ expect(legend.rows.map(r => r.modelId)).toEqual(['gemini-flash', 'llama-70b', 'llama-8b'])
+ // Only model rows — nothing in the shape names a pool or a provider header.
+ for (const row of legend.rows) expect(Object.keys(row)).not.toContain('pool')
+ })
+
+ it('computes used / remaining / width per row against the whole budget', () => {
+ const legend = buildBudgetLegend([model('groq', 'llama-70b', 1, 1_000_000, 250_000)], 4_000_000)
+ expect(legend.rows[0]).toMatchObject({ usedTokens: 250_000, remainingTokens: 750_000, widthPct: 18.75 })
+ })
+
+ it('folds rows without a published budget into the unpublished count', () => {
+ const legend = buildBudgetLegend([
+ model('groq', 'llama-70b', 1, 1_000_000),
+ model('pollinations', 'openai', 5, 0),
+ model('llm7', 'gpt', 9, 0),
+ ], 1_000_000)
+ expect(legend.rows.map(r => r.modelId)).toEqual(['llama-70b'])
+ expect(legend.unpublishedCount).toBe(2)
+ })
+
+ it('never divides by a zero budget', () => {
+ const legend = buildBudgetLegend([model('groq', 'x', 1, 10)], 0)
+ expect(legend.rows[0].widthPct).toBe(0)
+ })
+})
diff --git a/client/src/lib/budget-legend.ts b/client/src/lib/budget-legend.ts
new file mode 100644
index 00000000..12c7ea4b
--- /dev/null
+++ b/client/src/lib/budget-legend.ts
@@ -0,0 +1,59 @@
+import type { TokenUsageData } from './routing'
+
+// The monthly-budget legend: one flat list of models, smartest first.
+//
+// It used to nest model rows under provider pool headers (#1010), which read
+// as "arranged by provider" — the first thing on the Models page and the one
+// place it was grouped that way. The legend is back to models only, ordered by
+// intelligence so the top of the list is what the router reaches for first
+// under the smartest strategy. Pool-level quota readings live on each model's
+// own page.
+
+export type LegendModel = TokenUsageData['models'][number]
+
+export interface LegendRow extends LegendModel {
+ usedTokens: number
+ remainingTokens: number
+ /** Share of the whole budget this model's remaining allowance is. */
+ widthPct: number
+}
+
+export interface BudgetLegend {
+ /** Rows with a budget to show, smartest first. */
+ rows: LegendRow[]
+ /** Rows with no published quota, folded into the "+N" summary line. */
+ unpublishedCount: number
+}
+
+/** Smartest first (rank 1 = smartest). Rows without a rank sort last, and the
+ * incoming order is kept as the tiebreaker so equal ranks stay stable. */
+export function sortByIntelligence<T extends { intelligenceRank?: number }>(models: readonly T[]): T[] {
+ const rank = (m: T) => (typeof m.intelligenceRank === 'number' && Number.isFinite(m.intelligenceRank)
+ ? m.intelligenceRank
+ : Number.POSITIVE_INFINITY)
+ return models
+ .map((m, i) => ({ m, i }))
+ .sort((a, b) => rank(a.m) - rank(b.m) || a.i - b.i)
+ .map(({ m }) => m)
+}
+
+export function buildBudgetLegend(models: readonly LegendModel[], totalBudget: number): BudgetLegend {
+ const ordered = sortByIntelligence(models)
+ const rows: LegendRow[] = []
+ let unpublishedCount = 0
+ for (const m of ordered) {
+ const usedTokens = m.used ?? 0
+ const remainingTokens = Math.max(0, m.budget - usedTokens)
+ if (m.budget <= 0) {
+ unpublishedCount += 1
+ continue
+ }
+ rows.push({
+ ...m,
+ usedTokens,
+ remainingTokens,
+ widthPct: totalBudget > 0 ? (remainingTokens / totalBudget) * 100 : 0,
+ })
+ }
+ return { rows, unpublishedCount }
+}
diff --git a/client/src/lib/pool-legend.test.ts b/client/src/lib/pool-legend.test.ts
deleted file mode 100644
index 70e32c6f..00000000
--- a/client/src/lib/pool-legend.test.ts
+++ /dev/null
@@ -1,135 +0,0 @@
-import { describe, it, expect } from 'vitest'
-import { buildLegendGroups, isPerModelPool, poolScopeWords, type FreeTierPool, type LegendRow } from './pool-legend'
-
-function row(platform: string, modelId: string, budget: number, used = 0): LegendRow {
- return {
- displayName: `${platform}/${modelId}`,
- platform,
- modelId,
- budget,
- used,
- usedTokens: used,
- remainingTokens: Math.max(0, budget - used),
- widthPct: 0,
- }
-}
-
-function pool(over: Partial<FreeTierPool> & Pick<FreeTierPool, 'poolKey' | 'platform' | 'memberModelIds'>): FreeTierPool {
- return {
- modelCount: over.memberModelIds.length,
- disabledModelCount: 0,
- keyCount: 1,
- documentedBudget: 0,
- bestLabel: '',
- kind: 'documented',
- quota: null,
- ...over,
- }
-}
-
-describe('pool scope wording', () => {
- it('reads the scope out of a pool key', () => {
- expect(poolScopeWords('groq::account')).toBe('account')
- expect(poolScopeWords('openrouter::free')).toBe('free')
- })
-
- it('drops a scope that would only repeat the word "pool"', () => {
- // "nvidia · shared credit pool pool" helps nobody.
- expect(poolScopeWords('nvidia::credit-pool')).toBe('credit')
- expect(poolScopeWords('mistral::experiment-pool')).toBe('experiment')
- expect(poolScopeWords('cerebras::shared')).toBe('')
- })
-})
-
-describe('per-model pools', () => {
- it('recognises a pool that is really just one model', () => {
- expect(isPerModelPool(pool({ poolKey: 'custom::llama-3', platform: 'custom', memberModelIds: ['llama-3'] }))).toBe(true)
- expect(isPerModelPool(pool({ poolKey: 'groq::account', platform: 'groq', memberModelIds: ['llama-3'] }))).toBe(false)
- })
-})
-
-describe('legend grouping', () => {
- const groq = pool({
- poolKey: 'groq::account',
- platform: 'groq',
- memberModelIds: ['a', 'b'],
- documentedBudget: 15_000_000,
- disabledModelCount: 1,
- })
- const solo = pool({ poolKey: 'custom::c', platform: 'custom', memberModelIds: ['c'], documentedBudget: 1_000 })
-
- it('nests each model under the pool that names it', () => {
- const { groups, rowCount } = buildLegendGroups(
- [row('groq', 'a', 10), row('groq', 'b', 20), row('custom', 'c', 30)],
- [groq, solo],
- )
- expect(groups.map(g => g.pool?.poolKey ?? null)).toEqual(['groq::account', null])
- expect(groups[0].models.map(m => m.modelId)).toEqual(['a', 'b'])
- // A one-model pool is not worth a header of its own; it lands in the
- // trailing independent group.
- expect(groups[1].models.map(m => m.modelId)).toEqual(['c'])
- expect(rowCount).toBe(3)
- })
-
- it('folds models with no published budget into the count, as the flat legend did', () => {
- const { groups, unpublishedCount, rowCount } = buildLegendGroups(
- [row('groq', 'a', 10), row('groq', 'b', 0)],
- [groq],
- )
- expect(unpublishedCount).toBe(1)
- expect(rowCount).toBe(1)
- expect(groups[0].models.map(m => m.modelId)).toEqual(['a'])
- })
-
- it('keeps a header for a pool whose only numbers are live ones', () => {
- const live = pool({
- poolKey: 'kilo::anonymous',
- platform: 'kilo',
- memberModelIds: ['k'],
- documentedBudget: 0,
- kind: 'unpublished',
- quota: { limit: null, remaining: 40, resetAt: null, metric: 'requests', keyCount: 1 },
- })
- const { groups, unpublishedCount } = buildLegendGroups([row('kilo', 'k', 0)], [live])
- expect(groups).toHaveLength(1)
- expect(groups[0].models).toHaveLength(0)
- expect(unpublishedCount).toBe(1)
- })
-
- it('drops a pool that publishes nothing and reports nothing', () => {
- const silent = pool({
- poolKey: 'llm7::anonymous',
- platform: 'llm7',
- memberModelIds: ['x'],
- documentedBudget: 0,
- kind: 'unpublished',
- })
- const { groups, unpublishedCount } = buildLegendGroups([row('llm7', 'x', 0)], [silent])
- expect(groups).toHaveLength(0)
- expect(unpublishedCount).toBe(1)
- })
-
- it('falls back to one flat group before the pools have loaded', () => {
- const { groups, unpublishedCount, rowCount } = buildLegendGroups(
- [row('groq', 'a', 10), row('groq', 'b', 0)],
- undefined,
- )
- expect(groups).toHaveLength(1)
- expect(groups[0].pool).toBeNull()
- expect(rowCount).toBe(1)
- expect(unpublishedCount).toBe(1)
- })
-
- it('orders pools the way the server sent them and puts loose models last', () => {
- const small = pool({ poolKey: 'kilo::anonymous', platform: 'kilo', memberModelIds: ['k'], documentedBudget: 5 })
- const { groups } = buildLegendGroups(
- [row('custom', 'c', 30), row('kilo', 'k', 5), row('groq', 'a', 10)],
- [groq, small, solo],
- )
- expect(groups.map(g => g.pool?.poolKey ?? 'independent')).toEqual([
- 'groq::account',
- 'kilo::anonymous',
- 'independent',
- ])
- })
-})
diff --git a/client/src/lib/pool-legend.ts b/client/src/lib/pool-legend.ts
deleted file mode 100644
index f518f5a5..00000000
--- a/client/src/lib/pool-legend.ts
+++ /dev/null
@@ -1,151 +0,0 @@
-// Grouping for the token-bar legend: which quota pool each model row belongs
-// to (#907 + #1005 merged into one list).
-//
-// The stacked bar's legend lists models; GET /api/free-tier lists the provider
-// pools those models draw from. They used to be two separate accordions saying
-// related things about the same numbers, so the legend now nests its model rows
-// under their pool. The join is platform + provider model id, which is what
-// both payloads carry per model — the pool key itself is provider knowledge
-// (see inferQuotaPoolKey on the server) and is deliberately NOT re-derived here.
-
-import type { TokenUsageData } from './routing'
-
-export type PoolKind = 'documented' | 'credits' | 'unpublished'
-
-export interface FreeTierPool {
- poolKey: string
- platform: string
- memberModelIds: string[]
- modelCount: number
- disabledModelCount: number
- keyCount: number
- documentedBudget: number
- bestLabel: string
- kind: PoolKind
- quota: {
- limit: number | null
- remaining: number | null
- resetAt: string | null
- metric: string
- keyCount: number
- } | null
-}
-
-export interface FreeTierResponse {
- generatedAt: string
- summary: {
- poolCount: number
- documentedMonthlyTokens: number
- creditsBasedPools: number
- unpublishedPools: number
- }
- pools: FreeTierPool[]
-}
-
-export type LegendModel = TokenUsageData['models'][number]
-
-export interface LegendRow extends LegendModel {
- usedTokens: number
- remainingTokens: number
- widthPct: number
-}
-
-export interface LegendGroup {
- /** The pool these rows share, or null for the trailing independent-budget
- * group (models whose "pool" is just themselves). */
- pool: FreeTierPool | null
- models: LegendRow[]
-}
-
-export interface LegendGrouping {
- groups: LegendGroup[]
- /** Rows with no budget to show, folded into the "+N with no published quota"
- * summary line the same way the flat legend folded them. */
- unpublishedCount: number
- /** How many model rows the groups actually render (drives "show all N"). */
- rowCount: number
-}
-
-/** A pool of one model is not a pool: the model IS the budget. Those rows read
- * better ungrouped, so they collect in one trailing group instead of getting a
- * header each. */
-export function isPerModelPool(pool: FreeTierPool): boolean {
- return pool.memberModelIds.some(id => pool.poolKey === `${pool.platform}::${id}`)
-}
-
-const scopeSeparator = '::'
-
-/**
- * "groq::account" -> "account", "nvidia::credit-pool" -> "credit",
- * "cerebras::shared" -> "" (nothing left worth saying).
- * The caller turns what comes back into a sentence, so a scope that adds no
- * information comes back empty rather than as a word to repeat.
- */
-export function poolScopeWords(poolKey: string): string {
- const index = poolKey.indexOf(scopeSeparator)
- const scope = index === -1 ? '' : poolKey.slice(index + scopeSeparator.length)
- const words = scope.replace(/[-_]+/g, ' ').replace(/\s+pool$/, '').trim()
- return words === 'shared' ? '' : words
-}
-
-/**
- * Nest the legend's model rows under their pools.
- *
- * Pool order is the server's (largest documented budget first) so the biggest
- * allowance heads the list; model order inside a group is the chain order the
- * bar itself uses. A pool that publishes nothing AND has no live reading and no
- * budgeted member says nothing a reader can act on, so it folds into the
- * unpublished count instead of taking up a header row.
- */
-export function buildLegendGroups(models: LegendRow[], pools: FreeTierPool[] | undefined): LegendGrouping {
- const budgeted = models.filter(m => m.budget > 0)
-
- if (!pools || pools.length === 0) {
- // No pool data (still loading, or an install with no keys): the flat legend.
- return {
- groups: budgeted.length ? [{ pool: null, models: budgeted }] : [],
- unpublishedCount: models.length - budgeted.length,
- rowCount: budgeted.length,
- }
- }
-
- const poolByModel = new Map<string, FreeTierPool>()
- for (const pool of pools) {
- for (const id of pool.memberModelIds) poolByModel.set(`${pool.platform}::${id}`, pool)
- }
-
- const shared = new Map<string, LegendRow[]>()
- const independent: LegendRow[] = []
- let unpublishedCount = 0
- for (const model of models) {
- const pool = model.modelId ? poolByModel.get(`${model.platform}::${model.modelId}`) : undefined
- if (model.budget <= 0) {
- unpublishedCount += 1
- continue
- }
- if (!pool || isPerModelPool(pool)) {
- independent.push(model)
- continue
- }
- const list = shared.get(pool.poolKey)
- if (list) list.push(model)
- else shared.set(pool.poolKey, [model])
- }
-
- const groups: LegendGroup[] = []
- for (const pool of pools) {
- if (isPerModelPool(pool)) continue
- const rows = shared.get(pool.poolKey) ?? []
- // A live remaining figure or a documented budget is worth a header even
- // when every member model is itself unpublished.
- const worthShowing = rows.length > 0 || pool.documentedBudget > 0 || pool.quota?.remaining != null
- if (worthShowing) groups.push({ pool, models: rows })
- }
- if (independent.length) groups.push({ pool: null, models: independent })
-
- return {
- groups,
- unpublishedCount,
- rowCount: groups.reduce((sum, g) => sum + g.models.length, 0),
- }
-}
diff --git a/client/src/lib/routing.ts b/client/src/lib/routing.ts
index 7a5fd46d..d67fbb6c 100644
--- a/client/src/lib/routing.ts
+++ b/client/src/lib/routing.ts
@@ -115,7 +115,9 @@ export type Row = FallbackEntry & Partial<RoutingScore>
export interface TokenUsageData {
totalBudget: number
totalUsed: number
- models: { displayName: string; platform: string; modelId?: string; budget: number; used?: number }[]
+ /** Served smartest-first (intelligenceRank 1 = smartest); the bar and its
+ * legend keep that order. */
+ models: { displayName: string; platform: string; modelId?: string; intelligenceRank?: number; budget: number; used?: number }[]
}
// Custom endpoints all share the generic 'custom' platform id, so show the
diff --git a/server/src/__tests__/routes/fallback.test.ts b/server/src/__tests__/routes/fallback.test.ts
index 990d2d52..f3625fbe 100644
--- a/server/src/__tests__/routes/fallback.test.ts
+++ b/server/src/__tests__/routes/fallback.test.ts
@@ -105,6 +105,35 @@ describe('Fallback API', () => {
});
});
+ // The budget bar's legend follows this payload's order. Chain priority is
+ // seeded provider by provider, so ordering by it read as "grouped by
+ // provider" on the dashboard (#1243); the legend wants smartest first.
+ it('GET /api/fallback/token-usage lists models smartest first and carries the rank', async () => {
+ const db = getDb();
+ // Two platforms with keys, several models each, in a deliberately
+ // provider-clustered chain order so the old ORDER BY priority would fail.
+ const platforms = (db.prepare(`
+ SELECT DISTINCT platform FROM models ORDER BY platform LIMIT 2
+ `).all() as { platform: string }[]).map(p => p.platform);
+ for (const platform of platforms) {
+ const secret = encrypt(`order-test-${platform}`);
+ db.prepare(`
+ INSERT INTO api_keys (platform, label, encrypted_key, iv, auth_tag, status, enabled)
+ VALUES (?, 'order', ?, ?, ?, 'healthy', 1)
+ `).run(platform, secret.encrypted, secret.iv, secret.authTag);
+ }
+
+ const { status, body } = await request(app, 'GET', '/api/fallback/token-usage');
+ expect(status).toBe(200);
+ const ranks: number[] = body.models.map((m: any) => m.intelligenceRank);
+ expect(ranks.length).toBeGreaterThan(2);
+ for (const r of ranks) expect(typeof r).toBe('number');
+ expect(ranks).toEqual([...ranks].sort((a, b) => a - b));
+ // Not merely coincident with chain order: the models table holds more than
+ // one platform here, so a priority-ordered list would interleave differently.
+ expect(new Set(body.models.map((m: any) => m.platform)).size).toBeGreaterThan(1);
+ });
+
// Regression: GET /routing must always carry customWeights, even before the
// user has saved any — the dashboard's custom-weight sliders dereference it
// and a missing field white-screened the Fallback page.
diff --git a/server/src/routes/fallback.ts b/server/src/routes/fallback.ts
index bb9d8733..b5777132 100644
--- a/server/src/routes/fallback.ts
+++ b/server/src/routes/fallback.ts
@@ -487,29 +487,33 @@ fallbackRouter.get('/token-usage', (_req: Request, res: Response) => {
? db.prepare('SELECT id FROM profiles WHERE id = ?').get(activeProfileId) as any
: null;
- let rawModels: { model_db_id: number; platform: string; model_id: string; display_name: string; monthly_token_budget: string; priority: number; enabled: number; rpm_limit: number | null; rpd_limit: number | null; tpm_limit: number | null; tpd_limit: number | null }[];
+ // Ordered by intelligence (rank 1 = smartest), not chain priority: the
+ // dashboard's monthly-budget bar and legend follow this order, and chain
+ // priority is seeded provider by provider, which read as "grouped by
+ // provider" (#1243). Priority stays as the tiebreaker within a rank.
+ let rawModels: { model_db_id: number; platform: string; model_id: string; display_name: string; monthly_token_budget: string; priority: number; enabled: number; intelligence_rank: number; rpm_limit: number | null; rpd_limit: number | null; tpm_limit: number | null; tpd_limit: number | null }[];
if (activeProfile) {
// Profile mode: use profile_models chain (all models in profile, checked against enabled)
rawModels = db.prepare(`
SELECT m.id as model_db_id, m.platform, m.model_id, m.display_name, m.monthly_token_budget,
- pm.priority, pm.enabled,
+ pm.priority, pm.enabled, m.intelligence_rank,
m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit
FROM profile_models pm
JOIN models m ON m.id = pm.model_db_id
WHERE pm.profile_id = ? AND m.enabled = 1
- ORDER BY pm.priority ASC
+ ORDER BY m.intelligence_rank ASC, pm.priority ASC
`).all(activeProfileId) as any[];
} else {
// Default mode: use fallback_config (only include enabled models)
rawModels = db.prepare(`
SELECT m.id as model_db_id, m.platform, m.model_id, m.display_name, m.monthly_token_budget,
- fc.priority, fc.enabled,
+ fc.priority, fc.enabled, m.intelligence_rank,
m.rpm_limit, m.rpd_limit, m.tpm_limit, m.tpd_limit
FROM fallback_config fc
JOIN models m ON m.id = fc.model_db_id
WHERE m.enabled = 1
- ORDER BY fc.priority ASC
+ ORDER BY m.intelligence_rank ASC, fc.priority ASC
`).all() as any[];
}
@@ -537,6 +541,7 @@ fallbackRouter.get('/token-usage', (_req: Request, res: Response) => {
displayName: m.display_name,
platform: m.platform,
modelId: m.model_id,
+ intelligenceRank: m.intelligence_rank,
budget: parseBudget(m.monthly_token_budget) * keys,
used: usageByModel.get(`${m.platform}:${m.model_id}`) ?? 0,
enabled: m.enabled === 1,