@alexeiled/claude-router 1.6.1 → 1.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/panel.mjs CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  TIERS,
14
14
  tuningOf,
15
15
  } from './config.mjs';
16
+ import { routeEffort } from './cost.mjs';
16
17
  import {
17
18
  ACTIVITY_LETTER,
18
19
  activityReading,
@@ -21,6 +22,7 @@ import {
21
22
  classifierLabel,
22
23
  classifierStatus,
23
24
  credentialsNeeded,
25
+ differenceShare,
24
26
  formatTokens,
25
27
  GATEWAY_CLEANUP,
26
28
  GATEWAY_SETTINGS,
@@ -31,12 +33,20 @@ import {
31
33
  REASONS,
32
34
  routeLabel,
33
35
  routeName,
36
+ savingsColor,
37
+ signedUsd,
38
+ sinceDate,
34
39
  sparkline,
40
+ strongerReplies,
35
41
  switchCount,
36
42
  TIER_COLOR,
43
+ unavailableReason,
37
44
  usageMetrics,
45
+ usd,
46
+ yourModel,
38
47
  } from './display.mjs';
39
- import { isSameModel } from './route.mjs';
48
+ import { cellForModel, isSameModel } from './route.mjs';
49
+ import { EARLY_REPLIES, emptyTotals, modelSpec, readout, readSavingsStore } from './savings.mjs';
40
50
 
41
51
  const TABS = [
42
52
  ['now', '1', 'Now'],
@@ -49,14 +59,15 @@ const TUNING_CHOICES = {
49
59
  downgradeVotes: [1, 2, 3],
50
60
  horizon: [1, 3, 5, 10],
51
61
  cashCapUsd: [0.5, 1, 2, 5],
62
+ activityMass: [0.5, 0.6, 0.7, 0.8],
52
63
  };
53
64
  const LADDER = [...TIERS].reverse();
54
65
  const SESSION_EFFORT = 'session';
55
66
 
56
67
  const ACTIVITY_MODE_HINT = {
57
- off: 'asks nothing new; tier routes only',
58
- shadow: 'asks and shows what on would do; tier routes run',
59
- on: 'activity overrides run',
68
+ off: 'the activity is not asked; base routes run',
69
+ shadow: 'shows what on would do; base routes run',
70
+ on: 'an override runs when the activity has one',
60
71
  };
61
72
  const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max' };
62
73
 
@@ -113,7 +124,7 @@ export function renderPanel(
113
124
  view,
114
125
  usage,
115
126
  actions,
116
- { modelOptions = Object.keys(config.models) } = {},
127
+ { modelOptions = Object.keys(config.models), columns = null } = {},
117
128
  ) {
118
129
  const ui = kit(elements);
119
130
  const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
@@ -122,7 +133,7 @@ export function renderPanel(
122
133
  flexDirection: 'column',
123
134
  children: [
124
135
  ...header(ui, config, view, tab, actions),
125
- ...body(ui, config, view, usage, actions, modelOptions),
136
+ ...body(ui, config, view, usage, actions, modelOptions, columns),
126
137
  ...(view.help ? helpLines(ui) : []),
127
138
  ...statusBar(ui, config, view, actions),
128
139
  ui.Button({ key: 'close', label: 'Close', hotkey: 'q', role: 'dismiss', onPress: actions.close }),
@@ -130,40 +141,49 @@ export function renderPanel(
130
141
  });
131
142
  }
132
143
 
133
- function header(ui, config, view, tab, actions) {
134
- const auto = view.mode === 'auto';
144
+ // Routing's state and its one mode control. While routing is unavailable, on and off would change nothing, so the
145
+ // header says why instead of offering them.
146
+ function routingState(ui, config, view, actions) {
147
+ if (view.phase === 'unavailable')
148
+ return [
149
+ ui.line(
150
+ ui.head('ROUTING'),
151
+ ' ',
152
+ ui.text('unavailable', { color: 'yellow' }),
153
+ ui.dim(' Claude’s model is kept'),
154
+ ),
155
+ ui.text(unavailableReason(view), { color: 'yellow' }),
156
+ ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
157
+ ];
135
158
  const label = classifierLabel(config);
136
159
  const error = view.error ?? missingCredentials(config, view, config.classifier);
137
- const status =
138
- view.phase === 'unavailable'
139
- ? ui.text('Routing unavailable', { color: 'yellow' })
140
- : error
141
- ? ui.text(classifierStatus(config, error), { color: 'yellow' })
142
- : ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, {
143
- color: 'green',
144
- });
160
+ // A radio pair, marked as the Classifier tab marks its active row.
161
+ const option = (mode, label, hotkey) =>
162
+ ui.Button({
163
+ key: mode,
164
+ label: `${view.mode === mode ? '◉' : '○'} ${label}`,
165
+ hotkey,
166
+ variant: view.mode === mode ? 'primary' : 'secondary',
167
+ onPress: () => actions.mode(mode),
168
+ });
145
169
  return [
146
- ui.line(ui.head(`ROUTING · ${auto ? 'on' : 'off'}`), ' ', status),
147
- view.phase === 'unavailable' && view.error ? ui.text(view.error, { color: 'yellow' }) : null,
148
- ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
149
170
  ui.line(
150
- ui.Button({
151
- key: 'auto',
152
- label: 'Routing on',
153
- hotkey: 'o',
154
- variant: auto ? 'primary' : 'secondary',
155
- onPress: () => actions.mode('auto'),
156
- }),
171
+ ui.head('ROUTING'),
172
+ ' ',
173
+ option('auto', 'on', 'o'),
157
174
  ' ',
158
- ui.Button({
159
- key: 'manual',
160
- label: 'Routing off',
161
- hotkey: 'f',
162
- variant: auto ? 'secondary' : 'primary',
163
- onPress: () => actions.mode('manual'),
164
- }),
165
- ui.dim(' Routing off keeps the /model choice'),
175
+ option('manual', 'off', 'f'),
176
+ ui.dim(' off keeps the /model choice'),
166
177
  ),
178
+ error
179
+ ? ui.text(classifierStatus(config, error), { color: 'yellow' })
180
+ : ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, { color: 'green' }),
181
+ ];
182
+ }
183
+
184
+ function header(ui, config, view, tab, actions) {
185
+ return [
186
+ ...routingState(ui, config, view, actions),
167
187
  ui.line(
168
188
  ...TABS.flatMap(([id, hotkey, label]) => [
169
189
  ui.Button({
@@ -183,10 +203,11 @@ function header(ui, config, view, tab, actions) {
183
203
 
184
204
  function nowTab(ui, config, view, usage, actions) {
185
205
  const metrics = usageMetrics(config, view, usage);
186
- const auto = view.mode === 'auto';
206
+ const unavailable = view.phase === 'unavailable';
207
+ const auto = view.mode === 'auto' && !unavailable;
187
208
  const current = view.selectedModel ?? view.nativeModel;
188
209
  const label = classifierLabel(config);
189
- const missing = missingCredentials(config, view, config.classifier);
210
+ const missing = unavailable ? null : missingCredentials(config, view, config.classifier);
190
211
  const served = view.actualModel && current && !isSameModel(current, view.actualModel);
191
212
  const reading = config.activityRouting === 'off' ? null : activityReading(view);
192
213
  const route = activityRouteText(config, view);
@@ -250,7 +271,13 @@ function nowTab(ui, config, view, usage, actions) {
250
271
  );
251
272
  }
252
273
  out.push(
253
- ui.dim(auto ? ' Pins serve the next turn only.' : ' Pins need routing on. Routing off keeps the /model choice.'),
274
+ ui.dim(
275
+ unavailable
276
+ ? ' Pins need routing, which is unavailable.'
277
+ : auto
278
+ ? ' Pins serve the next turn only.'
279
+ : ' Pins need routing on. Routing off keeps the /model choice.',
280
+ ),
254
281
  );
255
282
  out.push(ui.text(' '), ...replies(ui, view));
256
283
  out.push(
@@ -275,8 +302,8 @@ function nowTab(ui, config, view, usage, actions) {
275
302
  function replies(ui, view) {
276
303
  const tiers = view.tiers ?? [];
277
304
  if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
278
- const switches = switchCount(tiers);
279
305
  const activities = view.activities ?? [];
306
+ const switches = switchCount((view.routes ?? []).slice(-tiers.length));
280
307
  const letters = tiers.map((_, i) => activities[activities.length - tiers.length + i] ?? null);
281
308
  return [
282
309
  ui.head(`REPLIES · last ${tiers.length}`),
@@ -301,6 +328,13 @@ const POLICY_ROWS = [
301
328
  ['downgradeVotes', 'Votes to go down', String, 'agreeing turns before cheaper', 'policy.downgradeVotes'],
302
329
  ['horizon', 'Payback horizon', (v) => `${v} turns`, 'a switch repays its cache', 'policy.downgradeHorizonTurns'],
303
330
  ['cashCapUsd', 'Credits cap', (v) => `$${v.toFixed(2)}`, 'max cold write on credits', 'policy.cashCapUsd'],
331
+ [
332
+ 'activityMass',
333
+ 'Activity threshold',
334
+ percent,
335
+ 'a weaker reading keeps the running activity or uses the base route',
336
+ 'policy.activityMass',
337
+ ],
304
338
  ];
305
339
 
306
340
  // What the Routing tab changed and has not saved: tiers, baseline, activity cells and mode against the saved config,
@@ -397,7 +431,7 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
397
431
  ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
398
432
  ui.dim('A new model ID needs router.json → models: price, window, efforts.'),
399
433
  ui.text(' '),
400
- ...activitySections(ui, config, changes, actions, modelOptions),
434
+ ...activitySections(ui, config, view, changes, actions, modelOptions),
401
435
  ui.text(' '),
402
436
  ui.line(ui.head('POLICY'), ui.dim(' · applies next turn · edit, then Save')),
403
437
  ...POLICY_ROWS.map(([key, label, format, hint]) =>
@@ -456,7 +490,7 @@ function overrideWarnings(config, uses, activity, tier) {
456
490
  }
457
491
 
458
492
  // ACTIVITIES, the effective matrix of the draft, and OVERRIDES, its editable cells. Both join the routing draft.
459
- function activitySections(ui, config, changes, actions, modelOptions) {
493
+ function activitySections(ui, config, view, changes, actions, modelOptions) {
460
494
  const draft = { ...config, ...changes.draft };
461
495
  const mode = draft.activityRouting;
462
496
  const cellRoutes = ACTIVITIES.flatMap((activity) => TIERS.map((tier) => resolveRoute(draft, tier, activity)));
@@ -475,19 +509,9 @@ function activitySections(ui, config, changes, actions, modelOptions) {
475
509
  }
476
510
  const distinct = uses.size;
477
511
  const matrixRow = (name, cells) => ui.text(` ${name.padEnd(30)}${cells.map((c) => c.padEnd(9)).join('')}`);
512
+ // The mode leads the section: it decides whether the matrix below runs or is only shown.
478
513
  const out = [
479
- ui.line(ui.cell(32, ui.head(`ACTIVITIES · routing ${mode}`)), ui.dim(TIERS.map((t) => t.padEnd(9)).join(''))),
480
- matrixRow(
481
- 'base',
482
- TIERS.map((tier) => shortRoute(draft, draft.routes[tier])),
483
- ),
484
- ...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
485
- ui.dim(
486
- ` ${distinct} distinct route${distinct === 1 ? '' : 's'} = ${distinct} cache${distinct === 1 ? '' : 's'}` +
487
- `${mode === 'on' ? '' : ' once activity routing is on'}`,
488
- ),
489
- ui.text(' '),
490
- ui.line(ui.head('OVERRIDES'), ui.dim(' · applies next turn · edit, then Save')),
514
+ ui.line(ui.head('ACTIVITIES'), ui.dim(' · applies next turn · edit, then Save')),
491
515
  ui.line(
492
516
  ui.cell(
493
517
  26,
@@ -504,6 +528,18 @@ function activitySections(ui, config, changes, actions, modelOptions) {
504
528
  marker(ui, changes.mode),
505
529
  ui.dim(ACTIVITY_MODE_HINT[mode]),
506
530
  ),
531
+ ui.line(ui.cell(32, ''), ui.dim(TIERS.map((t) => t.padEnd(9)).join(''))),
532
+ matrixRow(
533
+ 'base',
534
+ TIERS.map((tier) => shortRoute(draft, draft.routes[tier])),
535
+ ),
536
+ ...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
537
+ ui.dim(
538
+ ` ${distinct} distinct route${distinct === 1 ? '' : 's'} = ${distinct} cache${distinct === 1 ? '' : 's'}` +
539
+ `${mode === 'on' ? '' : ' once activity routing is on'}`,
540
+ ),
541
+ ui.text(' '),
542
+ ui.line(ui.head('OVERRIDES'), ...overridesState(ui, view, config.activityRouting, mode)),
507
543
  ];
508
544
  const free = [];
509
545
  for (const activity of ACTIVITIES) {
@@ -538,6 +574,24 @@ function activitySections(ui, config, changes, actions, modelOptions) {
538
574
  return out;
539
575
  }
540
576
 
577
+ // Whether the overrides run now: activity routing on is not enough while routing itself is off or unavailable.
578
+ // Whether overrides run now: the saved mode decides, since a draft runs nothing until Save. A different draft mode
579
+ // says what Save will change.
580
+ function overridesState(ui, view, saved, drafted) {
581
+ const why = (mode) =>
582
+ view.phase === 'unavailable'
583
+ ? 'routing is unavailable'
584
+ : view.mode === 'manual'
585
+ ? 'routing is off'
586
+ : mode === 'on'
587
+ ? null
588
+ : `activity routing is ${mode}`;
589
+ const now = why(saved);
590
+ const state = now ? ui.text(` · not in use: ${now}`, { color: 'yellow' }) : ui.dim(' · in use');
591
+ if (drafted === saved || Boolean(why(drafted)) === Boolean(now)) return [state];
592
+ return [state, ui.dim(why(drafted) ? ' · not in use after Save' : ' · in use after Save')];
593
+ }
594
+
541
595
  function overrideRow(ui, draft, route, activity, tier, edited, actions, modelOptions, uses) {
542
596
  const model = draft.models[route.model];
543
597
  const aliases = modelOptions.includes(route.model) ? modelOptions : [route.model, ...modelOptions];
@@ -710,8 +764,8 @@ function shadowLine(shadow) {
710
764
  return `${counted} · est. ${difference(shadow.minUsd)} … ${difference(shadow.maxUsd)}${some} at list prices`;
711
765
  }
712
766
 
713
- // The counts kept across sessions, read from the store as last loaded: what the decision to turn activity routing on
714
- // by default reads. Agreement uses the classifier's raw answer, so it can differ from the session's.
767
+ // The counts kept across sessions, read from the store as last loaded. Agreement uses the classifier's raw answer, so it
768
+ // can differ from the session's.
715
769
  function storeUsage(ui, store) {
716
770
  const summary = storeSummary(readStore(store));
717
771
  const out = [ui.head('ACROSS SESSIONS · since the last reset')];
@@ -732,7 +786,7 @@ function storeUsage(ui, store) {
732
786
  ui.text(`Mismatch ${summary.disagreements.map((d) => `${d.predicted} → ${d.observed} ${d.n}`).join(' · ')}`),
733
787
  );
734
788
  const { taken, refused } = summary.lateral;
735
- if (taken + refused > 0) out.push(ui.text(`Lateral ${taken} taken · ${refused} refused`));
789
+ if (taken + refused > 0) out.push(ui.text(`Activity moves ${taken} taken · ${refused} refused`));
736
790
  if (summary.shadow.turns > 0) out.push(ui.text(shadowLine(summary.shadow)));
737
791
  return out;
738
792
  }
@@ -742,20 +796,19 @@ function storeUsage(ui, store) {
742
796
  function activityUsage(ui, config, view, actions) {
743
797
  const stats = view.activityStats ?? null;
744
798
  const mode = config.activityRouting;
745
- const reset = ui.Button({
746
- key: 'reset-activity-stats',
747
- label: 'Reset activity stats',
748
- onPress: actions.resetActivityStats,
749
- });
799
+ const reset = ui.line(
800
+ ui.Button({ key: 'reset-stats', label: 'Reset stats', onPress: actions.resetStats }),
801
+ ui.dim(' clears activity stats and routing vs your model, all sessions'),
802
+ );
750
803
  const across = [ui.text(' '), ...storeUsage(ui, view.activityStore)];
751
804
  if (mode === 'off' && !stats)
752
805
  return [
753
- ui.head('ACTIVITY · routing off'),
806
+ ui.head('ACTIVITY · activity routing off'),
754
807
  ui.dim(' Not asked. Set activity routing to shadow or on in the Routing tab.'),
755
808
  ...(storeSummary(readStore(view.activityStore)).labelled > 0 ? across : []),
756
809
  reset,
757
810
  ];
758
- const out = [ui.line(ui.head('ACTIVITY · this session'), ui.dim(` routing ${mode}`))];
811
+ const out = [ui.line(ui.head('ACTIVITY · this session'), ui.dim(` activity routing ${mode}`))];
759
812
  const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
760
813
  if (!rows.length) out.push(ui.dim(' no activity readings yet'));
761
814
  else {
@@ -785,7 +838,237 @@ function activityUsage(ui, config, view, actions) {
785
838
  return [...out, ...across, reset];
786
839
  }
787
840
 
788
- function usageTab(ui, config, view, usage, actions) {
841
+ // Bars are this wide at the larger of routed and your model; the section drops them before any number when the pane is
842
+ // narrower than the table.
843
+ const SAVINGS_BAR = 22;
844
+ const SAVINGS_COLUMN = 15;
845
+ const SAVINGS_PARTS = [' cheaper models', ' stronger than yours', ' cache writes from switches'];
846
+ // The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
847
+ // line that ends in a joiner.
848
+ const SAVINGS_FOOTER = [
849
+ ['API list prices', ' · '],
850
+ ['same tokens and output length', ' · '],
851
+ ['not a bill;', ' '],
852
+ ['on a Claude plan it stands for quota', ' · '],
853
+ ['answer quality not measured', ''],
854
+ ];
855
+ const FULL_WIDTH = 68;
856
+
857
+ // `pieces` packed into lines of at most `width` cells after `indent`.
858
+ function pack(pieces, width, indent) {
859
+ const lines = [];
860
+ let line = '';
861
+ let joiner = '';
862
+ for (const [text, next] of pieces) {
863
+ if (line && indent.length + line.length + joiner.length + text.length > width) {
864
+ lines.push(indent + line);
865
+ line = text;
866
+ } else line += (line ? joiner : '') + text;
867
+ joiner = next;
868
+ }
869
+ return line ? [...lines, indent + line] : lines;
870
+ }
871
+
872
+ // A dim sentence wrapped by words to a narrow pane, keeping its indent, which Ink's own wrap would drop.
873
+ const dimLines = (ui, text, columns) => {
874
+ const indent = /^ */.exec(text)[0];
875
+ const words = text
876
+ .trim()
877
+ .split(' ')
878
+ .map((word) => [word, ' ']);
879
+ return (Number.isFinite(columns) ? pack(words, columns, indent) : [text]).map((line) => ui.dim(line));
880
+ };
881
+ const footer = (ui, columns) =>
882
+ pack(SAVINGS_FOOTER, Math.min(FULL_WIDTH, columns ?? FULL_WIDTH), ' ').map((line) => ui.dim(line));
883
+
884
+ // One readout cell of the Difference row: "too early" below EARLY_REPLIES replies, else the signed amount and share.
885
+ function differenceCell(ui, r) {
886
+ if (!r) return '';
887
+ if (r.early) return ui.dim('too early');
888
+ const share = differenceShare(r);
889
+ return ui.text(`${signedUsd(r.differenceUsd)}${share ? ` ${share}` : ''}`, {
890
+ bold: true,
891
+ color: savingsColor(r.differenceUsd),
892
+ });
893
+ }
894
+
895
+ const partCell = (ui, value) => (value === null ? '' : ui.text(signedUsd(value), { color: savingsColor(value) }));
896
+
897
+ // A bar of `parts` ([usd, color]) at `scale` cells per dollar, filled to SAVINGS_BAR with dim cells. Each part ends at
898
+ // its rounded running total, so the parts add up to the bar.
899
+ function moneyBar(ui, parts, scale) {
900
+ let at = 0;
901
+ let sum = 0;
902
+ const out = [];
903
+ for (const [value, color] of parts) {
904
+ sum += value;
905
+ const end = Math.round(sum * scale);
906
+ if (end > at) out.push(ui.text('█'.repeat(end - at), color ? { color } : {}));
907
+ at = Math.max(at, end);
908
+ }
909
+ if (at < SAVINGS_BAR) out.push(ui.dim('░'.repeat(SAVINGS_BAR - at)));
910
+ return out;
911
+ }
912
+
913
+ // The parts of a session readout past "too early": each part, the stronger replies, the two costs as bars (not in a
914
+ // narrow pane), the replies by tier, why routing spent more, and the shadow estimate.
915
+ function savingsDetail(ui, config, view, { totals, session, across, row, tier, narrow, columns }) {
916
+ const out = [];
917
+ const savedPart = (key) => (across && !across.early ? partCell(ui, across[key]) : null);
918
+ const stronger = strongerReplies(totals);
919
+ out.push(
920
+ row(ui.dim(SAVINGS_PARTS[0]), partCell(ui, session.cheaperUsd), savedPart('cheaperUsd')),
921
+ row(ui.dim(SAVINGS_PARTS[1]), partCell(ui, session.strongerUsd), savedPart('strongerUsd')),
922
+ ...(stronger.count
923
+ ? dimLines(
924
+ ui,
925
+ ` ${stronger.count} repl${stronger.count === 1 ? 'y' : 'ies'} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`,
926
+ columns,
927
+ )
928
+ : []),
929
+ row(ui.dim(SAVINGS_PARTS[2]), partCell(ui, session.switchUsd), savedPart('switchUsd')),
930
+ ui.text(' '),
931
+ );
932
+ const largest = Math.max(totals.routedUsd, totals.yoursUsd);
933
+ if (!narrow && largest > 0) {
934
+ const scale = SAVINGS_BAR / largest;
935
+ const tiers = [...TIERS, 'none'];
936
+ const routedParts = tiers.map((t) => [totals.tierUsd[t] ?? 0, TIER_COLOR[t]]);
937
+ out.push(
938
+ ui.line(ui.dim(' routed '), ...moneyBar(ui, routedParts, scale), ` ${usd(session.routedUsd)}`),
939
+ ui.line(
940
+ ui.dim(' your model '),
941
+ ...moneyBar(ui, [[totals.yoursUsd, TIER_COLOR[tier]]], scale),
942
+ ` ${usd(session.yoursUsd)}`,
943
+ ),
944
+ );
945
+ }
946
+ const shares = [...TIERS, 'none'].filter((t) => totals.tiers[t] > 0);
947
+ out.push(
948
+ ui.line(
949
+ ' ',
950
+ ...shares.flatMap((t, i) => [
951
+ t === 'none' ? ui.dim('■ ') : ui.tier(t, '■ '),
952
+ ui.dim(
953
+ `${t === 'none' ? 'not routed' : t} ${percent(totals.tiers[t] / totals.replies)}${i === shares.length - 1 ? ' of replies' : ' '}`,
954
+ ),
955
+ ]),
956
+ ),
957
+ );
958
+ if (session.differenceUsd > 0 && stronger.count && session.strongerUsd >= session.switchUsd) {
959
+ const models = stronger.several ? 'stronger models' : modelName(stronger.model);
960
+ out.push(
961
+ ...dimLines(
962
+ ui,
963
+ ` Routing spent more than ${modelName(view.nativeModel)} alone because upper tiers ran on ${models}.`,
964
+ columns,
965
+ ),
966
+ ...dimLines(ui, ' To spend less, pick cheaper models for those tiers in the Routing tab.', columns),
967
+ );
968
+ }
969
+ const shadow = view.activityStats?.shadow;
970
+ if (config.activityRouting === 'shadow' && shadow?.estimated > 0)
971
+ out.push(
972
+ ui.line(
973
+ ui.dim(' if activity routing were on: '),
974
+ ui.text(`est. ${signedUsd(shadow.minUsd)} … ${signedUsd(shadow.maxUsd)}`, {
975
+ color: savingsColor((shadow.minUsd + shadow.maxUsd) / 2),
976
+ }),
977
+ ui.dim(' this session'),
978
+ ),
979
+ );
980
+ return out;
981
+ }
982
+
983
+ // The tier whose route is your model at your effort, for its color; else any tier on your model.
984
+ function yourTier(config, view) {
985
+ const runs = (tier) => {
986
+ const route = resolveRoute(config, tier);
987
+ return (
988
+ isSameModel(config.models[route.model].id, view.nativeModel) &&
989
+ routeEffort(config, route, view.nativeEffort) === (view.nativeEffort ?? null)
990
+ );
991
+ };
992
+ return TIERS.find(runs) ?? cellForModel(config, view.nativeModel)?.tier ?? null;
993
+ }
994
+
995
+ // ROUTING VS YOUR MODEL: what routed replies cost at list prices against the same tokens on the session's own model
996
+ // and effort, this session and since the last reset, split into its three parts; then the two costs as bars and the
997
+ // replies by tier. Below EARLY_REPLIES replies a column reads "too early"; with routing off or unavailable and no
998
+ // routed reply, one sentence says so.
999
+ function savingsSection(ui, config, view, columns) {
1000
+ const name = yourModel(view);
1001
+ const tier = yourTier(config, view);
1002
+ const title = [ui.head('ROUTING VS YOUR MODEL'), ui.dim(' · '), tier ? ui.tier(tier, name) : ui.text(name)];
1003
+ if (!modelSpec(config, view.nativeModel))
1004
+ return [ui.line(...title), ui.dim(` ${modelName(view.nativeModel)} has no list price in router.json models.`)];
1005
+ const totals = view.savings ?? emptyTotals();
1006
+ const session = readout(totals);
1007
+ const store = readSavingsStore(view.savingsStore);
1008
+ let across = store.replies ? readout(store) : null;
1009
+ const same = 'Same tokens on your model';
1010
+ const unavailable = view.phase === 'unavailable';
1011
+ const quiet = (unavailable || view.mode === 'manual') && !session.replies;
1012
+ // The label column fits the longest label shown, two spaces before the amounts.
1013
+ const labels = [` ${same}`, ...(quiet || session.early ? [] : SAVINGS_PARTS)];
1014
+ const widest = Math.max(...labels.map((text) => text.length)) + 2;
1015
+ const titleWidth = 'ROUTING VS YOUR MODEL · '.length + name.length;
1016
+ // The last column needs only its widest value, "−$11.42 −22%".
1017
+ const table = (label) => label + SAVINGS_COLUMN + (across ? 13 : 0);
1018
+ // The column heads share the title's line when it fits; a narrow pane puts them on their own and drops the bars,
1019
+ // and a narrower one the saved column too.
1020
+ let label = Math.max(titleWidth + 3, widest);
1021
+ const narrow = Number.isFinite(columns) && table(label) > columns;
1022
+ if (narrow) label = widest;
1023
+ if (narrow && table(label) > columns) across = null;
1024
+ const heads = [
1025
+ ui.cell(SAVINGS_COLUMN, ui.dim('this session')),
1026
+ across ? ui.dim(`since ${sinceDate(store.since)}`) : null,
1027
+ ];
1028
+ const row = (name, mine, saved = null) =>
1029
+ ui.line(ui.cell(label, name), ui.cell(SAVINGS_COLUMN, mine), across && saved !== null ? saved : null);
1030
+ // The saved column adds up sessions that each had their own model.
1031
+ const notes = () => [
1032
+ ui.text(' '),
1033
+ ...(across
1034
+ ? dimLines(ui, ` since ${sinceDate(store.since)}: each session compared with its own model`, columns)
1035
+ : []),
1036
+ ...footer(ui, columns),
1037
+ ];
1038
+ const out = narrow
1039
+ ? [ui.line(...title), ui.line(ui.cell(label, ''), ...heads)]
1040
+ : [ui.line(ui.cell(label, ui.line(...title)), ...heads)];
1041
+ if (quiet) {
1042
+ out.push(
1043
+ ...dimLines(
1044
+ ui,
1045
+ ` Routing is ${unavailable ? 'unavailable' : 'off'}: every reply this session used your model.`,
1046
+ columns,
1047
+ ),
1048
+ row(' Difference', '$0.00', differenceCell(ui, across)),
1049
+ );
1050
+ return [...out, ...notes()];
1051
+ }
1052
+ out.push(
1053
+ row(' Routed replies', usd(session.routedUsd), across ? usd(across.routedUsd) : null),
1054
+ row(` ${same}`, usd(session.yoursUsd), across ? usd(across.yoursUsd) : null),
1055
+ row(ui.text(' Difference', { bold: true }), differenceCell(ui, session), differenceCell(ui, across)),
1056
+ );
1057
+ if (session.early) {
1058
+ out.push(
1059
+ ...dimLines(
1060
+ ui,
1061
+ ` ${session.replies} routed repl${session.replies === 1 ? 'y' : 'ies'} so far; the session figure appears after ${EARLY_REPLIES}`,
1062
+ columns,
1063
+ ),
1064
+ );
1065
+ return [...out, ...notes()];
1066
+ }
1067
+ out.push(...savingsDetail(ui, config, view, { totals, session, across, row, tier, narrow, columns }));
1068
+ return [...out, ...notes()];
1069
+ }
1070
+
1071
+ function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
789
1072
  const metrics = usageMetrics(config, view, usage);
790
1073
  const history = view.history ?? [];
791
1074
  const tiers = view.tiers ?? [];
@@ -801,6 +1084,8 @@ function usageTab(ui, config, view, usage, actions) {
801
1084
  )
802
1085
  : ui.dim(' no replies yet');
803
1086
  return [
1087
+ ...savingsSection(ui, config, view, columns),
1088
+ ui.text(' '),
804
1089
  ui.head('USAGE · Claude readings'),
805
1090
  ui.line(`Cost ${dollars(metrics.cost)} `, ui.dim('reported by Claude')),
806
1091
  ui.gauge('Context ', metrics.contextBar, contextSuffix(metrics)),
@@ -841,7 +1126,8 @@ function helpLines(ui) {
841
1126
  ui.head('ABOUT THESE NUMBERS'),
842
1127
  ui.dim('Cost, context and cache are Claude readings. $ estimates use the'),
843
1128
  ui.dim('list prices in router.json; plan prices are equivalents, not cash.'),
844
- ui.dim('Routing savings are not measured. Cache benefit is before writes.'),
1129
+ ui.dim('Routing vs your model holds tokens and output length the same'),
1130
+ ui.dim('and does not measure answer quality. Cache benefit is before writes.'),
845
1131
  ui.dim('The context bar uses the routed model’s window, 20% in reserve.'),
846
1132
  ui.dim('A switch estimate prices 5m–1h cache writes; minus means cheaper.'),
847
1133
  ];
package/lib/route.mjs CHANGED
@@ -9,7 +9,7 @@ const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
9
9
 
10
10
  // A request this much smaller than the last means the history was compacted or rewound: the cached prefixes the
11
11
  // observations describe are gone, so they are dropped rather than priced as warm.
12
- const HISTORY_SHRINK = 0.8;
12
+ export const HISTORY_SHRINK = 0.8;
13
13
 
14
14
  // Whether the engine served the requested model, allowing for a dated snapshot of it.
15
15
  export function isSameModel(requested, served) {
@@ -53,6 +53,8 @@ export function emptyLoop(config, model) {
53
53
  turnId: null,
54
54
  decision: null,
55
55
  ineligible: [],
56
+ // Your model's simulated cache for the routing-vs-your-model readout (compareReply in savings.mjs).
57
+ yours: null,
56
58
  };
57
59
  }
58
60
 
@@ -69,6 +71,7 @@ export function resetHistory(loop) {
69
71
  would: loop.would ? { ...loop.would, state: freshState(loop.would.state) } : null,
70
72
  // The active turn keeps its route, pin and fallback suspension; the next turn decides afresh.
71
73
  ineligible: [],
74
+ yours: null,
72
75
  };
73
76
  }
74
77
 
@@ -204,9 +207,12 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
204
207
  }
205
208
  if (shrinksUnmeasured(result)) result = { ...result, ...last, reason: 'context-unknown' };
206
209
  if (!fits(result)) {
210
+ // A cell labelled with another activity would run, and stick, a route this turn did not choose.
207
211
  const native = cellForModel(config, nativeModel);
208
212
  const cells = [last, native, ...TIERS.map((tier) => ({ tier, activity }))];
209
- const fallback = cells.find((cell) => cell && fits(cell)) ?? { tier: null, activity: null };
213
+ const fallback = cells.find(
214
+ (cell) => cell && (cell.activity === null || cell.activity === activity) && fits(cell),
215
+ ) ?? { tier: null, activity: null };
210
216
  result = { ...result, ...fallback, reason: 'model-unavailable' };
211
217
  }
212
218
  const route = result.tier ? routeOf(result) : null;