@alexeiled/claude-router 1.6.0 → 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
@@ -1,4 +1,5 @@
1
1
  import { compareRoutes } from './activity.mjs';
2
+ import { readStore, storeSummary } from './activity-stats.mjs';
2
3
  import {
3
4
  ACTIVITIES,
4
5
  ACTIVITY_MODES,
@@ -12,6 +13,7 @@ import {
12
13
  TIERS,
13
14
  tuningOf,
14
15
  } from './config.mjs';
16
+ import { routeEffort } from './cost.mjs';
15
17
  import {
16
18
  ACTIVITY_LETTER,
17
19
  activityReading,
@@ -20,6 +22,7 @@ import {
20
22
  classifierLabel,
21
23
  classifierStatus,
22
24
  credentialsNeeded,
25
+ differenceShare,
23
26
  formatTokens,
24
27
  GATEWAY_CLEANUP,
25
28
  GATEWAY_SETTINGS,
@@ -30,12 +33,20 @@ import {
30
33
  REASONS,
31
34
  routeLabel,
32
35
  routeName,
36
+ savingsColor,
37
+ signedUsd,
38
+ sinceDate,
33
39
  sparkline,
40
+ strongerReplies,
34
41
  switchCount,
35
42
  TIER_COLOR,
43
+ unavailableReason,
36
44
  usageMetrics,
45
+ usd,
46
+ yourModel,
37
47
  } from './display.mjs';
38
- import { isSameModel } from './route.mjs';
48
+ import { cellForModel, isSameModel } from './route.mjs';
49
+ import { EARLY_REPLIES, emptyTotals, modelSpec, readout, readSavingsStore } from './savings.mjs';
39
50
 
40
51
  const TABS = [
41
52
  ['now', '1', 'Now'],
@@ -48,14 +59,15 @@ const TUNING_CHOICES = {
48
59
  downgradeVotes: [1, 2, 3],
49
60
  horizon: [1, 3, 5, 10],
50
61
  cashCapUsd: [0.5, 1, 2, 5],
62
+ activityMass: [0.5, 0.6, 0.7, 0.8],
51
63
  };
52
64
  const LADDER = [...TIERS].reverse();
53
65
  const SESSION_EFFORT = 'session';
54
66
 
55
67
  const ACTIVITY_MODE_HINT = {
56
- off: 'asks nothing new; tier routes only',
57
- shadow: 'asks and shows what on would do; tier routes run',
58
- 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',
59
71
  };
60
72
  const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max' };
61
73
 
@@ -112,7 +124,7 @@ export function renderPanel(
112
124
  view,
113
125
  usage,
114
126
  actions,
115
- { modelOptions = Object.keys(config.models) } = {},
127
+ { modelOptions = Object.keys(config.models), columns = null } = {},
116
128
  ) {
117
129
  const ui = kit(elements);
118
130
  const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
@@ -121,7 +133,7 @@ export function renderPanel(
121
133
  flexDirection: 'column',
122
134
  children: [
123
135
  ...header(ui, config, view, tab, actions),
124
- ...body(ui, config, view, usage, actions, modelOptions),
136
+ ...body(ui, config, view, usage, actions, modelOptions, columns),
125
137
  ...(view.help ? helpLines(ui) : []),
126
138
  ...statusBar(ui, config, view, actions),
127
139
  ui.Button({ key: 'close', label: 'Close', hotkey: 'q', role: 'dismiss', onPress: actions.close }),
@@ -129,40 +141,49 @@ export function renderPanel(
129
141
  });
130
142
  }
131
143
 
132
- function header(ui, config, view, tab, actions) {
133
- 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
+ ];
134
158
  const label = classifierLabel(config);
135
159
  const error = view.error ?? missingCredentials(config, view, config.classifier);
136
- const status =
137
- view.phase === 'unavailable'
138
- ? ui.text('Routing unavailable', { color: 'yellow' })
139
- : error
140
- ? ui.text(classifierStatus(config, error), { color: 'yellow' })
141
- : ui.text(`${label} ready${Number.isFinite(view.adviceMs) ? ` · ${view.adviceMs} ms` : ''}`, {
142
- color: 'green',
143
- });
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
+ });
144
169
  return [
145
- ui.line(ui.head(`ROUTING · ${auto ? 'on' : 'off'}`), ' ', status),
146
- view.phase === 'unavailable' && view.error ? ui.text(view.error, { color: 'yellow' }) : null,
147
- ...(view.error === GATEWAY_SETTINGS ? GATEWAY_CLEANUP.map((line) => ui.text(line, { color: 'yellow' })) : []),
148
170
  ui.line(
149
- ui.Button({
150
- key: 'auto',
151
- label: 'Routing on',
152
- hotkey: 'o',
153
- variant: auto ? 'primary' : 'secondary',
154
- onPress: () => actions.mode('auto'),
155
- }),
171
+ ui.head('ROUTING'),
172
+ ' ',
173
+ option('auto', 'on', 'o'),
156
174
  ' ',
157
- ui.Button({
158
- key: 'manual',
159
- label: 'Routing off',
160
- hotkey: 'f',
161
- variant: auto ? 'secondary' : 'primary',
162
- onPress: () => actions.mode('manual'),
163
- }),
164
- ui.dim(' Routing off keeps the /model choice'),
175
+ option('manual', 'off', 'f'),
176
+ ui.dim(' off keeps the /model choice'),
165
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),
166
187
  ui.line(
167
188
  ...TABS.flatMap(([id, hotkey, label]) => [
168
189
  ui.Button({
@@ -182,10 +203,11 @@ function header(ui, config, view, tab, actions) {
182
203
 
183
204
  function nowTab(ui, config, view, usage, actions) {
184
205
  const metrics = usageMetrics(config, view, usage);
185
- const auto = view.mode === 'auto';
206
+ const unavailable = view.phase === 'unavailable';
207
+ const auto = view.mode === 'auto' && !unavailable;
186
208
  const current = view.selectedModel ?? view.nativeModel;
187
209
  const label = classifierLabel(config);
188
- const missing = missingCredentials(config, view, config.classifier);
210
+ const missing = unavailable ? null : missingCredentials(config, view, config.classifier);
189
211
  const served = view.actualModel && current && !isSameModel(current, view.actualModel);
190
212
  const reading = config.activityRouting === 'off' ? null : activityReading(view);
191
213
  const route = activityRouteText(config, view);
@@ -249,7 +271,13 @@ function nowTab(ui, config, view, usage, actions) {
249
271
  );
250
272
  }
251
273
  out.push(
252
- 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
+ ),
253
281
  );
254
282
  out.push(ui.text(' '), ...replies(ui, view));
255
283
  out.push(
@@ -274,8 +302,8 @@ function nowTab(ui, config, view, usage, actions) {
274
302
  function replies(ui, view) {
275
303
  const tiers = view.tiers ?? [];
276
304
  if (!tiers.length) return [ui.head('REPLIES'), ui.dim(' no replies yet')];
277
- const switches = switchCount(tiers);
278
305
  const activities = view.activities ?? [];
306
+ const switches = switchCount((view.routes ?? []).slice(-tiers.length));
279
307
  const letters = tiers.map((_, i) => activities[activities.length - tiers.length + i] ?? null);
280
308
  return [
281
309
  ui.head(`REPLIES · last ${tiers.length}`),
@@ -300,6 +328,13 @@ const POLICY_ROWS = [
300
328
  ['downgradeVotes', 'Votes to go down', String, 'agreeing turns before cheaper', 'policy.downgradeVotes'],
301
329
  ['horizon', 'Payback horizon', (v) => `${v} turns`, 'a switch repays its cache', 'policy.downgradeHorizonTurns'],
302
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
+ ],
303
338
  ];
304
339
 
305
340
  // What the Routing tab changed and has not saved: tiers, baseline, activity cells and mode against the saved config,
@@ -396,7 +431,7 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
396
431
  ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
397
432
  ui.dim('A new model ID needs router.json → models: price, window, efforts.'),
398
433
  ui.text(' '),
399
- ...activitySections(ui, config, changes, actions, modelOptions),
434
+ ...activitySections(ui, config, view, changes, actions, modelOptions),
400
435
  ui.text(' '),
401
436
  ui.line(ui.head('POLICY'), ui.dim(' · applies next turn · edit, then Save')),
402
437
  ...POLICY_ROWS.map(([key, label, format, hint]) =>
@@ -455,7 +490,7 @@ function overrideWarnings(config, uses, activity, tier) {
455
490
  }
456
491
 
457
492
  // ACTIVITIES, the effective matrix of the draft, and OVERRIDES, its editable cells. Both join the routing draft.
458
- function activitySections(ui, config, changes, actions, modelOptions) {
493
+ function activitySections(ui, config, view, changes, actions, modelOptions) {
459
494
  const draft = { ...config, ...changes.draft };
460
495
  const mode = draft.activityRouting;
461
496
  const cellRoutes = ACTIVITIES.flatMap((activity) => TIERS.map((tier) => resolveRoute(draft, tier, activity)));
@@ -474,19 +509,9 @@ function activitySections(ui, config, changes, actions, modelOptions) {
474
509
  }
475
510
  const distinct = uses.size;
476
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.
477
513
  const out = [
478
- ui.line(ui.cell(32, ui.head(`ACTIVITIES · routing ${mode}`)), ui.dim(TIERS.map((t) => t.padEnd(9)).join(''))),
479
- matrixRow(
480
- 'base',
481
- TIERS.map((tier) => shortRoute(draft, draft.routes[tier])),
482
- ),
483
- ...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
484
- ui.dim(
485
- ` ${distinct} distinct route${distinct === 1 ? '' : 's'} = ${distinct} cache${distinct === 1 ? '' : 's'}` +
486
- `${mode === 'on' ? '' : ' once activity routing is on'}`,
487
- ),
488
- ui.text(' '),
489
- 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')),
490
515
  ui.line(
491
516
  ui.cell(
492
517
  26,
@@ -503,6 +528,18 @@ function activitySections(ui, config, changes, actions, modelOptions) {
503
528
  marker(ui, changes.mode),
504
529
  ui.dim(ACTIVITY_MODE_HINT[mode]),
505
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)),
506
543
  ];
507
544
  const free = [];
508
545
  for (const activity of ACTIVITIES) {
@@ -537,6 +574,24 @@ function activitySections(ui, config, changes, actions, modelOptions) {
537
574
  return out;
538
575
  }
539
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
+
540
595
  function overrideRow(ui, draft, route, activity, tier, edited, actions, modelOptions, uses) {
541
596
  const model = draft.models[route.model];
542
597
  const aliases = modelOptions.includes(route.model) ? modelOptions : [route.model, ...modelOptions];
@@ -700,24 +755,60 @@ function statusBar(ui, config, view, actions) {
700
755
  return out;
701
756
  }
702
757
 
703
- // Session counts by activity, the switches by cause, the tool agreement and the shadow readout. Counts only: what the
704
- // would-route turns cost is not recorded, so no dollar figure is shown. Reset is always offered: the counts kept
705
- // across sessions may exist before this session has any.
758
+ // `on` would route N of M shadow turns differently, with the summed next-request range of the differing turns that had
759
+ // one, at configured list prices; `for K` says when only some had one.
760
+ function shadowLine(shadow) {
761
+ const counted = `Shadow on would route ${shadow.differs} of ${shadow.turns} turns differently`;
762
+ if (!(shadow.estimated > 0)) return counted;
763
+ const some = shadow.estimated < shadow.differs ? ` for ${shadow.estimated}` : '';
764
+ return `${counted} · est. ${difference(shadow.minUsd)} … ${difference(shadow.maxUsd)}${some} at list prices`;
765
+ }
766
+
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.
769
+ function storeUsage(ui, store) {
770
+ const summary = storeSummary(readStore(store));
771
+ const out = [ui.head('ACROSS SESSIONS · since the last reset')];
772
+ if (summary.labelled === 0) return [...out, ui.dim(' no turns recorded yet')];
773
+ out.push(ui.text(`Labelled ${summary.labelled} turns`));
774
+ const { matched, total } = summary.agreement;
775
+ if (total > 0)
776
+ out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
777
+ const { focus } = summary;
778
+ if (focus.total > 0)
779
+ out.push(
780
+ ui.text(
781
+ `Code/ops/explore classifier vs tools: ${focus.matched} of ${focus.total} turns (${percent(focus.matched / focus.total)})`,
782
+ ),
783
+ );
784
+ if (summary.disagreements.length)
785
+ out.push(
786
+ ui.text(`Mismatch ${summary.disagreements.map((d) => `${d.predicted} → ${d.observed} ${d.n}`).join(' · ')}`),
787
+ );
788
+ const { taken, refused } = summary.lateral;
789
+ if (taken + refused > 0) out.push(ui.text(`Activity moves ${taken} taken · ${refused} refused`));
790
+ if (summary.shadow.turns > 0) out.push(ui.text(shadowLine(summary.shadow)));
791
+ return out;
792
+ }
793
+
794
+ // Session counts by activity, the switches by cause, the tool agreement and the shadow readout, then the counts kept
795
+ // across sessions. Reset is always offered: the counts kept across sessions may exist before this session has any.
706
796
  function activityUsage(ui, config, view, actions) {
707
797
  const stats = view.activityStats ?? null;
708
798
  const mode = config.activityRouting;
709
- const reset = ui.Button({
710
- key: 'reset-activity-stats',
711
- label: 'Reset activity stats',
712
- onPress: actions.resetActivityStats,
713
- });
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
+ );
803
+ const across = [ui.text(' '), ...storeUsage(ui, view.activityStore)];
714
804
  if (mode === 'off' && !stats)
715
805
  return [
716
- ui.head('ACTIVITY · routing off'),
806
+ ui.head('ACTIVITY · activity routing off'),
717
807
  ui.dim(' Not asked. Set activity routing to shadow or on in the Routing tab.'),
808
+ ...(storeSummary(readStore(view.activityStore)).labelled > 0 ? across : []),
718
809
  reset,
719
810
  ];
720
- 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}`))];
721
812
  const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
722
813
  if (!rows.length) out.push(ui.dim(' no activity readings yet'));
723
814
  else {
@@ -742,14 +833,242 @@ function activityUsage(ui, config, view, actions) {
742
833
  const { matched, total } = stats.agreement;
743
834
  if (total > 0)
744
835
  out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
745
- if (mode === 'shadow' || stats.shadow.turns > 0)
746
- out.push(ui.text(`Shadow on would route ${stats.shadow.differs} of ${stats.shadow.turns} turns differently`));
836
+ if (mode === 'shadow' || stats.shadow.turns > 0) out.push(ui.text(shadowLine(stats.shadow)));
837
+ }
838
+ return [...out, ...across, reset];
839
+ }
840
+
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;
747
868
  }
748
- out.push(reset);
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)));
749
910
  return out;
750
911
  }
751
912
 
752
- function usageTab(ui, config, view, usage, actions) {
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) {
753
1072
  const metrics = usageMetrics(config, view, usage);
754
1073
  const history = view.history ?? [];
755
1074
  const tiers = view.tiers ?? [];
@@ -765,6 +1084,8 @@ function usageTab(ui, config, view, usage, actions) {
765
1084
  )
766
1085
  : ui.dim(' no replies yet');
767
1086
  return [
1087
+ ...savingsSection(ui, config, view, columns),
1088
+ ui.text(' '),
768
1089
  ui.head('USAGE · Claude readings'),
769
1090
  ui.line(`Cost ${dollars(metrics.cost)} `, ui.dim('reported by Claude')),
770
1091
  ui.gauge('Context ', metrics.contextBar, contextSuffix(metrics)),
@@ -805,7 +1126,8 @@ function helpLines(ui) {
805
1126
  ui.head('ABOUT THESE NUMBERS'),
806
1127
  ui.dim('Cost, context and cache are Claude readings. $ estimates use the'),
807
1128
  ui.dim('list prices in router.json; plan prices are equivalents, not cash.'),
808
- 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.'),
809
1131
  ui.dim('The context bar uses the routed model’s window, 20% in reserve.'),
810
1132
  ui.dim('A switch estimate prices 5m–1h cache writes; minus means cheaper.'),
811
1133
  ];