@alexeiled/claude-router 1.17.0 → 1.18.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
@@ -18,12 +18,14 @@ import { effortSharesCache, routeEffort } from './cost.mjs';
18
18
  import {
19
19
  activityReading,
20
20
  activityVerb,
21
+ COST_PART,
21
22
  classifierLabel,
22
23
  classifierStatus,
23
- differenceShare,
24
+ comparedModel,
24
25
  formatTokens,
25
26
  GATEWAY_CLEANUP,
26
27
  GATEWAY_SETTINGS,
28
+ liveUsage,
27
29
  missingCredentials,
28
30
  missingText,
29
31
  modelColor,
@@ -31,14 +33,18 @@ import {
31
33
  percent,
32
34
  percentPair,
33
35
  probeOf,
36
+ recordedModels,
34
37
  routeLabel,
35
38
  routeName,
39
+ routingAnswer,
36
40
  routingModeOf,
37
41
  routingModeText,
38
42
  savingsColor,
43
+ sessionUsage,
39
44
  shownConfig,
40
45
  signedUsd,
41
46
  sparkline,
47
+ switchesLost,
42
48
  TIER_COLOR,
43
49
  unavailableReason,
44
50
  unpricedLine,
@@ -67,7 +73,6 @@ import {
67
73
  mostlyOn,
68
74
  PERIODS,
69
75
  periodRange,
70
- readUsage,
71
76
  summarize,
72
77
  trend,
73
78
  } from './usage.mjs';
@@ -264,7 +269,7 @@ function header(ui, config, view, tab, actions) {
264
269
  ];
265
270
  }
266
271
 
267
- // "Jev gave stronger tiers 88% (needs 82%) · switch ≈ $0.48": the support for the switch the policy weighed against
272
+ // "Jev gave stronger tiers 88% (needs 82%) · next switch ≈ $0.48": the support for the switch the policy weighed against
268
273
  // the bar it set, and the switch cost. Null without a reading.
269
274
  function supportText(label, estimate) {
270
275
  if (!Number.isFinite(estimate?.threshold)) return null;
@@ -272,7 +277,7 @@ function supportText(label, estimate) {
272
277
  ? ['stronger', estimate.upgradeMass]
273
278
  : ['cheaper', estimate.downgradeMass];
274
279
  const [needs, gave] = percentPair(estimate.threshold, Number.isFinite(mass) ? mass : estimate.threshold);
275
- const cost = Number.isFinite(estimate.taxUsd) ? ` · switch ≈ ${usd(estimate.taxUsd)}` : '';
280
+ const cost = Number.isFinite(estimate.taxUsd) ? ` · next switch ≈ ${usd(estimate.taxUsd)}` : '';
276
281
  return `${label} gave ${direction} tiers ${Number.isFinite(mass) ? gave : '—'} (needs ${needs})${cost}`;
277
282
  }
278
283
 
@@ -340,11 +345,11 @@ function nowTab(ui, config, view, usage, actions, _modelOptions, columns, now) {
340
345
  : null,
341
346
  pinLine(ui, view, auto, unavailable, actions, columns),
342
347
  ];
343
- const session = summarize(readUsage(view.usageSession), periodRange('session', now));
344
- const today = summarize(readUsage(view.usageStore), periodRange('today', now));
345
- out.push(ui.text(' '), ...sessionLines(ui, view, session, actions, columns, now));
348
+ const session = sessionUsage(view, now);
349
+ const today = summarize(liveUsage(view, view.usageStore), periodRange('today', now));
350
+ out.push(ui.text(' '), ...sessionLines(ui, config, view, session, actions, columns, now));
346
351
  // Today repeats this session's part of it until another session adds to it.
347
- const ownToday = summarize(readUsage(view.usageSession), periodRange('today', now));
352
+ const ownToday = summarize(liveUsage(view, view.usageSession), periodRange('today', now));
348
353
  if (today.replies > ownToday.replies) out.push(ui.text(' '), ...todayLines(ui, today, actions, columns));
349
354
  out.push(
350
355
  ...sessionAgentLines(ui, view, actions, columns),
@@ -473,8 +478,8 @@ function fittedStrip(strip, columns, since, now) {
473
478
  // This session: since its first routed reply, its routed replies and route switches; its replies as a strip colored
474
479
  // by tier, with the replies per tier, and the routed replies per activity; the last switch; and routing against your
475
480
  // model, linked to Usage.
476
- function sessionLines(ui, view, summary, actions, columns, now) {
477
- const since = readUsage(view.usageSession).since;
481
+ function sessionLines(ui, config, view, summary, actions, columns, now) {
482
+ const since = liveUsage(view, view.usageSession).since;
478
483
  const strip = view.strip?.cells?.length ? fittedStrip(view.strip, columns, since, now) : null;
479
484
  const counts = {};
480
485
  for (const cell of view.strip?.cells ?? [])
@@ -484,7 +489,7 @@ function sessionLines(ui, view, summary, actions, columns, now) {
484
489
  const head = [
485
490
  Number.isFinite(since) ? `since ${sinceText(since, now)}` : null,
486
491
  summary.replies
487
- ? plural(summary.replies, 'routed reply', 'routed replies')
492
+ ? `${plural(summary.replies, 'routed reply', 'routed replies')}${view.usageLive?.length ? ` (${view.usageLive.length} live)` : ''}`
488
493
  : replies
489
494
  ? plural(replies, 'reply', 'replies')
490
495
  : null,
@@ -492,6 +497,16 @@ function sessionLines(ui, view, summary, actions, columns, now) {
492
497
  ].filter(Boolean);
493
498
  if (!strip && !summary.replies) return [ui.line(ui.head('SESSION'), ui.dim(' · no replies yet'))];
494
499
  const out = [ui.line(ui.head('SESSION'), head.length ? ui.dim(` · ${head.join(' · ')}`) : null)];
500
+ // The band's session row, then the parts that add up to it.
501
+ if (summary.replies) {
502
+ const r = readout(summary);
503
+ out.push(
504
+ answerLine(ui, r, comparedModel(config, view, summary), () => actions.usageFor('session'), 'session', columns),
505
+ );
506
+ if (!r.early)
507
+ out.push(...ledger(ui, config, view, { period: 'session', r, summary, legacy: null, columns, compact: true }));
508
+ if (!r.early && switchesLost(r)) out.push(lostLine(ui, actions));
509
+ }
495
510
  if (strip) {
496
511
  out.push(
497
512
  ui.line(
@@ -535,8 +550,6 @@ function sessionLines(ui, view, summary, actions, columns, now) {
535
550
  );
536
551
  const last = view.lastSwitch;
537
552
  if (Number.isFinite(last?.at) && last.from && last.to) out.push(lastSwitchLine(ui, last, now));
538
- if (summary.replies)
539
- out.push(verdictLine(ui, readout(summary), () => actions.usageFor('session'), 'session', columns));
540
553
  return out;
541
554
  }
542
555
 
@@ -553,25 +566,27 @@ function lastSwitchLine(ui, last, now) {
553
566
  );
554
567
  }
555
568
 
556
- // Routing against your model, with the Usage tab on the same period one press away. The switch writes in it and an
557
- // early sample follow where the pane has room, the early sample dropping first.
558
- function verdictLine(ui, r, open, period, columns) {
559
- const notes = [
560
- r.switchUsd > 0 ? ` · switch writes ${usd(r.switchUsd)}` : null,
561
- r.early ? ' · early sample' : null,
562
- ].filter(Boolean);
563
- const width = () => 2 + verdictText(r).join('').length + notes.join('').length + USAGE_LINK;
564
- while (notes.length && Number.isFinite(columns) && width() > columns) notes.pop();
569
+ // Routing against your model in the band's words, "routing −$1.84 (−22%) vs Opus 5.5 · xhigh", with the Usage tab on
570
+ // the same period one press away; an early sample says so.
571
+ function answerLine(ui, r, against, open, period, columns) {
572
+ const early = ' · early sample';
573
+ const width = 2 + routingAnswer(r).length + ` vs ${against}`.length + early.length + USAGE_LINK;
565
574
  return ui.line(
566
575
  ' ',
567
- ...verdictParts(ui, r),
568
- ...notes.map((note) => ui.dim(note)),
576
+ ...answerParts(ui, r, against),
577
+ r.early && (!Number.isFinite(columns) || width <= columns) ? ui.dim(early) : null,
569
578
  ' ',
570
579
  ui.Button({ key: `usage-${period}`, label: 'Usage', onPress: open }),
571
580
  );
572
581
  }
573
- // " [ Usage ]" after the verdict.
582
+ // " [ Usage ]" after the answer.
574
583
  const USAGE_LINK = 11;
584
+ // Cache rebuilds cost more than the cheaper models saved: the policy's switch weight is the lever.
585
+ const lostLine = (ui, actions) =>
586
+ ui.line(
587
+ ui.text(' ▲ switches cost more than they saved ', { color: 'yellow' }),
588
+ ui.Button({ key: 'session-policy', label: 'Policy', onPress: () => actions.tab('policy') }),
589
+ );
575
590
 
576
591
  // Today across sessions, shown once another session added to it.
577
592
  function todayLines(ui, summary, actions, columns) {
@@ -583,7 +598,7 @@ function todayLines(ui, summary, actions, columns) {
583
598
  ` · ${plural(summary.replies, 'routed reply', 'routed replies')} · ${plural(switches, 'route switch', 'route switches')}`,
584
599
  ),
585
600
  ),
586
- verdictLine(ui, readout(summary), () => actions.usageFor('today'), 'today', columns),
601
+ answerLine(ui, readout(summary), 'each session’s own model', () => actions.usageFor('today'), 'today', columns),
587
602
  ];
588
603
  }
589
604
 
@@ -1352,7 +1367,8 @@ function squares(ui, models, max, scale) {
1352
1367
  return { parts, width: max - left, rest: scale ? 0 : total - (max - left) };
1353
1368
  }
1354
1369
 
1355
- // "Routing saved $0.21 (5%) on 7 routed agents": the routed agents' cost against Claude Code's picks.
1370
+ // "routing −$0.21 (−5%) vs Claude Code’s picks on 7 routed agents", in the session readout's words: the routed
1371
+ // agents' cost against Claude Code's picks.
1356
1372
  function agentVerdict(ui, routed) {
1357
1373
  const agents = routed.reduce((n, [, c]) => n + c.agents, 0);
1358
1374
  const totals = routed.map(([, c]) => modelTotals(c));
@@ -1365,12 +1381,11 @@ function agentVerdict(ui, routed) {
1365
1381
  if (!counts.pricedRequests) return ui.dim(` ${plural(agents, 'routed agent')}, not priced`);
1366
1382
  const difference = cents(cost - core);
1367
1383
  const on = pricedText(` on ${plural(agents, 'routed agent')}`, counts);
1368
- if (!difference) return ui.line(' Routing cost the same as Claude Code’s picks', ui.dim(on));
1384
+ const share = core > 0 && difference ? ` (${difference < 0 ? '−' : '+'}${percent(Math.abs(difference) / core)})` : '';
1369
1385
  return ui.line(
1370
- ` Routing ${difference < 0 ? 'saved' : 'cost'} `,
1371
- ui.text(usd(difference), { color: savingsColor(difference) }),
1372
- difference < 0 ? '' : ' more',
1373
- core > 0 ? ` (${percent(Math.abs(difference) / core)})` : '',
1386
+ ' ',
1387
+ ui.text(`routing ${signedUsd(difference)}${share}`, { bold: true, color: savingsColor(difference) }),
1388
+ ' vs Claude Code’s picks',
1374
1389
  ui.dim(on),
1375
1390
  );
1376
1391
  }
@@ -1678,16 +1693,8 @@ function scopeLine(ui, config, view, period, summary, now) {
1678
1693
  return ui.dim(`all projects · ${span} · each session vs its own model`);
1679
1694
  }
1680
1695
 
1681
- // The models a session's replies were priced against, when they are not the one `/model` selects now: after a
1682
- // `/model` change the record keeps each reply against the model selected when it ran. Null when they are.
1683
- function recordedModels(config, view, summary) {
1684
- const ids = Object.keys(summary.yours);
1685
- if (!ids.length || (ids.length === 1 && ids[0] === modelSpec(config, view.nativeModel)?.id)) return null;
1686
- return ids.map(modelName).join(', ');
1687
- }
1688
-
1689
- // The answer first: "Routing saved $1.84 (22%) vs your model", what it cost more, or why there is nothing to compare.
1690
- function headline(ui, config, view, period, r) {
1696
+ // The answer first, in the band's words: "routing −$1.84 (−22%) vs Opus 5.5 · xhigh", or why there is nothing to compare.
1697
+ function headline(ui, config, view, period, r, summary) {
1691
1698
  if (!r.replies) {
1692
1699
  // A reply is priced against your model, so on a model without a price none is counted.
1693
1700
  if (period === 'session' && !modelSpec(config, view.nativeModel))
@@ -1701,31 +1708,24 @@ function headline(ui, config, view, period, r) {
1701
1708
  ),
1702
1709
  ];
1703
1710
  }
1711
+ const against = period === 'session' ? comparedModel(config, view, summary) : 'each session’s own model';
1704
1712
  return [
1705
1713
  ui.line(
1706
- ...verdictParts(ui, r),
1714
+ ...answerParts(ui, r, against),
1707
1715
  r.early ? ui.text(` early sample · ${plural(r.replies, 'reply', 'replies')}`, { color: 'yellow' }) : null,
1708
1716
  ),
1709
1717
  ];
1710
1718
  }
1711
1719
 
1712
- // "Routing saved $1.84 (22%) vs your model", or what it cost more: the text before the amount, the amount, the rest.
1713
- function verdictText(r) {
1714
- const share = differenceShare(r)?.replace(/^[−+]/, '');
1715
- return [
1716
- r.differenceUsd < 0 ? 'Routing saved ' : r.differenceUsd > 0 ? 'Routing cost ' : 'Routing cost the same as',
1717
- r.differenceUsd === 0 ? '' : `${usd(r.differenceUsd)}${share ? ` (${share})` : ''}`,
1718
- r.differenceUsd < 0 ? ' vs your model' : r.differenceUsd > 0 ? ' more than your model' : ' your model',
1719
- ];
1720
- }
1721
- // The verdict as line parts, the amount bold in the savings color.
1722
- function verdictParts(ui, r) {
1723
- const [before, amount, after] = verdictText(r);
1724
- return [before, amount ? ui.text(amount, { bold: true, color: savingsColor(r.differenceUsd) }) : '', after];
1720
+ const capital = (words) => words[0].toUpperCase() + words.slice(1);
1721
+
1722
+ // "routing −$1.84 (−22%) vs Opus 5.5 · xhigh", the answer bold in the savings color, as the band says it.
1723
+ function answerParts(ui, r, against) {
1724
+ return [ui.text(routingAnswer(r), { bold: true, color: savingsColor(r.differenceUsd) }), ` vs ${against}`];
1725
1725
  }
1726
1726
 
1727
1727
  // Your model − cheaper models + stronger models + switch cache writes = routed, in whole cents that add up (readout).
1728
- function ledger(ui, config, view, { period, r, summary, legacy, columns }) {
1728
+ function ledger(ui, config, view, { period, r, summary, legacy, columns, compact = false }) {
1729
1729
  const tier = yourTier(config, view);
1730
1730
  const row = (sign, label, value, color, note = null, bold = false) =>
1731
1731
  ui.line(
@@ -1756,22 +1756,26 @@ function ledger(ui, config, view, { period, r, summary, legacy, columns }) {
1756
1756
  ? ui.tier(tier, yourModel(view))
1757
1757
  : ui.text(yourModel(view)),
1758
1758
  ),
1759
- row('−', 'Cheaper models', r.cheaperUsd, savingsColor(r.cheaperUsd)),
1760
- row('+', 'Stronger models', r.strongerUsd, savingsColor(r.strongerUsd)),
1761
- row(
1762
- '+',
1763
- 'Switch cache writes',
1764
- r.switchUsd,
1765
- savingsColor(r.switchUsd),
1766
- ui.dim(
1767
- [
1768
- switches === null ? null : plural(switches, 'route switch', 'route switches'),
1769
- saving > 0 && r.switchUsd > 0 ? `${percent(r.switchUsd / saving)} of the saving` : null,
1770
- ]
1771
- .filter(Boolean)
1772
- .join(' · '),
1773
- ),
1774
- ),
1759
+ compact && !r.cheaperUsd ? null : row('−', capital(COST_PART.cheaper), r.cheaperUsd, savingsColor(r.cheaperUsd)),
1760
+ compact && !r.strongerUsd
1761
+ ? null
1762
+ : row('+', capital(COST_PART.stronger), r.strongerUsd, savingsColor(r.strongerUsd)),
1763
+ compact && !r.switchUsd
1764
+ ? null
1765
+ : row(
1766
+ '+',
1767
+ capital(COST_PART.switches),
1768
+ r.switchUsd,
1769
+ savingsColor(r.switchUsd),
1770
+ ui.dim(
1771
+ [
1772
+ switches === null ? null : plural(switches, 'route switch', 'route switches'),
1773
+ saving > 0 && r.switchUsd > 0 ? `${percent(r.switchUsd / saving)} of the saving` : null,
1774
+ ]
1775
+ .filter(Boolean)
1776
+ .join(' · '),
1777
+ ),
1778
+ ),
1775
1779
  row(
1776
1780
  '=',
1777
1781
  'Routed',
@@ -1786,7 +1790,7 @@ function ledger(ui, config, view, { period, r, summary, legacy, columns }) {
1786
1790
  : null,
1787
1791
  true,
1788
1792
  ),
1789
- ];
1793
+ ].filter(Boolean);
1790
1794
  if (legacy?.replies)
1791
1795
  out.push(
1792
1796
  ...dimLines(
@@ -1972,7 +1976,7 @@ function insightLine(ui, insight, actions, columns) {
1972
1976
  : null;
1973
1977
  return insight.kind === 'switch'
1974
1978
  ? ui.line(
1975
- ` Switch cache writes used ${percent(insight.share)} of the cheaper-model saving `,
1979
+ ` Cache rebuilds used ${percent(insight.share)} of the cheaper-model saving `,
1976
1980
  ui.Button({ key: 'insight-switch', label: 'Policy', onPress: () => actions.tab('policy') }),
1977
1981
  )
1978
1982
  : ui.line(
@@ -2010,14 +2014,14 @@ function deleteLine(ui, view, actions) {
2010
2014
  // activity, model or tier, the trend or What if, and what is worth a look. Subagents follow, for the same period.
2011
2015
  function usageTab(ui, config, view, _usage, actions, _modelOptions, columns, now) {
2012
2016
  const period = PERIODS.includes(view.usagePeriod) ? view.usagePeriod : 'session';
2013
- const record = readUsage(period === 'session' ? view.usageSession : view.usageStore);
2017
+ const record = liveUsage(view, period === 'session' ? view.usageSession : view.usageStore);
2014
2018
  const summary = summarize(record, periodRange(period, now));
2015
2019
  // All adds the undated savings totals kept from before the usage records.
2016
- const kept = period === 'all' ? allTime(view.usageStore, view.savingsStore) : null;
2020
+ const kept = period === 'all' ? allTime(record, view.savingsStore) : null;
2017
2021
  const legacy = kept?.legacy ?? null;
2018
2022
  const r = readout(kept?.totals ?? summary);
2019
2023
  const out = [periodRow(ui, period, actions), scopeLine(ui, config, view, period, summary, now), ui.text(' ')];
2020
- out.push(...headline(ui, config, view, period, r));
2024
+ out.push(...headline(ui, config, view, period, r, summary));
2021
2025
  if (r.replies) {
2022
2026
  out.push(ui.text(' '), ...ledger(ui, config, view, { period, r, summary, legacy, columns }));
2023
2027
  if (summary.replies) out.push(ui.text(' '), statsLine(ui, summary));
package/lib/route.mjs CHANGED
@@ -218,16 +218,18 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
218
218
  (!loop.decision && Object.values(config.models).find((m) => isSameModel(m.id, nativeModel))) || specOf(last);
219
219
  const shrinksUnmeasured = (cell) =>
220
220
  (!contextKnown || !loop.historyMeasured) && specOf(cell).contextWindow < lastSpec.contextWindow;
221
- const fits = (cell) => {
221
+ const runs = (cell) => {
222
222
  const model = specOf(cell);
223
223
  return (
224
224
  !shrinksUnmeasured(cell) &&
225
225
  (!facts.hasImages || model.imageInput !== false) &&
226
226
  isModelAllowed(model.id, availableModels, nativeModel) &&
227
- !loop.ineligible.includes(model.id) &&
228
- (!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * CONTEXT_FILL)
227
+ !loop.ineligible.includes(model.id)
229
228
  );
230
229
  };
230
+ const roomFor = (cell) =>
231
+ !contextKnown || costs.nextContextTokens(facts) <= specOf(cell).contextWindow * CONTEXT_FILL;
232
+ const fits = (cell) => runs(cell) && roomFor(cell);
231
233
  if (contextKnown) {
232
234
  const cell = result;
233
235
  const routeFor = (tier) => (tier === cell.tier ? routeOf(cell) : resolveRoute(config, tier, cell.activity));
@@ -243,7 +245,9 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
243
245
  const fallback = cells.find(
244
246
  (cell) => cell && (cell.activity === null || cell.activity === activity) && fits(cell),
245
247
  ) ?? { tier: null, activity: null };
246
- result = { ...result, ...fallback, reason: 'model-unavailable', lateral: null };
248
+ // Only the window failed: no route is withheld, the conversation is just too full for every one of them.
249
+ const reason = runs(result) ? 'context-full' : 'model-unavailable';
250
+ result = { ...result, ...fallback, reason, lateral: null };
247
251
  }
248
252
  const route = result.tier ? routeOf(result) : null;
249
253
  const model = route ? routeSpec(config, route).id : nativeModel;
package/lib/savings.mjs CHANGED
@@ -1,7 +1,6 @@
1
1
  // Routing vs your model: what each main reply cost at configured list prices on the model that served it, against
2
2
  // the same tokens on the session's own model and effort ("your model"). Counts and dollars only, never text. Pure:
3
- // the hook feeds each reply, keeps the session totals in the view, and keeps each reply by day in usage.mjs's
4
- // records. The records under SAVINGS_PREFIX are undated history from before those, read and never written.
3
+ // the hook feeds each reply and keeps it by day in usage.mjs's records. The records under SAVINGS_PREFIX are undated history from before those, read and never written.
5
4
 
6
5
  import { effortSharesCache, ratesAt } from './cost.mjs';
7
6
  import { canonicalModel } from './providers.mjs';
@@ -13,7 +12,7 @@ export const SAVINGS_PREFIX = 'savings:v1:';
13
12
  // When Delete history last ran (ms), from any session: a session may still hold its record from before and write it back.
14
13
  // The activity metrics records (METRICS_PREFIX) use it too.
15
14
  export const SAVINGS_RESET_KEY = 'savings:reset:v1';
16
- // Fewer replies than this read "too early": one switch and its cache write dominate a short run.
15
+ // Fewer replies than this read as an "early sample": one switch and its cache write dominate a short run.
17
16
  export const EARLY_REPLIES = 10;
18
17
  const TTLS = ['5m', '1h'];
19
18
 
@@ -159,8 +158,6 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
159
158
  cheaperUsd: Math.min(0, modelUsd),
160
159
  strongerUsd: Math.max(0, modelUsd),
161
160
  switchUsd: routedUsd - servedOnYoursUsd,
162
- // The model a stronger reply ran on, for "9 replies on Opus 5.5".
163
- strongerModel: modelUsd > 0 ? servedSpec.id : null,
164
161
  },
165
162
  };
166
163
  }
@@ -183,9 +180,9 @@ export function unpricedCause(config, { usage, served, yours, ttl }) {
183
180
  return config.cache.mode === 'disabled' || Number.isFinite(config.cache.writeMultiplier[ttl]) ? null : 'ttl';
184
181
  }
185
182
 
186
- // One main reply against every configured model as yours, at your effort, so the session readout can follow a
187
- // `/model` change without replaying the session: `states` and `replies` by models key, each as compareReply returns,
188
- // and `reply`, the prices against `yours`, the API model id /model selects now, or null.
183
+ // One main reply against every configured model as yours, at your effort, so What if can price the same tokens on each
184
+ // model: `states` and `replies` by models key, each as compareReply returns, and `reply`, the prices against `yours`,
185
+ // the API model id /model selects now, or null.
189
186
  export function compareModels(config, states, { usage, served, effort, ttl, now, yours }) {
190
187
  const out = { states: {}, replies: {}, reply: null };
191
188
  for (const [alias, model] of Object.entries(config.models)) {
@@ -203,37 +200,8 @@ export function compareModels(config, states, { usage, served, effort, ttl, now,
203
200
  return out;
204
201
  }
205
202
 
206
- // Each model's session totals with this reply's prices added; models without a price for it keep theirs.
207
- export function addReplies(byModel, replies) {
208
- const next = { ...byModel };
209
- for (const [alias, reply] of Object.entries(replies))
210
- next[alias] = addReply(modelEntry(next, alias) ?? emptyTotals(), reply);
211
- return next;
212
- }
213
-
214
203
  const SUMS = ['routedUsd', 'yoursUsd', 'cheaperUsd', 'strongerUsd', 'switchUsd'];
215
204
 
216
- // A session's totals: replies and the five sums. `stronger` counts replies by the stronger model they ran on.
217
- export function emptyTotals() {
218
- return {
219
- replies: 0,
220
- routedUsd: 0,
221
- yoursUsd: 0,
222
- cheaperUsd: 0,
223
- strongerUsd: 0,
224
- switchUsd: 0,
225
- stronger: {},
226
- };
227
- }
228
-
229
- export function addReply(totals, reply) {
230
- const next = { ...totals, replies: totals.replies + 1 };
231
- for (const sum of SUMS) next[sum] = totals[sum] + reply[sum];
232
- if (reply.strongerModel)
233
- next.stronger = { ...totals.stronger, [reply.strongerModel]: (totals.stronger[reply.strongerModel] ?? 0) + 1 };
234
- return next;
235
- }
236
-
237
205
  // A record kept across sessions, one session's or their sum: replies and the five sums since `since` (ms), the first
238
206
  // reply after a reset.
239
207
  export function emptySavingsStore() {
package/lib/usage.mjs CHANGED
@@ -97,15 +97,41 @@ export function replyEntry({ at, mode, activity, tier, route, yours, reply, repl
97
97
 
98
98
  const addSums = (a, b) => a.map((value, i) => (IS_USD[i] ? microUsd(value + b[i]) : value + b[i]));
99
99
 
100
+ // Replies added to a record, each on the day and hour it arrived; no turn is counted, and `since` stays the record's:
101
+ // it is when the first turn was recorded, which a Reset compares with. The readouts add a running turn's replies this
102
+ // way, so the band and the pane count them before the turn ends and is recorded.
103
+ export function withReplies(record, entries) {
104
+ if (!entries.length) return record;
105
+ return { version: USAGE_VERSION, since: record.since, days: addEntries(record.days, entries) };
106
+ }
107
+
100
108
  // A finished turn's replies added to the session's record at `now`: each on the day and hour it arrived, the turn and
101
109
  // the route switch that started it (`switched`: 'tier', 'activity' or null) on the day it ended. Days past
102
110
  // RETAIN_DAYS leave the record. A turn without replies returns `record` itself.
103
111
  export function recordUsage(record, entries, { switched = null, now }) {
104
112
  if (!entries.length) return record;
105
- const days = structuredClone(record.days);
113
+ const days = addEntries(record.days, entries);
114
+ const ended = structuredClone(days[dayKey(now)] ?? emptyDay());
115
+ ended.turns += 1;
116
+ if (switched === 'tier' || switched === 'activity') ended.switches[switched] += 1;
117
+ days[dayKey(now)] = ended;
118
+ const cutoff = daysBefore(now, RETAIN_DAYS);
119
+ for (const key of Object.keys(days)) if (key < cutoff) delete days[key];
120
+ return { version: USAGE_VERSION, since: record.since ?? now, days };
121
+ }
122
+
123
+ // The record's days with the entries added. Only the days they touch are copied: the readouts add the running turn's
124
+ // replies to every session's record on each render.
125
+ function addEntries(recordDays, entries) {
126
+ const days = { ...recordDays };
127
+ const copied = new Set();
106
128
  for (const entry of entries) {
107
129
  const key = dayKey(entry.at);
108
- const day = days[key] ?? emptyDay();
130
+ if (!copied.has(key)) {
131
+ days[key] = days[key] ? structuredClone(days[key]) : emptyDay();
132
+ copied.add(key);
133
+ }
134
+ const day = days[key];
109
135
  day.cells[entry.cell] = day.cells[entry.cell] ? addSums(day.cells[entry.cell], entry.sums) : [...entry.sums];
110
136
  for (const [model, usd] of Object.entries(entry.whatIf)) {
111
137
  const [n, sum] = own(day.whatIf, model) ?? [0, 0];
@@ -116,13 +142,7 @@ export function recordUsage(record, entries, { switched = null, now }) {
116
142
  day.hours[hour] = microUsd((day.hours[hour] ?? 0) + entry.sums[1]);
117
143
  days[key] = day;
118
144
  }
119
- const ended = days[dayKey(now)] ?? emptyDay();
120
- ended.turns += 1;
121
- if (switched === 'tier' || switched === 'activity') ended.switches[switched] += 1;
122
- days[dayKey(now)] = ended;
123
- const cutoff = daysBefore(now, RETAIN_DAYS);
124
- for (const key of Object.keys(days)) if (key < cutoff) delete days[key];
125
- return { version: USAGE_VERSION, since: record.since ?? now, days };
145
+ return days;
126
146
  }
127
147
 
128
148
  const isCount = (n) => Number.isSafeInteger(n) && n >= 0;
package/lib/view.mjs CHANGED
@@ -47,15 +47,13 @@ export function initialView(model) {
47
47
  activityStats: null,
48
48
  activityStore: null,
49
49
  activityMetrics: null,
50
- // Routing vs your model: this session's totals and the totals kept across sessions as last read or written.
51
- // Routing vs each configured model this session, by models key; `savings` is the one /model selects now.
52
- savingsBy: {},
53
- savings: null,
54
50
  // The undated savings totals from before the usage records, added to the All period.
55
51
  savingsStore: null,
56
52
  // Routed usage by day: every session's records as last read or written, and this session's own.
57
53
  usageStore: null,
58
54
  usageSession: null,
55
+ // The replies of turns still running (replyEntry), not yet in usageSession.
56
+ usageLive: [],
59
57
  // Subagents by day: every session's records as last read or written, and this session's own.
60
58
  subagentStore: null,
61
59
  subagentSession: null,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexeiled/claude-router",
3
- "version": "1.17.0",
3
+ "version": "1.18.0",
4
4
  "description": "Native Claude Code model and effort routing with Anthropic, DeepSeek and z.ai defaults, custom provider profiles, and classifier advice.",
5
5
  "license": "MIT",
6
6
  "author": "Alexei Ledenev",