@alexeiled/claude-router 1.10.5 → 1.12.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,5 +1,5 @@
1
1
  import { compareRoutes } from './activity.mjs';
2
- import { latencyP95, mostlyOn, readMetrics, readStore, storeSummary } from './activity-stats.mjs';
2
+ import { latencyP95, readMetrics, readStore, storeSummary } from './activity-stats.mjs';
3
3
  import {
4
4
  ACTIVITIES,
5
5
  ACTIVITY_MODES,
@@ -34,9 +34,7 @@ import {
34
34
  routeName,
35
35
  savingsColor,
36
36
  signedUsd,
37
- sinceDate,
38
37
  sparkline,
39
- strongerReplies,
40
38
  switchCount,
41
39
  TIER_COLOR,
42
40
  unavailableReason,
@@ -46,16 +44,21 @@ import {
46
44
  yourModel,
47
45
  } from './display.mjs';
48
46
  import { cellForModel, isSameModel } from './route.mjs';
49
- import {
50
- EARLY_REPLIES,
51
- emptyTotals,
52
- modelAlias,
53
- modelEntry,
54
- modelSpec,
55
- readout,
56
- readSavingsStore,
57
- } from './savings.mjs';
47
+ import { modelSpec, readout } from './savings.mjs';
58
48
  import { PASS_REASONS, readSubagentStore } from './subagents.mjs';
49
+ import {
50
+ allTime,
51
+ breakdown,
52
+ cellsWhere,
53
+ INSIGHT_REPLIES,
54
+ insights,
55
+ mostlyOn,
56
+ PERIODS,
57
+ periodRange,
58
+ readUsage,
59
+ summarize,
60
+ trend,
61
+ } from './usage.mjs';
59
62
 
60
63
  // Routes and Policy edit one draft and save together; Classifier saves each pick at once.
61
64
  const TABS = [
@@ -121,7 +124,6 @@ const hostOf = (endpoint) => urlOf(endpoint)?.host ?? endpoint.replace(/^https?:
121
124
  const LOCAL_HOSTS = ['127.0.0.1', 'localhost', '[::1]'];
122
125
  const isLocal = (endpoint) => LOCAL_HOSTS.includes(urlOf(endpoint)?.hostname);
123
126
  const windowSize = (tokens) => (tokens >= 1e6 ? `${tokens / 1e6}M` : `${Math.round(tokens / 1e3)}K`);
124
- const share = (value) => (value === null ? '—' : `${value}%`);
125
127
  const plural = (n, word, many = `${word}s`) => `${n} ${n === 1 ? word : many}`;
126
128
  const contextSuffix = (metrics) =>
127
129
  Number.isFinite(metrics.nextContext)
@@ -161,7 +163,7 @@ export function renderPanel(
161
163
  view,
162
164
  usage,
163
165
  actions,
164
- { modelOptions = Object.keys(config.models), columns = null } = {},
166
+ { modelOptions = Object.keys(config.models), columns = null, now = Date.now() } = {},
165
167
  ) {
166
168
  const ui = kit(elements);
167
169
  const tab = TABS.some(([id]) => id === view.tab) ? view.tab : 'now';
@@ -170,7 +172,7 @@ export function renderPanel(
170
172
  flexDirection: 'column',
171
173
  children: [
172
174
  ...header(ui, config, view, tab, actions),
173
- ...body(ui, config, view, usage, actions, modelOptions, columns),
175
+ ...body(ui, config, view, usage, actions, modelOptions, columns, now),
174
176
  ...(view.help ? helpLines(ui) : []),
175
177
  ...statusBar(ui, config, view, tab, actions),
176
178
  ui.Button({ key: 'close', label: 'Close', hotkey: 'q', role: 'dismiss', onPress: actions.close }),
@@ -345,12 +347,63 @@ function nowTab(ui, config, view, usage, actions) {
345
347
  : ui.text(`${metrics.cacheBar.percent}% reused on the last reply`, { color: metrics.cacheBar.color }),
346
348
  ui.dim(' · '),
347
349
  `cost ${dollars(metrics.cost)} `,
348
- ui.dim('by Claude'),
350
+ ui.dim('by Claude '),
351
+ detailButton(ui, view, actions),
349
352
  ),
353
+ ...(view.usageDetail ? readingDetail(ui, view, metrics) : []),
350
354
  );
351
355
  return out;
352
356
  }
353
357
 
358
+ const detailButton = (ui, view, actions) =>
359
+ ui.Button({
360
+ key: 'usage-detail',
361
+ label: view.usageDetail ? 'less' : 'details',
362
+ variant: view.usageDetail ? 'primary' : 'secondary',
363
+ onPress: actions.usageDetail,
364
+ });
365
+
366
+ // Behind `details` on the Now tab: Claude's readings of the last reply, the input per reply, and the estimates the
367
+ // last decision weighed.
368
+ function readingDetail(ui, view, metrics) {
369
+ const history = view.history ?? [];
370
+ const tiers = view.tiers ?? [];
371
+ const glyphs = sparkline(history);
372
+ const trend = history.length
373
+ ? ui.line(
374
+ ' ',
375
+ ...[...glyphs].map((glyph, i) => {
376
+ const tier = tiers[tiers.length - history.length + i];
377
+ return tier ? ui.tier(tier, glyph) : ui.text(glyph);
378
+ }),
379
+ ui.dim(` ${formatTokens(Math.min(...history))} … ${formatTokens(Math.max(...history))}`),
380
+ )
381
+ : ui.dim(' no replies yet');
382
+ return [
383
+ ui.dim(` context observed ${formatTokens(metrics.observed)} · estimated ${formatTokens(view.contextTokens)}`),
384
+ ui.dim(` cache read ${formatTokens(view.cacheRead)} · written ${formatTokens(view.cacheWrite)} tokens`),
385
+ ui.dim(` output ${formatTokens(view.outputTokens)} tokens on the last reply`),
386
+ ui.text(' '),
387
+ ui.head('INPUT PER REPLY · lowest to highest'),
388
+ trend,
389
+ ui.text(' '),
390
+ ui.head('ESTIMATES · configured list prices'),
391
+ ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
392
+ Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
393
+ ...(view.comparison
394
+ ? [
395
+ ui.text(`Compared tiers ${view.comparison.incumbent} → ${view.comparison.candidate}`),
396
+ ui.text(
397
+ `Next-turn difference ${difference(view.comparison.minUsd)} to ${difference(view.comparison.maxUsd)}`,
398
+ ),
399
+ ui.text(
400
+ `Payback ${view.comparison.paybackTurns === null ? 'none projected' : `${view.comparison.paybackTurns} later turns`}`,
401
+ ),
402
+ ]
403
+ : []),
404
+ ];
405
+ }
406
+
354
407
  const verbShare = (text) => text.replace(/^(\S+)/, (name) => activityVerb(name));
355
408
 
356
409
  // The last replies, one cell each colored by the tier that served it, a dot where routing did not choose; the switch
@@ -892,12 +945,24 @@ function classifierTab(ui, config, view, _usage, actions, _modelOptions, columns
892
945
  ui.Button({ key: 'key', label: 'Edit', hotkey: 'k', onPress: actions.key }),
893
946
  ui.dim(' opens /plugin configure'),
894
947
  ),
948
+ ...sessionAgreement(ui, view),
895
949
  ...(storeSummary(readStore(view.activityStore)).labelled > 0
896
950
  ? [ui.text(' '), ...storeUsage(ui, view.activityStore, view.activityMetrics)]
897
951
  : []),
898
952
  ];
899
953
  }
900
954
 
955
+ // How often this session's tools fit the classifier's accepted activity; none before a labelled turn.
956
+ function sessionAgreement(ui, view) {
957
+ const { matched = 0, total = 0 } = view.activityStats?.agreement ?? {};
958
+ if (!total) return [];
959
+ return [
960
+ ui.text(' '),
961
+ ui.head('THIS SESSION'),
962
+ ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`),
963
+ ];
964
+ }
965
+
901
966
  // Green for a write or its undo, red for a refused write, yellow for everything else that needs a look.
902
967
  const noticeColor = (notice) =>
903
968
  /^Not saved/.test(notice) ? 'red' : /^(Saved|Undid|Path copied)/.test(notice) ? 'green' : 'yellow';
@@ -992,81 +1057,6 @@ function storeUsage(ui, store, metrics) {
992
1057
  return out;
993
1058
  }
994
1059
 
995
- // The ACTIVITY table's columns before the cost, and the cost column.
996
- const ACTIVITY_TABLE = 48;
997
- const ACTIVITY_COST = 11;
998
-
999
- // Session counts by activity: turns, requests, share, the estimated list price of their replies and the route most of
1000
- // them ran on; the switches by cause and the tool agreement. The counts kept across sessions are on
1001
- // the Classifier tab.
1002
- function activityUsage(ui, config, view, columns) {
1003
- const stats = view.activityStats ?? null;
1004
- const mode = config.activityRouting;
1005
- if (mode === 'off' && !stats)
1006
- return [
1007
- ui.head('BY ACTIVITY · activity routing off'),
1008
- ui.dim(' Not asked. Set activity routing to on in the Routes tab.'),
1009
- ];
1010
- const out = [ui.line(ui.head('BY ACTIVITY · this session'), ui.dim(` activity routing ${mode}`))];
1011
- const rows = Object.entries(stats?.byActivity ?? {}).filter(([, counts]) => counts?.turns > 0);
1012
- if (!rows.length) out.push(ui.dim(' no activity readings yet'));
1013
- else {
1014
- const total = rows.reduce((sum, [, counts]) => sum + counts.turns, 0);
1015
- // A partial sum would read as the whole: a cost shows only when every reply had a price.
1016
- const cost = (counts) =>
1017
- Number.isFinite(counts.routedUsd) && counts.pricedRequests === counts.requests ? usd(counts.routedUsd) : '—';
1018
- const route = (counts) => {
1019
- const key = mostlyOn(counts);
1020
- if (!key) return '—';
1021
- const at = key.lastIndexOf('@');
1022
- const effort = key.slice(at + 1);
1023
- return routeLabel(key.slice(0, at), effort === 'session' ? null : effort);
1024
- };
1025
- // A narrow pane drops "mostly on", then the cost, before any column it had before them.
1026
- const widest = Math.max('mostly on'.length, ...rows.map(([, counts]) => route(counts).length));
1027
- const fits = (width) => !Number.isFinite(columns) || width <= columns;
1028
- const showCost = fits(ACTIVITY_TABLE + ACTIVITY_COST);
1029
- const showRoute = showCost && fits(ACTIVITY_TABLE + ACTIVITY_COST + 3 + widest);
1030
- out.push(
1031
- ui.dim(
1032
- ` ${''.padEnd(12)}${''.padEnd(10)}${'turns'.padStart(7)}${'requests'.padStart(10)}${'share'.padStart(7)}` +
1033
- `${showCost ? 'est. cost'.padStart(ACTIVITY_COST) : ''}${showRoute ? ' mostly on' : ''}`,
1034
- ),
1035
- );
1036
- for (const [name, counts] of rows.sort((a, b) => b[1].turns - a[1].turns)) {
1037
- const share = counts.turns / total;
1038
- out.push(
1039
- ui.line(
1040
- ' ',
1041
- ui.cell(12, ui.activity(name)),
1042
- ui.text(bar(share, 1, 10).text, { color: 'cyan' }),
1043
- `${String(counts.turns).padStart(7)}${String(counts.requests).padStart(10)}${percent(share).padStart(7)}` +
1044
- `${showCost ? cost(counts).padStart(ACTIVITY_COST) : ''}${showRoute ? ` ${route(counts)}` : ''}`,
1045
- ),
1046
- );
1047
- }
1048
- }
1049
- if (stats) {
1050
- const { tier, activity } = stats.switches;
1051
- // Refused moves change no route.
1052
- const refused = stats.lateral?.refused ?? 0;
1053
- out.push(
1054
- ui.text(
1055
- `Switches ${tier + activity} · ${tier} by tier · ${activity} by activity${refused ? ` · ${refused} refused` : ''}`,
1056
- ),
1057
- );
1058
- const { matched, total } = stats.agreement;
1059
- if (total > 0)
1060
- out.push(ui.text(`Agreement classifier vs tools: ${matched} of ${total} turns (${percent(matched / total)})`));
1061
- }
1062
- return out;
1063
- }
1064
-
1065
- // Bars are this wide at the larger of routed and your model; the section drops them before any number when the pane is
1066
- // narrower than the table.
1067
- const SAVINGS_BAR = 22;
1068
- const SAVINGS_COLUMN = 15;
1069
- const SAVINGS_PARTS = [' cheaper models', ' stronger than yours', ' cache writes from switches'];
1070
1060
  // The footer as [text, joiner] pieces, packed to the pane: two lines at full width, more in a narrow pane, never a
1071
1061
  // line that ends in a joiner.
1072
1062
  const SAVINGS_FOOTER = [
@@ -1105,112 +1095,6 @@ const dimLines = (ui, text, columns) => {
1105
1095
  const footer = (ui, columns) =>
1106
1096
  pack(SAVINGS_FOOTER, Math.min(FULL_WIDTH, columns ?? FULL_WIDTH), ' ').map((line) => ui.dim(line));
1107
1097
 
1108
- // One readout cell of the Difference row: "too early" below EARLY_REPLIES replies, else the signed amount and share.
1109
- function differenceCell(ui, r) {
1110
- if (!r) return '';
1111
- if (r.early) return ui.dim('too early');
1112
- const share = differenceShare(r);
1113
- return ui.text(`${signedUsd(r.differenceUsd)}${share ? ` ${share}` : ''}`, {
1114
- bold: true,
1115
- color: savingsColor(r.differenceUsd),
1116
- });
1117
- }
1118
-
1119
- const partCell = (ui, value) => (value === null ? '' : ui.text(signedUsd(value), { color: savingsColor(value) }));
1120
-
1121
- // A bar of `parts` ([usd, color]) at `scale` cells per dollar, filled to SAVINGS_BAR with dim cells. Each part ends at
1122
- // its rounded running total, so the parts add up to the bar.
1123
- function moneyBar(ui, parts, scale) {
1124
- let at = 0;
1125
- let sum = 0;
1126
- const out = [];
1127
- for (const [value, color] of parts) {
1128
- sum += value;
1129
- const end = Math.round(sum * scale);
1130
- if (end > at) out.push(ui.text('█'.repeat(end - at), color ? { color } : {}));
1131
- at = Math.max(at, end);
1132
- }
1133
- if (at < SAVINGS_BAR) out.push(ui.dim('░'.repeat(SAVINGS_BAR - at)));
1134
- return out;
1135
- }
1136
-
1137
- // The parts of a session readout past "too early": each part, the stronger replies, the two costs as bars (not in a
1138
- // narrow pane), the replies by tier and why routing spent more.
1139
- function savingsDetail(ui, view, { totals, session, across, row, tier, narrow, columns }) {
1140
- const out = [];
1141
- const savedPart = (key) => (across && !across.early ? partCell(ui, across[key]) : null);
1142
- const stronger = strongerReplies(totals);
1143
- out.push(
1144
- row(ui.dim(SAVINGS_PARTS[0]), partCell(ui, session.cheaperUsd), savedPart('cheaperUsd')),
1145
- row(ui.dim(SAVINGS_PARTS[1]), partCell(ui, session.strongerUsd), savedPart('strongerUsd')),
1146
- ...(stronger.count
1147
- ? dimLines(
1148
- ui,
1149
- ` ${stronger.count} repl${stronger.count === 1 ? 'y' : 'ies'} on ${stronger.several ? 'stronger models' : modelName(stronger.model)}`,
1150
- columns,
1151
- )
1152
- : []),
1153
- row(ui.dim(SAVINGS_PARTS[2]), partCell(ui, session.switchUsd), savedPart('switchUsd')),
1154
- ui.text(' '),
1155
- );
1156
- const largest = Math.max(totals.routedUsd, totals.yoursUsd);
1157
- if (!narrow && largest > 0) {
1158
- const scale = SAVINGS_BAR / largest;
1159
- const tiers = [...TIERS, 'none'];
1160
- const routedParts = tiers.map((t) => [totals.tierUsd[t] ?? 0, TIER_COLOR[t]]);
1161
- out.push(
1162
- ui.line(ui.dim(' routed '), ...moneyBar(ui, routedParts, scale), ` ${usd(session.routedUsd)}`),
1163
- ui.line(
1164
- ui.dim(' your model '),
1165
- ...moneyBar(ui, [[totals.yoursUsd, TIER_COLOR[tier]]], scale),
1166
- ` ${usd(session.yoursUsd)}`,
1167
- ),
1168
- );
1169
- }
1170
- const shares = [...TIERS, 'none'].filter((t) => totals.tiers[t] > 0);
1171
- out.push(
1172
- ui.line(
1173
- ' ',
1174
- ...shares.flatMap((t, i) => [
1175
- t === 'none' ? ui.dim('■ ') : ui.tier(t, '■ '),
1176
- ui.dim(
1177
- `${t === 'none' ? 'not routed' : t} ${percent(totals.tiers[t] / totals.replies)}${i === shares.length - 1 ? ' of replies' : ' '}`,
1178
- ),
1179
- ]),
1180
- ),
1181
- );
1182
- if (session.differenceUsd > 0 && stronger.count && session.strongerUsd >= session.switchUsd) {
1183
- const models = stronger.several ? 'stronger models' : modelName(stronger.model);
1184
- out.push(
1185
- ...dimLines(
1186
- ui,
1187
- ` Routing spent more than ${modelName(view.nativeModel)} alone because upper tiers ran on ${models}.`,
1188
- columns,
1189
- ),
1190
- ...dimLines(ui, ' To spend less, pick cheaper models for those tiers in the Routes tab.', columns),
1191
- );
1192
- }
1193
- return out;
1194
- }
1195
-
1196
- // This session against each other priced model as yours, those past "too early", in router.json order:
1197
- // "Opus 5.5 saved $6.20 · Haiku 5.5 extra $1.10". Null when there is none.
1198
- function otherModels(config, view) {
1199
- const current = modelAlias(config, view.nativeModel);
1200
- const parts = Object.keys(config.models)
1201
- .filter((alias) => alias !== current && modelEntry(view.savingsBy, alias))
1202
- .map((alias) => [modelName(config.models[alias].id), readout(modelEntry(view.savingsBy, alias))])
1203
- .filter(([, r]) => !r.early)
1204
- .map(([name, r]) =>
1205
- r.differenceUsd < 0
1206
- ? `${name} saved ${usd(r.differenceUsd)}`
1207
- : r.differenceUsd > 0
1208
- ? `${name} extra ${usd(r.differenceUsd)}`
1209
- : `${name} same cost`,
1210
- );
1211
- return parts.length ? parts.join(' · ') : null;
1212
- }
1213
-
1214
1098
  // The tier whose route is your model at your effort, for its color; else any tier on your model.
1215
1099
  function yourTier(config, view) {
1216
1100
  const runs = (tier) => {
@@ -1223,107 +1107,6 @@ function yourTier(config, view) {
1223
1107
  return TIERS.find(runs) ?? cellForModel(config, view.nativeModel)?.tier ?? null;
1224
1108
  }
1225
1109
 
1226
- // ROUTING VS YOUR MODEL: what routed replies cost at list prices against the same tokens on the session's own model
1227
- // and effort, this session and since the last reset, split into its three parts; then the two costs as bars and the
1228
- // replies by tier. Below EARLY_REPLIES replies a column reads "too early"; with routing off or unavailable and no
1229
- // routed reply, one sentence says so.
1230
- function savingsSection(ui, config, view, columns) {
1231
- const name = yourModel(view);
1232
- const tier = yourTier(config, view);
1233
- const title = [ui.head('ROUTING VS YOUR MODEL'), ui.dim(' · '), tier ? ui.tier(tier, name) : ui.text(name)];
1234
- if (!modelSpec(config, view.nativeModel))
1235
- return [ui.line(...title), ui.dim(` ${modelName(view.nativeModel)} has no list price in router.json models.`)];
1236
- const totals = view.savings ?? emptyTotals();
1237
- const session = readout(totals);
1238
- const store = readSavingsStore(view.savingsStore);
1239
- let across = store.replies ? readout(store) : null;
1240
- const same = 'Same tokens on your model';
1241
- const unavailable = view.phase === 'unavailable';
1242
- const quiet = (unavailable || view.mode === 'manual') && !session.replies;
1243
- // The label column fits the longest label shown, two spaces before the amounts.
1244
- const labels = [` ${same}`, ...(quiet || session.early ? [] : SAVINGS_PARTS)];
1245
- const widest = Math.max(...labels.map((text) => text.length)) + 2;
1246
- const titleWidth = 'ROUTING VS YOUR MODEL · '.length + name.length;
1247
- // The last column needs only its widest value, "−$11.42 −22%".
1248
- const table = (label) => label + SAVINGS_COLUMN + (across ? 13 : 0);
1249
- // The column heads share the title's line when it fits; a narrow pane puts them on their own and drops the bars,
1250
- // and a narrower one the saved column too.
1251
- let label = Math.max(titleWidth + 3, widest);
1252
- const narrow = Number.isFinite(columns) && table(label) > columns;
1253
- if (narrow) label = widest;
1254
- if (narrow && table(label) > columns) across = null;
1255
- const heads = [
1256
- ui.cell(SAVINGS_COLUMN, ui.dim('this session')),
1257
- across ? ui.dim(`since ${sinceDate(store.since)}`) : null,
1258
- ];
1259
- const row = (name, mine, saved = null) =>
1260
- ui.line(ui.cell(label, name), ui.cell(SAVINGS_COLUMN, mine), across && saved !== null ? saved : null);
1261
- // The saved column adds up sessions that each had their own model.
1262
- const notes = () => [
1263
- ui.text(' '),
1264
- ...(across
1265
- ? dimLines(ui, ` since ${sinceDate(store.since)}: each session compared with its own model`, columns)
1266
- : []),
1267
- ...footer(ui, columns),
1268
- ];
1269
- const out = narrow
1270
- ? [ui.line(...title), ui.line(ui.cell(label, ''), ...heads)]
1271
- : [ui.line(ui.cell(label, ui.line(...title)), ...heads)];
1272
- if (quiet) {
1273
- out.push(
1274
- ...dimLines(
1275
- ui,
1276
- ` Routing is ${unavailable ? 'unavailable' : 'off'}: every reply this session used your model.`,
1277
- columns,
1278
- ),
1279
- row(' Difference', '$0.00', differenceCell(ui, across)),
1280
- );
1281
- return [...out, ...notes()];
1282
- }
1283
- out.push(
1284
- row(' Routed replies', usd(session.routedUsd), across ? usd(across.routedUsd) : null),
1285
- row(` ${same}`, usd(session.yoursUsd), across ? usd(across.yoursUsd) : null),
1286
- row(ui.text(' Difference', { bold: true }), differenceCell(ui, session), differenceCell(ui, across)),
1287
- );
1288
- if (session.early) {
1289
- out.push(
1290
- ...dimLines(
1291
- ui,
1292
- ` ${session.replies} routed repl${session.replies === 1 ? 'y' : 'ies'} so far; the session figure appears after ${EARLY_REPLIES}`,
1293
- columns,
1294
- ),
1295
- );
1296
- return [...out, ...notes()];
1297
- }
1298
- const others = otherModels(config, view);
1299
- if (others) out.push(...dimLines(ui, ` vs other models: ${others}`, columns));
1300
- out.push(...savingsDetail(ui, view, { totals, session, across, row, tier, narrow, columns }));
1301
- return [...out, ...notes()];
1302
- }
1303
-
1304
- // The answer before the table: "Routing saved $1.84 (22%) this session vs Opus 5.5 · xhigh", or what it cost more.
1305
- // None before EARLY_REPLIES routed replies, or while the section says every reply used your model.
1306
- function savingsHeadline(ui, config, view) {
1307
- if (!modelSpec(config, view.nativeModel)) return [];
1308
- const session = readout(view.savings ?? emptyTotals());
1309
- if (session.early || !session.replies) return [];
1310
- const tier = yourTier(config, view);
1311
- const name = yourModel(view);
1312
- const share = differenceShare(session)?.replace(/^[−+]/, '');
1313
- const amount = `${usd(session.differenceUsd)}${share ? ` (${share})` : ''}`;
1314
- const verb = session.differenceUsd < 0 ? 'saved' : session.differenceUsd > 0 ? 'cost' : 'cost the same';
1315
- return [
1316
- ui.line(
1317
- `Routing ${verb} `,
1318
- session.differenceUsd === 0 ? '' : ui.text(amount, { bold: true, color: savingsColor(session.differenceUsd) }),
1319
- session.differenceUsd > 0 ? ' more' : '',
1320
- session.differenceUsd > 0 ? ' this session than ' : ' this session vs ',
1321
- tier ? ui.tier(tier, name) : ui.text(name),
1322
- ),
1323
- ui.text(' '),
1324
- ];
1325
- }
1326
-
1327
1110
  // Prices summed over the priced requests, and how many were not priced (a model without a price in router.json).
1328
1111
  const pricedText = (text, counts) =>
1329
1112
  counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
@@ -1367,93 +1150,459 @@ function subagentUsage(ui, config, view, columns) {
1367
1150
  return out;
1368
1151
  }
1369
1152
 
1370
- // Answer first: routing vs your model, then plan quota, then this session by activity. Claude's own readings fold to
1371
- // one line, with the detail and the estimates behind `details`.
1372
- function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
1373
- const metrics = usageMetrics(config, view, usage);
1374
- const history = view.history ?? [];
1375
- const tiers = view.tiers ?? [];
1376
- const glyphs = sparkline(history);
1377
- const quota = (usage?.rateLimits ?? []).filter((limit) => Number.isFinite(limit.percentUsed));
1378
- const trend = history.length
1379
- ? ui.line(
1380
- ' ',
1381
- ...[...glyphs].map((glyph, i) => {
1382
- const tier = tiers[tiers.length - history.length + i];
1383
- return tier ? ui.tier(tier, glyph) : ui.text(glyph);
1384
- }),
1385
- ui.dim(` ${formatTokens(Math.min(...history))} … ${formatTokens(Math.max(...history))}`),
1386
- )
1387
- : ui.dim(' no replies yet');
1388
- const detail = view.usageDetail
1389
- ? [
1390
- ui.dim(` context observed ${formatTokens(metrics.observed)} · estimated ${formatTokens(view.contextTokens)}`),
1391
- ui.dim(` cache read ${formatTokens(view.cacheRead)} · written ${formatTokens(view.cacheWrite)} tokens`),
1392
- ui.dim(` output ${formatTokens(view.outputTokens)} tokens on the last reply`),
1393
- ui.text(' '),
1394
- ui.head('INPUT PER REPLY · lowest to highest'),
1395
- trend,
1396
- ui.text(' '),
1397
- ui.head('ESTIMATES · configured list prices'),
1398
- ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
1399
- Number.isFinite(view.estimate?.taxUsd)
1400
- ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`)
1401
- : null,
1402
- ...(view.comparison
1403
- ? [
1404
- ui.text(`Compared tiers ${view.comparison.incumbent} → ${view.comparison.candidate}`),
1405
- ui.text(
1406
- `Next-turn difference ${difference(view.comparison.minUsd)} to ${difference(view.comparison.maxUsd)}`,
1407
- ),
1408
- ui.text(
1409
- `Payback ${view.comparison.paybackTurns === null ? 'none projected' : `${view.comparison.paybackTurns} later turns`}`,
1410
- ),
1411
- ]
1412
- : []),
1413
- ]
1414
- : [];
1153
+ const PERIOD_LABEL = { session: 'Session', today: 'Today', '7d': '7d', '30d': '30d', month: 'Month', all: 'All' };
1154
+ // "No routed replies …" for each period.
1155
+ const PERIOD_PHRASE = {
1156
+ session: 'this session',
1157
+ today: 'today',
1158
+ '7d': 'in the last 7 days',
1159
+ '30d': 'in the last 30 days',
1160
+ month: 'this month',
1161
+ all: 'yet',
1162
+ };
1163
+ const GROUPS = [
1164
+ ['activity', 'Activity'],
1165
+ ['model', 'Model'],
1166
+ ['tier', 'Tier'],
1167
+ ];
1168
+ // What an open row lists: the routes of an activity, the activities of a model or a tier.
1169
+ const CHILD_BY = { activity: 'route', model: 'activity', tier: 'activity' };
1170
+ // The ledger's label column after its sign, and its amount column.
1171
+ const LEDGER_LABEL = 20;
1172
+ const LEDGER_AMOUNT = 9;
1173
+ // The breakdown: the open mark, the name, replies, cost and vs yours; "mostly on" or the share only where it fits.
1174
+ const NAME = 20;
1175
+ const BREAKDOWN = 4 + NAME + 8 + 10 + 11;
1176
+ const TAIL = 23;
1177
+ // Bars at full scale: the routed share of your model, and the costliest row of What if.
1178
+ const BAR = 22;
1179
+ const SPARK = '▁▂▃▄▅▆▇█';
1180
+
1181
+ const dayLabel = (key) => {
1182
+ const [y, m, d] = key.split('-').map(Number);
1183
+ return new Date(y, m - 1, d).toLocaleDateString('en-US', { month: 'short', day: 'numeric' });
1184
+ };
1185
+ // "Opus 5.5 · high" for a cell's `id@effort`.
1186
+ function routeText(route) {
1187
+ const at = route.lastIndexOf('@');
1188
+ const effort = route.slice(at + 1);
1189
+ return routeLabel(route.slice(0, at), effort === 'session' ? null : effort);
1190
+ }
1191
+ // A cents difference, so a row never reads "+$0.00" for a fraction of a cent.
1192
+ const cents = (usd) => Math.round(usd * 100) / 100;
1193
+ const differenceText = (ui, usdValue, width) => {
1194
+ const value = cents(usdValue);
1195
+ return ui.text(signedUsd(value).padStart(width), savingsColor(value) ? { color: savingsColor(value) } : {});
1196
+ };
1197
+
1198
+ // A breakdown row's name: an activity as its verb, a tier in its color, a model or a route by name.
1199
+ function groupName(ui, by, name) {
1200
+ if (name === 'none') return ui.dim(by === 'activity' ? 'no activity' : 'not routed');
1201
+ if (by === 'activity') return ui.activity(name);
1202
+ if (by === 'tier') return ui.tier(name);
1203
+ return ui.text(by === 'model' ? modelName(name) : routeText(name));
1204
+ }
1205
+
1206
+ // The period buttons; `p` presses the one after the shown period, so it steps through them.
1207
+ function periodRow(ui, period, actions) {
1208
+ const next = PERIODS[(PERIODS.indexOf(period) + 1) % PERIODS.length];
1209
+ return ui.line(
1210
+ ...PERIODS.flatMap((id) => [
1211
+ ui.Button({
1212
+ key: `period-${id}`,
1213
+ label: PERIOD_LABEL[id],
1214
+ ...(id === next ? { hotkey: 'p' } : {}),
1215
+ variant: id === period ? 'primary' : 'secondary',
1216
+ onPress: () => actions.period(id),
1217
+ }),
1218
+ ' ',
1219
+ ]),
1220
+ ui.dim(' p next'),
1221
+ );
1222
+ }
1223
+
1224
+ // Whose replies the period holds and what each is compared with.
1225
+ function scopeLine(ui, config, view, period, summary, now) {
1226
+ if (period === 'session') {
1227
+ const recorded = recordedModels(config, view, summary);
1228
+ if (recorded)
1229
+ return ui.dim(
1230
+ Object.keys(summary.yours).length > 1
1231
+ ? `this session · each reply vs the model /model selected: ${recorded}`
1232
+ : `this session · your model ${recorded}`,
1233
+ );
1234
+ const tier = yourTier(config, view);
1235
+ return ui.line(
1236
+ ui.dim('this session · your model '),
1237
+ tier ? ui.tier(tier, yourModel(view)) : ui.text(yourModel(view)),
1238
+ );
1239
+ }
1240
+ const { from, to } = periodRange(period, now);
1241
+ const first = summary.days[0]?.[0];
1242
+ const span =
1243
+ period === 'today'
1244
+ ? `today, ${dayLabel(to)}`
1245
+ : from
1246
+ ? `${dayLabel(from)} – ${dayLabel(to)}`
1247
+ : first
1248
+ ? `since ${dayLabel(first)}`
1249
+ : 'no dated replies yet';
1250
+ return ui.dim(`all sessions · ${span} · each session vs its own model`);
1251
+ }
1252
+
1253
+ // The models a session's replies were priced against, when they are not the one `/model` selects now: after a
1254
+ // `/model` change the record keeps each reply against the model selected when it ran. Null when they are.
1255
+ function recordedModels(config, view, summary) {
1256
+ const ids = Object.keys(summary.yours);
1257
+ if (!ids.length || (ids.length === 1 && ids[0] === modelSpec(config, view.nativeModel)?.id)) return null;
1258
+ return ids.map(modelName).join(', ');
1259
+ }
1260
+
1261
+ // The answer first: "Routing saved $1.84 (22%) vs your model", what it cost more, or why there is nothing to compare.
1262
+ function headline(ui, config, view, period, r) {
1263
+ if (!r.replies) {
1264
+ // A reply is priced against your model, so on a model without a price none is counted.
1265
+ if (period === 'session' && !modelSpec(config, view.nativeModel))
1266
+ return [ui.dim(`${modelName(view.nativeModel)} has no list price in router.json models.`)];
1267
+ const off = period === 'session' && (view.phase === 'unavailable' || view.mode === 'manual');
1268
+ return [
1269
+ ui.dim(
1270
+ off
1271
+ ? `Routing is ${view.phase === 'unavailable' ? 'unavailable' : 'off'}: replies use your model and are not counted.`
1272
+ : `No routed replies ${PERIOD_PHRASE[period]}.`,
1273
+ ),
1274
+ ];
1275
+ }
1276
+ const share = differenceShare(r)?.replace(/^[−+]/, '');
1277
+ const amount = `${usd(r.differenceUsd)}${share ? ` (${share})` : ''}`;
1278
+ const color = savingsColor(r.differenceUsd);
1415
1279
  return [
1416
- ...savingsHeadline(ui, config, view),
1417
- ...savingsSection(ui, config, view, columns),
1418
- ...(quota.length
1419
- ? [
1420
- ui.text(' '),
1421
- ui.head('PLAN QUOTA'),
1422
- ...quota.map((limit) =>
1423
- ui.gauge(`${limit.kind.replaceAll('_', ' ').padEnd(11)}`, bar(limit.percentUsed, 100), 'used'),
1424
- ),
1280
+ ui.line(
1281
+ r.differenceUsd < 0 ? 'Routing saved ' : r.differenceUsd > 0 ? 'Routing cost ' : 'Routing cost the same as',
1282
+ r.differenceUsd === 0 ? '' : ui.text(amount, { bold: true, color }),
1283
+ r.differenceUsd < 0 ? ' vs your model' : r.differenceUsd > 0 ? ' more than your model' : ' your model',
1284
+ r.early ? ui.text(` early sample · ${plural(r.replies, 'reply', 'replies')}`, { color: 'yellow' }) : null,
1285
+ ),
1286
+ ];
1287
+ }
1288
+
1289
+ // Your model − cheaper models + stronger models + switch cache writes = routed, in whole cents that add up (readout).
1290
+ function ledger(ui, config, view, { period, r, summary, legacy, columns }) {
1291
+ const tier = yourTier(config, view);
1292
+ const row = (sign, label, value, color, note = null, bold = false) =>
1293
+ ui.line(
1294
+ ui.dim(` ${sign} `),
1295
+ ui.cell(LEDGER_LABEL, ui.text(label, bold ? { bold: true } : {})),
1296
+ ui.text(usd(value).padStart(LEDGER_AMOUNT), { ...(color ? { color } : {}), ...(bold ? { bold: true } : {}) }),
1297
+ note ? ' ' : null,
1298
+ note,
1299
+ );
1300
+ // The undated totals kept no switch count, so All with them names none.
1301
+ const switches = legacy?.replies ? null : summary.switches.tier + summary.switches.activity;
1302
+ const recorded = period === 'session' ? recordedModels(config, view, summary) : null;
1303
+ const saving = -r.cheaperUsd;
1304
+ const ratio = r.yoursUsd > 0 ? r.routedUsd / r.yoursUsd : null;
1305
+ const wide = !Number.isFinite(columns) || columns >= 4 + LEDGER_LABEL + LEDGER_AMOUNT + 3 + BAR + 16;
1306
+ const filled = ratio === null ? 0 : Math.max(0, Math.min(BAR, Math.round(ratio * BAR)));
1307
+ const out = [
1308
+ row(
1309
+ ' ',
1310
+ 'Your model',
1311
+ r.yoursUsd,
1312
+ undefined,
1313
+ period !== 'session'
1314
+ ? ui.dim('each session’s own model')
1315
+ : recorded
1316
+ ? ui.dim(recorded)
1317
+ : tier
1318
+ ? ui.tier(tier, yourModel(view))
1319
+ : ui.text(yourModel(view)),
1320
+ ),
1321
+ row('−', 'Cheaper models', r.cheaperUsd, savingsColor(r.cheaperUsd)),
1322
+ row('+', 'Stronger models', r.strongerUsd, savingsColor(r.strongerUsd)),
1323
+ row(
1324
+ '+',
1325
+ 'Switch cache writes',
1326
+ r.switchUsd,
1327
+ savingsColor(r.switchUsd),
1328
+ ui.dim(
1329
+ [
1330
+ switches === null ? null : plural(switches, 'route switch', 'route switches'),
1331
+ saving > 0 && r.switchUsd > 0 ? `${percent(r.switchUsd / saving)} of the saving` : null,
1425
1332
  ]
1426
- : []),
1427
- ui.text(' '),
1428
- ...activityUsage(ui, config, view, columns),
1429
- ui.text(' '),
1430
- ...subagentUsage(ui, config, view, columns),
1431
- ui.text(' '),
1333
+ .filter(Boolean)
1334
+ .join(' · '),
1335
+ ),
1336
+ ),
1337
+ row(
1338
+ '=',
1339
+ 'Routed',
1340
+ r.routedUsd,
1341
+ undefined,
1342
+ wide && ratio !== null
1343
+ ? ui.line(
1344
+ ui.text('█'.repeat(filled), { color: 'cyan' }),
1345
+ ui.dim('░'.repeat(BAR - filled)),
1346
+ ui.dim(` ${ratio < 2 ? percent(ratio) : `×${ratio.toFixed(1)}`} of your model`),
1347
+ )
1348
+ : null,
1349
+ true,
1350
+ ),
1351
+ ];
1352
+ if (legacy?.replies)
1353
+ out.push(
1354
+ ...dimLines(
1355
+ ui,
1356
+ ` includes ${usd(legacy.routedUsd)} routed before dated records, kept without a breakdown`,
1357
+ columns,
1358
+ ),
1359
+ );
1360
+ return out;
1361
+ }
1362
+
1363
+ // Replies, turns, tokens and the cache share of the input.
1364
+ const statsLine = (ui, s) =>
1365
+ ui.dim(
1366
+ ` ${plural(s.replies, 'reply', 'replies')} · ${plural(s.turns, 'turn')} · input ${formatTokens(s.inputTokens)}` +
1367
+ ` · cache hit ${s.inputTokens ? percent(s.cacheReadTokens / s.inputTokens) : '—'} · output ${formatTokens(s.outputTokens)}`,
1368
+ );
1369
+
1370
+ // The period's cost by activity, model or tier, most first; `g` presses the next grouping. A row opens to what it
1371
+ // holds and its cost parts. A narrow pane drops "mostly on" (or the share) before any number.
1372
+ function breakdownLines(ui, view, actions, summary, columns) {
1373
+ const by = GROUPS.some(([id]) => id === view.usageGroup) ? view.usageGroup : 'activity';
1374
+ const next = GROUPS[(GROUPS.findIndex(([id]) => id === by) + 1) % GROUPS.length][0];
1375
+ const out = [
1432
1376
  ui.line(
1433
- ui.head('CLAUDE READINGS'),
1434
- ` cost ${dollars(metrics.cost)}`,
1435
- ui.dim(' · '),
1436
- ui.text(`context ${share(metrics.contextBar.percent)}`, { color: metrics.contextBar.color }),
1437
- ui.dim(' · '),
1438
- ui.text(`cache ${share(metrics.cacheBar.percent)}`, { color: metrics.cacheBar.color }),
1439
- ' ',
1440
- ui.Button({
1441
- key: 'usage-detail',
1442
- label: view.usageDetail ? 'less' : 'details',
1443
- variant: view.usageDetail ? 'primary' : 'secondary',
1444
- onPress: actions.usageDetail,
1445
- }),
1377
+ ui.head('BY '),
1378
+ ...GROUPS.flatMap(([id, label]) => [
1379
+ ui.Button({
1380
+ key: `group-${id}`,
1381
+ label,
1382
+ ...(id === next ? { hotkey: 'g' } : {}),
1383
+ variant: id === by ? 'primary' : 'secondary',
1384
+ onPress: () => actions.usageGroup(id),
1385
+ }),
1386
+ ' ',
1387
+ ]),
1388
+ ui.dim(' g next'),
1446
1389
  ),
1447
- ...detail,
1448
- ui.text(' '),
1390
+ ];
1391
+ const groups = breakdown(summary.cells, by);
1392
+ if (!groups.length) return [...out, ui.dim(' no routed replies in this period')];
1393
+ const tail = !Number.isFinite(columns) || columns >= BREAKDOWN + TAIL;
1394
+ const tailHead = by === 'model' ? 'share' : 'mostly on';
1395
+ out.push(
1396
+ ui.dim(
1397
+ ` ${''.padEnd(NAME)}${'replies'.padStart(8)}${'cost'.padStart(10)}${'vs yours'.padStart(11)}${tail ? ` ${tailHead}` : ''}`,
1398
+ ),
1399
+ );
1400
+ const numbers = (sums) => [
1401
+ `${String(sums[0]).padStart(8)}${usd(sums[1]).padStart(10)}`,
1402
+ differenceText(ui, sums[1] - sums[2], 11),
1403
+ ];
1404
+ for (const group of groups) {
1405
+ const id = `${by}:${group.name}`;
1406
+ const open = view.usageOpen === id;
1407
+ const route = by === 'model' ? null : mostlyOn(cellsWhere(summary.cells, by, group.name));
1408
+ out.push(
1409
+ ui.line(
1410
+ ' ',
1411
+ ui.Button({
1412
+ key: `row-${id}`,
1413
+ label: open ? '▾' : '▸',
1414
+ plain: true,
1415
+ onPress: () => actions.usageOpen(open ? null : id),
1416
+ }),
1417
+ ' ',
1418
+ ui.cell(NAME, groupName(ui, by, group.name)),
1419
+ ...numbers(group.sums),
1420
+ !tail
1421
+ ? null
1422
+ : by === 'model'
1423
+ ? ui.dim(` ${percent(summary.routedUsd > 0 ? group.sums[1] / summary.routedUsd : 0)}`)
1424
+ : route
1425
+ ? ui.dim(` ${routeText(route)}`)
1426
+ : null,
1427
+ ),
1428
+ );
1429
+ if (!open) continue;
1430
+ for (const child of group.children)
1431
+ out.push(ui.line(' ', ui.cell(NAME - 2, groupName(ui, CHILD_BY[by], child.name)), ...numbers(child.sums)));
1432
+ const [, , , cheaper, stronger, switched, input, , output] = group.sums;
1433
+ out.push(
1434
+ ...dimLines(
1435
+ ui,
1436
+ ` cheaper ${signedUsd(cents(cheaper))} · stronger ${signedUsd(cents(stronger))} · switch ${signedUsd(cents(switched))} · input ${formatTokens(input)} · output ${formatTokens(output)}`,
1437
+ columns,
1438
+ ),
1439
+ );
1440
+ }
1441
+ return out;
1442
+ }
1443
+
1444
+ // What the period's tokens would cost on each model alone, as your model for every reply (compareModels), beside what
1445
+ // routing spent and your own models; a model some reply had no price on is left out.
1446
+ function compareLines(ui, summary) {
1447
+ const yours = Object.keys(summary.yours);
1448
+ const single = yours.length === 1 ? yours[0] : null;
1449
+ const rows = Object.entries(summary.whatIf)
1450
+ .filter(([, [n]]) => n === summary.replies)
1451
+ .map(([model, [, value]]) => ({
1452
+ label: `${modelName(model)}${model === single ? ' · yours' : ''}`,
1453
+ value,
1454
+ routed: false,
1455
+ yours: model === single,
1456
+ }));
1457
+ if (!rows.some((row) => row.yours))
1458
+ rows.push({
1459
+ label: yours.length > 1 ? 'Your models' : 'Your model',
1460
+ value: summary.yoursUsd,
1461
+ routed: false,
1462
+ yours: true,
1463
+ });
1464
+ rows.push({ label: 'Routed', value: summary.routedUsd, routed: true, yours: false });
1465
+ rows.sort((a, b) => a.value - b.value);
1466
+ const max = Math.max(...rows.map((row) => row.value));
1467
+ return [
1468
+ ...rows.map((row) => {
1469
+ const filled = max > 0 ? Math.max(0, Math.min(BAR, Math.round((row.value / max) * BAR))) : 0;
1470
+ return ui.line(
1471
+ ' ',
1472
+ ui.cell(22, ui.text(row.label, row.routed ? { bold: true } : {})),
1473
+ ui.text('█'.repeat(filled), { color: row.routed ? 'cyan' : 'gray' }),
1474
+ ' '.repeat(BAR - filled),
1475
+ usd(row.value).padStart(LEDGER_AMOUNT),
1476
+ row.routed ? '' : row.yours ? ui.dim('—'.padStart(11)) : differenceText(ui, row.value - summary.yoursUsd, 11),
1477
+ );
1478
+ }),
1479
+ ui.dim(' same tokens and output on each model alone · answer quality not measured'),
1480
+ ];
1481
+ }
1482
+
1483
+ // Routed cost per hour, day or week as one line of blocks, a dot for a bucket without replies; and the What if switch.
1484
+ function trendLines(ui, view, actions, summary, period, now) {
1485
+ const toggle = ui.Button({
1486
+ key: 'usage-compare',
1487
+ label: view.usageCompare ? 'Trend' : 'What if',
1488
+ hotkey: 'c',
1489
+ onPress: actions.usageCompare,
1490
+ });
1491
+ const t = trend(summary, period, now);
1492
+ if (t.values.length < 2) return [ui.line(ui.head('COST'), ui.dim(' one bucket so far '), toggle)];
1493
+ const max = Math.max(...t.values);
1494
+ const glyphs = t.values.map((v) => (v > 0 ? SPARK[Math.min(7, Math.floor((v / max) * 8))] : '·')).join('');
1495
+ return [
1449
1496
  ui.line(
1450
- ui.Button({ key: 'reset-stats', label: 'Reset stats', onPress: actions.resetStats }),
1451
- ui.dim(' clears activity stats and routing vs your model, all sessions'),
1497
+ ui.head(`COST BY ${t.unit.toUpperCase()} `),
1498
+ ui.text(glyphs, { color: 'cyan' }),
1499
+ ui.dim(` peak ${usd(max)} `),
1500
+ toggle,
1452
1501
  ),
1453
- ui.dim('Press ? for what these estimates leave out.'),
1454
1502
  ];
1455
1503
  }
1456
1504
 
1505
+ // The evidence worth a look, each with the tab that changes it: the cell that carried most of the cost, the switch
1506
+ // writes against the saving, the cell that ran above your model. Never which model to use.
1507
+ function insightLines(ui, summary, actions, columns) {
1508
+ if (summary.replies < INSIGHT_REPLIES) return [];
1509
+ const list = insights(summary);
1510
+ if (!list.length) return [];
1511
+ const where = ({ activity, tier }) => [
1512
+ ...(ACTIVITIES.includes(activity) ? [ui.activity(activity), ui.dim(' · ')] : []),
1513
+ TIERS.includes(tier) ? ui.tier(tier) : ui.dim('not routed'),
1514
+ ];
1515
+ const link = (insight) =>
1516
+ TIERS.includes(insight.tier)
1517
+ ? ui.Button({
1518
+ key: `insight-${insight.kind}`,
1519
+ label: 'Routes',
1520
+ onPress: () =>
1521
+ actions.openRoute(
1522
+ ACTIVITIES.includes(insight.activity) ? `${insight.activity}.${insight.tier}` : `tier.${insight.tier}`,
1523
+ ),
1524
+ })
1525
+ : null;
1526
+ const lines = list.map((insight) =>
1527
+ insight.kind === 'switch'
1528
+ ? ui.line(
1529
+ ` Switch cache writes used ${percent(insight.share)} of the cheaper-model saving `,
1530
+ ui.Button({ key: 'insight-switch', label: 'Policy', onPress: () => actions.tab('policy') }),
1531
+ )
1532
+ : ui.line(
1533
+ ' ',
1534
+ ...where(insight),
1535
+ insight.kind === 'cost'
1536
+ ? ` carried ${percent(insight.share)} of the cost`
1537
+ : ` ran ${usd(insight.usd)} above your model`,
1538
+ insight.route && (!Number.isFinite(columns) || columns >= 90)
1539
+ ? ui.dim(` · mostly ${routeText(insight.route)}`)
1540
+ : null,
1541
+ ' ',
1542
+ link(insight),
1543
+ ),
1544
+ );
1545
+ return [
1546
+ ui.text(' '),
1547
+ ui.line(ui.head('WORTH A LOOK'), ui.dim(' · from the numbers; answer quality is not measured')),
1548
+ ...lines,
1549
+ ];
1550
+ }
1551
+
1552
+ // Delete history waits for a second press, as nothing brings the records back.
1553
+ function deleteLine(ui, view, actions) {
1554
+ if (!view.confirmDelete)
1555
+ return ui.Button({
1556
+ key: 'delete-history',
1557
+ label: 'Delete history',
1558
+ dimColor: true,
1559
+ onPress: () => actions.confirmDelete(true),
1560
+ });
1561
+ return ui.line(
1562
+ ui.text('Delete the usage, activity and subagent history of every session? ', { color: 'yellow' }),
1563
+ ui.Button({ key: 'delete-confirm', label: 'Delete', variant: 'primary', onPress: actions.resetStats }),
1564
+ ' ',
1565
+ ui.Button({ key: 'delete-keep', label: 'Keep', onPress: () => actions.confirmDelete(false) }),
1566
+ );
1567
+ }
1568
+
1569
+ // Answer first for the chosen period: routing against your model as a ledger that adds up, the tokens, the cost by
1570
+ // activity, model or tier, the trend or What if, and what is worth a look. Subagents follow, across sessions.
1571
+ function usageTab(ui, config, view, _usage, actions, _modelOptions, columns, now) {
1572
+ const period = PERIODS.includes(view.usagePeriod) ? view.usagePeriod : 'session';
1573
+ const record = readUsage(period === 'session' ? view.usageSession : view.usageStore);
1574
+ const summary = summarize(record, periodRange(period, now));
1575
+ // All adds the undated savings totals kept from before the usage records.
1576
+ const kept = period === 'all' ? allTime(view.usageStore, view.savingsStore) : null;
1577
+ const legacy = kept?.legacy ?? null;
1578
+ const r = readout(kept?.totals ?? summary);
1579
+ const out = [periodRow(ui, period, actions), scopeLine(ui, config, view, period, summary, now), ui.text(' ')];
1580
+ out.push(...headline(ui, config, view, period, r));
1581
+ if (r.replies) {
1582
+ out.push(ui.text(' '), ...ledger(ui, config, view, { period, r, summary, legacy, columns }));
1583
+ if (summary.replies) out.push(ui.text(' '), statsLine(ui, summary));
1584
+ }
1585
+ if (summary.replies) {
1586
+ out.push(ui.text(' '), ...breakdownLines(ui, view, actions, summary, columns), ui.text(' '));
1587
+ out.push(
1588
+ ...(view.usageCompare
1589
+ ? [
1590
+ ui.line(
1591
+ ui.head('WHAT IF'),
1592
+ ui.dim(' · the same tokens on one model '),
1593
+ ui.Button({ key: 'usage-compare', label: 'Trend', hotkey: 'c', onPress: actions.usageCompare }),
1594
+ ),
1595
+ ...compareLines(ui, summary),
1596
+ ]
1597
+ : trendLines(ui, view, actions, summary, period, now)),
1598
+ ...insightLines(ui, summary, actions, columns),
1599
+ );
1600
+ }
1601
+ out.push(ui.text(' '), ...subagentUsage(ui, config, view, columns), ui.text(' '), ...footer(ui, columns));
1602
+ out.push(ui.text(' '), deleteLine(ui, view, actions));
1603
+ return out;
1604
+ }
1605
+
1457
1606
  function helpLines(ui) {
1458
1607
  return [
1459
1608
  ui.text(' '),