@alexeiled/claude-router 1.8.0 → 1.9.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/.claude-plugin/plugin.json +2 -2
- package/CHANGELOG.md +17 -0
- package/README.md +7 -3
- package/docs/activity-routing.md +5 -4
- package/docs/architecture.md +13 -3
- package/docs/configuration.md +62 -2
- package/docs/evaluation.md +39 -3
- package/docs/native-router.md +3 -3
- package/docs/router-band.svg +60 -60
- package/docs/router-pane-routing.svg +3 -4
- package/docs/router-pane-usage.svg +12 -9
- package/docs/user-guide.md +5 -5
- package/hooks/native-router.mjs +339 -33
- package/lib/band.mjs +29 -8
- package/lib/classifier-client.mjs +6 -3
- package/lib/config.mjs +37 -1
- package/lib/cost.mjs +14 -3
- package/lib/display.mjs +13 -1
- package/lib/panel.mjs +84 -10
- package/lib/route.mjs +2 -2
- package/lib/savings.mjs +11 -3
- package/lib/subagents.mjs +280 -0
- package/package.json +1 -1
- package/types/index.d.ts +29 -0
package/lib/band.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { activeClassifier, TIERS } from './config.mjs';
|
|
2
2
|
import {
|
|
3
|
+
activityVerb,
|
|
3
4
|
classifierLabel,
|
|
4
5
|
classifierStatus,
|
|
5
6
|
classifierWarns,
|
|
@@ -28,7 +29,15 @@ const GAP = ' ';
|
|
|
28
29
|
const ROUTER_BUTTON = 8;
|
|
29
30
|
|
|
30
31
|
// Hex, like TIER_COLOR, so every surface draws the same shades. Yellow stays out: it marks a fault that needs action.
|
|
31
|
-
|
|
32
|
+
// Activity is Claude Code's orange for a turn's action; italic sets it apart from the high and medium tier colors.
|
|
33
|
+
const COLOR = {
|
|
34
|
+
accent: '#7c9cff',
|
|
35
|
+
off: '#2a3040',
|
|
36
|
+
muted: '#8b93a7',
|
|
37
|
+
warn: '#f0b429',
|
|
38
|
+
ink: '#0d1016',
|
|
39
|
+
activity: '#d77757',
|
|
40
|
+
};
|
|
32
41
|
|
|
33
42
|
const part = (text, style = {}) => ({ text, style });
|
|
34
43
|
// A filled chip: the text on a background, as the prototype's status pills.
|
|
@@ -82,12 +91,14 @@ const pendingMeter = () => [...BARS].map((bar) => part(bar, { color: COLOR.accen
|
|
|
82
91
|
|
|
83
92
|
const route = (view) => routeLabel(view.actualModel ?? view.selectedModel ?? view.nativeModel, view.effort);
|
|
84
93
|
|
|
85
|
-
// The turn's activity between the tier and the route,
|
|
86
|
-
//
|
|
94
|
+
// The turn's activity between the tier and the route, as a verb in orange italic; dim and marked in shadow, where the
|
|
95
|
+
// route shown is still the one in use. One part with its arrow, so a narrow band drops both before the reason.
|
|
87
96
|
function activityPart(config, view) {
|
|
88
97
|
if (!view.activity || config.activityRouting === 'off') return null;
|
|
89
|
-
const
|
|
90
|
-
return
|
|
98
|
+
const verb = activityVerb(view.activity);
|
|
99
|
+
return config.activityRouting === 'shadow'
|
|
100
|
+
? { ...part(`${verb} (shadow) → `, { dimColor: true }), priority: 2 }
|
|
101
|
+
: { ...part(`${verb} → `, { color: COLOR.activity, italic: true }), priority: 2 };
|
|
91
102
|
}
|
|
92
103
|
|
|
93
104
|
function served(view) {
|
|
@@ -155,7 +166,8 @@ export function bandSegments(config, view, usage, actions) {
|
|
|
155
166
|
return [segment(0, meter(null, true), part(' Routing on · ready', { dimColor: true })), pin].filter(Boolean);
|
|
156
167
|
const main = segment(
|
|
157
168
|
0,
|
|
158
|
-
|
|
169
|
+
// The space after the pill keeps its background off the next word.
|
|
170
|
+
view.tier ? [...meter(view.tier), pill(view.tier, TIER_COLOR[view.tier]), part(' ')] : part('○ '),
|
|
159
171
|
activityPart(config, view),
|
|
160
172
|
part(route(view)),
|
|
161
173
|
served(view) ? part(' fallback', { color: 'yellow' }) : null,
|
|
@@ -259,7 +271,16 @@ function hoverSegments(view, actions, routing) {
|
|
|
259
271
|
export function renderBand({ Box, Text, Button }, config, view, usage, { columns, agentId }, actions) {
|
|
260
272
|
const draw = (p) => (p.button ? Button(p.button) : Text({ ...p.style, children: p.text }));
|
|
261
273
|
const row = (parts) => Box({ children: parts.map(draw) });
|
|
262
|
-
|
|
274
|
+
// A subagent's model is chosen once, at spawn, and never changes between its steps.
|
|
275
|
+
if (agentId)
|
|
276
|
+
return row([
|
|
277
|
+
part(
|
|
278
|
+
config.subagentRouting === 'on'
|
|
279
|
+
? '○ Router · subagent model chosen at spawn'
|
|
280
|
+
: '○ Router · subagents keep their own model',
|
|
281
|
+
{ dimColor: true },
|
|
282
|
+
),
|
|
283
|
+
]);
|
|
263
284
|
const kept = fitSegments(bandSegments(config, view, usage, actions), Math.max(0, columns - ROUTER_BUTTON));
|
|
264
285
|
const line = kept.flatMap((s, i) => (i ? [part(SEPARATOR, { dimColor: true }), ...s.parts] : s.parts));
|
|
265
286
|
line.push(part(' '), button({ key: 'details', label: 'Router', plain: true, onPress: actions.open }));
|
|
@@ -283,7 +304,7 @@ export function renderBand({ Box, Text, Button }, config, view, usage, { columns
|
|
|
283
304
|
export function switchToast(config, previous, next, estimate) {
|
|
284
305
|
const label = (decision) => routeLabel(decision.model, decision.effort);
|
|
285
306
|
const support = supportText(config, estimate);
|
|
286
|
-
// An activity move names the activity: "Model changed: Sonnet 5.5 · medium → Haiku 5.5 · high ·
|
|
307
|
+
// An activity move names the activity: "Model changed: Sonnet 5.5 · medium → Haiku 5.5 · high · running".
|
|
287
308
|
const why = shortReason(next.reason, next.activity).replace(/^\W+\s*/, '');
|
|
288
309
|
return `Model changed: ${label(previous)} → ${label(next)} · ${why}${support ? ` (${support})` : ''}`;
|
|
289
310
|
}
|
|
@@ -19,10 +19,13 @@ class AdviceError extends Error {
|
|
|
19
19
|
}
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
-
// Owned by one Mod activation. It survives history resets; the host cancels its work on unload.
|
|
22
|
+
// Owned by one Mod activation. It survives history resets; the host cancels its work on unload. An exclusive client
|
|
23
|
+
// (the main turn's) answers `busy` while a request is in flight, so a late answer never overlaps the next turn's; the
|
|
24
|
+
// spawn path's client is not exclusive, so a parent that spawns several agents at once gets an answer for each.
|
|
23
25
|
export class ClassifierClient {
|
|
24
|
-
constructor({ now = Date.now, health = null } = {}) {
|
|
26
|
+
constructor({ now = Date.now, health = null, exclusive = true } = {}) {
|
|
25
27
|
this.now = now;
|
|
28
|
+
this.exclusive = exclusive;
|
|
26
29
|
this.pending = null;
|
|
27
30
|
this.active = false;
|
|
28
31
|
this.restore(health);
|
|
@@ -43,7 +46,7 @@ export class ClassifierClient {
|
|
|
43
46
|
if (!endpoint) return { advice: null, error: 'missing-account' };
|
|
44
47
|
if (signal?.aborted) return { advice: null, error: 'cancelled' };
|
|
45
48
|
if (this.now() < this.pausedUntil) return { advice: null, error: 'paused' };
|
|
46
|
-
if (this.active || this.pending) return { advice: null, error: 'busy' };
|
|
49
|
+
if (this.exclusive && (this.active || this.pending)) return { advice: null, error: 'busy' };
|
|
47
50
|
this.active = true;
|
|
48
51
|
try {
|
|
49
52
|
const advice = await this.complete({ request, sleep, config, apiKey, endpoint, prompt, turns, signal });
|
package/lib/config.mjs
CHANGED
|
@@ -8,6 +8,11 @@ export const ACTIVITY_VALUES = [...ACTIVITIES, 'uncertain'];
|
|
|
8
8
|
// `off` asks nothing new and routes as 1.5; `shadow` asks and records but keeps the 1.5 route; `on`, the default since
|
|
9
9
|
// 1.7, applies overrides.
|
|
10
10
|
export const ACTIVITY_MODES = ['off', 'shadow', 'on'];
|
|
11
|
+
// `subagentRouting` takes the same modes: `off` asks and records nothing, `shadow` decides and records but keeps core's
|
|
12
|
+
// model, `on` applies the decision at agent.spawn.
|
|
13
|
+
export const SUBAGENT_MODES = ACTIVITY_MODES;
|
|
14
|
+
// A `subagents.types` value that asks the classifier instead of naming a model.
|
|
15
|
+
export const CLASSIFY = 'classify';
|
|
11
16
|
const SONNET_HIGH = { model: 'sonnet', effort: 'high' };
|
|
12
17
|
export const DEFAULTS = {
|
|
13
18
|
baselineTier: 'low',
|
|
@@ -38,6 +43,20 @@ export const DEFAULTS = {
|
|
|
38
43
|
review: { low: SONNET_HIGH },
|
|
39
44
|
ops: { medium: { model: 'haiku', effort: 'high' } },
|
|
40
45
|
},
|
|
46
|
+
// Subagents get one model at agent.spawn, never between their steps. An agent whose definition names no model, says
|
|
47
|
+
// `inherit`, or sets `modelRouting: auto` gets `light`, `standard` or `heavy` from the classifier; a pinned one keeps
|
|
48
|
+
// its model. `types` overrides by agent type: a models key, `classify`, or null to never route it. A key without a
|
|
49
|
+
// colon matches only the built-in of that name, whose definition is not readable, so only the listed built-ins route.
|
|
50
|
+
// A listed type is routed even if it pins a model. docs/plans/2026-10-10-subagent-routing-plan.md.
|
|
51
|
+
subagentRouting: 'on',
|
|
52
|
+
subagents: {
|
|
53
|
+
types: { Explore: 'haiku', Plan: CLASSIFY, 'general-purpose': CLASSIFY },
|
|
54
|
+
light: 'haiku',
|
|
55
|
+
standard: 'sonnet',
|
|
56
|
+
heavy: 'opus',
|
|
57
|
+
// The classifier's probability of `high` a task needs to run on `heavy`.
|
|
58
|
+
heavyMass: 0.8,
|
|
59
|
+
},
|
|
41
60
|
// `id` selects the native model. List prices in USD per million tokens; `cacheRead` is absolute,
|
|
42
61
|
// not a multiplier (Opus 5.5 reads at 0.05x input, the rest at the standard 0.1x). `longContext` multiplies every
|
|
43
62
|
// rate of a request whose prompt (input, cache read and cache write) is over `above` tokens: Haiku 5.5 lists its
|
|
@@ -426,7 +445,7 @@ function checkShape(file) {
|
|
|
426
445
|
if (file && typeof file === 'object' && Object.hasOwn(file, 'jev'))
|
|
427
446
|
throw new Error(`router.json uses the 1.1 jev section; ${MIGRATION_HINT}`);
|
|
428
447
|
checkObject(file, 'router.json', Object.keys(DEFAULTS));
|
|
429
|
-
const { routes, activities, models, cache, policy, classifiers, context } = file;
|
|
448
|
+
const { routes, activities, models, cache, policy, classifiers, context, subagents } = file;
|
|
430
449
|
if (routes !== undefined) {
|
|
431
450
|
checkObject(routes, 'routes', TIERS);
|
|
432
451
|
for (const [tier, route] of Object.entries(routes)) checkObject(route, `routes.${tier}`, ROUTE_KEYS);
|
|
@@ -459,6 +478,10 @@ function checkShape(file) {
|
|
|
459
478
|
for (const [id, entry] of Object.entries(classifiers)) checkObject(entry, `classifiers.${id}`, CLASSIFIER_KEYS);
|
|
460
479
|
}
|
|
461
480
|
if (context !== undefined) checkObject(context, 'context', Object.keys(DEFAULTS.context));
|
|
481
|
+
if (subagents !== undefined) {
|
|
482
|
+
checkObject(subagents, 'subagents', Object.keys(DEFAULTS.subagents));
|
|
483
|
+
if (subagents.types !== undefined) checkObject(subagents.types, 'subagents.types');
|
|
484
|
+
}
|
|
462
485
|
}
|
|
463
486
|
|
|
464
487
|
// `allowed` null: an open map.
|
|
@@ -518,6 +541,19 @@ function validate(config) {
|
|
|
518
541
|
for (const [id, entry] of Object.entries(config.classifiers)) validateClassifier(`classifiers.${id}`, entry);
|
|
519
542
|
if (typeof config.classifier !== 'string' || !Object.hasOwn(config.classifiers, config.classifier))
|
|
520
543
|
throw new Error('classifier is not in classifiers');
|
|
544
|
+
if (!SUBAGENT_MODES.includes(config.subagentRouting))
|
|
545
|
+
throw new Error(`subagentRouting must be one of ${SUBAGENT_MODES.join(', ')}`);
|
|
546
|
+
const { subagents } = config;
|
|
547
|
+
for (const [type, route] of Object.entries(subagents.types)) {
|
|
548
|
+
if (route !== null && route !== CLASSIFY && !(typeof route === 'string' && Object.hasOwn(config.models, route)))
|
|
549
|
+
throw new Error(`subagents.types.${type} must be a models key, ${CLASSIFY} or null`);
|
|
550
|
+
}
|
|
551
|
+
for (const field of ['light', 'standard', 'heavy']) {
|
|
552
|
+
if (!(typeof subagents[field] === 'string' && Object.hasOwn(config.models, subagents[field])))
|
|
553
|
+
throw new Error(`subagents.${field} is not in models`);
|
|
554
|
+
}
|
|
555
|
+
if (!(isNumber(subagents.heavyMass, 0) && subagents.heavyMass <= 1))
|
|
556
|
+
throw new Error('subagents.heavyMass must be between 0 and 1');
|
|
521
557
|
for (const field of ['recentTurns', 'maxTextChars']) {
|
|
522
558
|
if (!isNumber(context[field], 1, { integer: true })) throw new Error(`context.${field} must be a positive integer`);
|
|
523
559
|
}
|
package/lib/cost.mjs
CHANGED
|
@@ -1,7 +1,18 @@
|
|
|
1
1
|
import { EFFORTS, routeSpec } from './config.mjs';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
// Models that keep one prompt cache across effort levels with an API key or a Claude subscription
|
|
4
|
+
// (code.claude.com/docs/en/prompt-caching, "Changing effort level"). A dated snapshot or a `[1m]` suffix is the same
|
|
5
|
+
// model. `config.effortSplitsCache`, set by the hook from the session's provider, gives every effort its own cache.
|
|
6
|
+
const EFFORT_SHARED_CACHE = ['claude-opus-5-5', 'claude-sonnet-5-5', 'claude-haiku-5-5', 'claude-fable-5-1'];
|
|
7
|
+
|
|
8
|
+
export function effortSharesCache(config, modelId) {
|
|
9
|
+
if (config.effortSplitsCache || typeof modelId !== 'string') return false;
|
|
10
|
+
const bare = modelId.replace(/\[[^\]]*\]$/, '').replace(/-\d{8}$/, '');
|
|
11
|
+
return EFFORT_SHARED_CACHE.includes(bare);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function cacheKey(config, modelId, effort) {
|
|
15
|
+
return effort && !effortSharesCache(config, modelId) ? `${modelId}@${effort}` : modelId;
|
|
5
16
|
}
|
|
6
17
|
|
|
7
18
|
export function clampEffort(wanted, supported) {
|
|
@@ -38,7 +49,7 @@ export function routeEffort(config, route, sentEffort) {
|
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
export function routeCacheKey(config, route, sentEffort) {
|
|
41
|
-
return cacheKey(routeSpec(config, route).id, routeEffort(config, route, sentEffort));
|
|
52
|
+
return cacheKey(config, routeSpec(config, route).id, routeEffort(config, route, sentEffort));
|
|
42
53
|
}
|
|
43
54
|
|
|
44
55
|
export function isWarm(state, now, cache) {
|
package/lib/display.mjs
CHANGED
|
@@ -167,9 +167,21 @@ export const SHORT_REASONS = {
|
|
|
167
167
|
'activity-pending': '… {activity}: not worth a switch',
|
|
168
168
|
};
|
|
169
169
|
|
|
170
|
+
// What the band calls each activity: the verb for the work, as Claude Code names a turn's action.
|
|
171
|
+
const ACTIVITY_VERB = {
|
|
172
|
+
code: 'coding',
|
|
173
|
+
debug: 'debugging',
|
|
174
|
+
explore: 'exploring',
|
|
175
|
+
plan: 'planning',
|
|
176
|
+
review: 'reviewing',
|
|
177
|
+
ops: 'running',
|
|
178
|
+
docs: 'documenting',
|
|
179
|
+
};
|
|
180
|
+
export const activityVerb = (activity) => ACTIVITY_VERB[activity] ?? activity;
|
|
181
|
+
|
|
170
182
|
// The band's words for `reason`; an activity move names the activity it is about, `base` for the tier's own route.
|
|
171
183
|
export const shortReason = (reason, activity) =>
|
|
172
|
-
(SHORT_REASONS[reason] ?? reason ?? '').replace('{activity}', activity
|
|
184
|
+
(SHORT_REASONS[reason] ?? reason ?? '').replace('{activity}', activity ? activityVerb(activity) : 'base');
|
|
173
185
|
|
|
174
186
|
// The activity a reason is about: the applied one for a move, and for a refused move the classifier's activity when
|
|
175
187
|
// it carries enough mass to apply, else null (the tier's base route).
|
package/lib/panel.mjs
CHANGED
|
@@ -13,7 +13,7 @@ import {
|
|
|
13
13
|
TIERS,
|
|
14
14
|
tuningOf,
|
|
15
15
|
} from './config.mjs';
|
|
16
|
-
import { routeEffort } from './cost.mjs';
|
|
16
|
+
import { effortSharesCache, routeEffort } from './cost.mjs';
|
|
17
17
|
import {
|
|
18
18
|
ACTIVITY_LETTER,
|
|
19
19
|
activityReading,
|
|
@@ -57,6 +57,7 @@ import {
|
|
|
57
57
|
readout,
|
|
58
58
|
readSavingsStore,
|
|
59
59
|
} from './savings.mjs';
|
|
60
|
+
import { PASS_REASONS, readSubagentStore } from './subagents.mjs';
|
|
60
61
|
|
|
61
62
|
const TABS = [
|
|
62
63
|
['now', '1', 'Now'],
|
|
@@ -79,6 +80,12 @@ const ACTIVITY_MODE_HINT = {
|
|
|
79
80
|
shadow: 'shows what on would do; base routes run',
|
|
80
81
|
on: 'an override runs when the activity has one',
|
|
81
82
|
};
|
|
83
|
+
// What `subagentRouting` does, under the router.json path: it is set in the file, not in the pane.
|
|
84
|
+
const SUBAGENT_LINE = {
|
|
85
|
+
off: 'Subagents keep their own model (subagentRouting off).',
|
|
86
|
+
shadow: 'Subagents: shadow. A model is chosen at spawn and recorded, not applied.',
|
|
87
|
+
on: 'Subagents: on. Agents that pin no model get one at spawn, kept for every step.',
|
|
88
|
+
};
|
|
82
89
|
const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max: 'max' };
|
|
83
90
|
|
|
84
91
|
// The route draft the Routing tab edits: the saved routes, activity overrides and mode until the person changes one.
|
|
@@ -438,7 +445,7 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
|
|
|
438
445
|
),
|
|
439
446
|
ui.text(' '),
|
|
440
447
|
ui.head('SWITCH COST · from these routes'),
|
|
441
|
-
...switchCost(ui, draft.routes),
|
|
448
|
+
...switchCost(ui, config, draft.routes),
|
|
442
449
|
ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
|
|
443
450
|
ui.dim('A new model ID needs router.json → models: price, window, efforts.'),
|
|
444
451
|
ui.text(' '),
|
|
@@ -463,12 +470,12 @@ function routingTab(ui, config, view, _usage, actions, modelOptions) {
|
|
|
463
470
|
`File ${view.configPath ?? 'profile router.json'} `,
|
|
464
471
|
view.configPath ? ui.Button({ key: 'copy-path', label: 'copy', onPress: actions.copyPath }) : null,
|
|
465
472
|
),
|
|
466
|
-
ui.dim(
|
|
473
|
+
ui.dim(SUBAGENT_LINE[config.subagentRouting]),
|
|
467
474
|
);
|
|
468
475
|
return out;
|
|
469
476
|
}
|
|
470
477
|
|
|
471
|
-
function switchCost(ui, routes) {
|
|
478
|
+
function switchCost(ui, config, routes) {
|
|
472
479
|
return LADDER.slice(0, -1).map((upperTier, i) => {
|
|
473
480
|
const upper = routes[upperTier];
|
|
474
481
|
const lower = routes[LADDER[i + 1]];
|
|
@@ -476,7 +483,13 @@ function switchCost(ui, routes) {
|
|
|
476
483
|
if (sameRoute(upper, lower))
|
|
477
484
|
return ui.line(ui.text('! ', { color: 'yellow' }), pair, 'identical: this step changes nothing');
|
|
478
485
|
if (upper.model === lower.model)
|
|
479
|
-
return ui.line(
|
|
486
|
+
return ui.line(
|
|
487
|
+
ui.dim('· '),
|
|
488
|
+
pair,
|
|
489
|
+
sharesEffort(config, upper)
|
|
490
|
+
? 'same model, new effort: keeps its cache'
|
|
491
|
+
: 'same model, new effort: priced as a new cache',
|
|
492
|
+
);
|
|
480
493
|
return ui.line(ui.dim('· '), pair, 'model change: priced as a cold cache write');
|
|
481
494
|
});
|
|
482
495
|
}
|
|
@@ -488,6 +501,9 @@ function shortRoute(config, route) {
|
|
|
488
501
|
return `${modelName(model.id)[0]}${effort}`;
|
|
489
502
|
}
|
|
490
503
|
const routeKey = (route) => `${route.model}@${route.effort ?? ''}`;
|
|
504
|
+
const sharesEffort = (config, route) => effortSharesCache(config, config.models[route.model]?.id);
|
|
505
|
+
// The cache a route runs on: one per model on a model that keeps one cache across efforts, else one per route.
|
|
506
|
+
const cacheOf = (config, route) => (sharesEffort(config, route) ? route.model : routeKey(route));
|
|
491
507
|
|
|
492
508
|
// Why an override may be a mistake: no effect, a route above the next tier's, or a pair no other cell runs.
|
|
493
509
|
function overrideWarnings(config, uses, activity, tier) {
|
|
@@ -496,7 +512,9 @@ function overrideWarnings(config, uses, activity, tier) {
|
|
|
496
512
|
const above = config.routes[TIERS[TIERS.indexOf(tier) + 1]];
|
|
497
513
|
return [
|
|
498
514
|
above && compareRoutes(config, route, above) > 0 ? 'stronger than the tier above' : null,
|
|
499
|
-
uses.get(
|
|
515
|
+
uses.get(cacheOf(config, route)) === 1
|
|
516
|
+
? `${sharesEffort(config, route) ? 'new model' : 'new (model, effort) pair'}: one more cache`
|
|
517
|
+
: null,
|
|
500
518
|
].filter(Boolean);
|
|
501
519
|
}
|
|
502
520
|
|
|
@@ -505,9 +523,13 @@ function activitySections(ui, config, view, changes, actions, modelOptions) {
|
|
|
505
523
|
const draft = { ...config, ...changes.draft };
|
|
506
524
|
const mode = draft.activityRouting;
|
|
507
525
|
const cellRoutes = ACTIVITIES.flatMap((activity) => TIERS.map((tier) => resolveRoute(draft, tier, activity)));
|
|
526
|
+
// Uses per cache, for the one-more-cache warning, and the distinct routes.
|
|
508
527
|
const uses = new Map();
|
|
509
|
-
|
|
510
|
-
|
|
528
|
+
const routeKeys = new Set();
|
|
529
|
+
for (const route of [...TIERS.map((tier) => draft.routes[tier]), ...cellRoutes]) {
|
|
530
|
+
uses.set(cacheOf(draft, route), (uses.get(cacheOf(draft, route)) ?? 0) + 1);
|
|
531
|
+
routeKeys.add(routeKey(route));
|
|
532
|
+
}
|
|
511
533
|
// Activities with the same row share it, so the matrix stays as short as the overrides are few.
|
|
512
534
|
const rows = new Map();
|
|
513
535
|
for (const activity of ACTIVITIES) {
|
|
@@ -518,7 +540,7 @@ function activitySections(ui, config, view, changes, actions, modelOptions) {
|
|
|
518
540
|
const row = rows.get(cells.join()) ?? { names: [], cells };
|
|
519
541
|
rows.set(cells.join(), { ...row, names: [...row.names, activity] });
|
|
520
542
|
}
|
|
521
|
-
const distinct = uses.size;
|
|
543
|
+
const [distinct, caches] = [routeKeys.size, uses.size];
|
|
522
544
|
const matrixRow = (name, cells) => ui.text(` ${name.padEnd(30)}${cells.map((c) => c.padEnd(9)).join('')}`);
|
|
523
545
|
// The mode leads the section: it decides whether the matrix below runs or is only shown.
|
|
524
546
|
const out = [
|
|
@@ -546,7 +568,7 @@ function activitySections(ui, config, view, changes, actions, modelOptions) {
|
|
|
546
568
|
),
|
|
547
569
|
...[...rows.values()].map(({ names, cells }) => matrixRow(names.join(' '), cells)),
|
|
548
570
|
ui.dim(
|
|
549
|
-
` ${distinct} distinct route${distinct === 1 ? '' : 's'}
|
|
571
|
+
` ${distinct} distinct route${distinct === 1 ? '' : 's'} · ${caches} cache${caches === 1 ? '' : 's'}` +
|
|
550
572
|
`${mode === 'on' ? '' : ' once activity routing is on'}`,
|
|
551
573
|
),
|
|
552
574
|
ui.text(' '),
|
|
@@ -1144,6 +1166,56 @@ function savingsSection(ui, config, view, columns) {
|
|
|
1144
1166
|
return [...out, ...notes()];
|
|
1145
1167
|
}
|
|
1146
1168
|
|
|
1169
|
+
// Prices summed over the priced requests, and how many were not priced (a model without a price in router.json).
|
|
1170
|
+
const pricedText = (text, counts) =>
|
|
1171
|
+
counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
|
|
1172
|
+
|
|
1173
|
+
// Subagent routing across sessions since the last reset. Per routed type: agents, how many the decision moved off
|
|
1174
|
+
// core's model, requests per agent and respawns (the retry proxies), and core's cost against the routed one at list
|
|
1175
|
+
// prices. Shadow and on count apart: shadow prices the decision on the agent's own tokens, on applies it and estimates
|
|
1176
|
+
// core's model. Then the spawns that passed through, by reason, priced on the model they ran on.
|
|
1177
|
+
function subagentUsage(ui, config, view, columns) {
|
|
1178
|
+
const mode = config.subagentRouting;
|
|
1179
|
+
const store = readSubagentStore(view.subagentStore);
|
|
1180
|
+
const out = [ui.line(ui.head('SUBAGENTS · across sessions'), ui.dim(` subagent routing ${mode}`))];
|
|
1181
|
+
const sections = ['shadow', 'on']
|
|
1182
|
+
.map((m) => ({ m, rows: Object.entries(store.routed[m]).filter(([, counts]) => counts.agents > 0) }))
|
|
1183
|
+
.filter(({ rows }) => rows.length);
|
|
1184
|
+
const passed = PASS_REASONS.filter((reason) => store.passed[reason]?.agents > 0);
|
|
1185
|
+
if (!sections.length && !passed.length)
|
|
1186
|
+
return [
|
|
1187
|
+
...out,
|
|
1188
|
+
ui.dim(
|
|
1189
|
+
mode === 'off' ? ' Not recorded. Set subagentRouting to shadow or on in router.json.' : ' no subagents yet',
|
|
1190
|
+
),
|
|
1191
|
+
];
|
|
1192
|
+
for (const { m, rows } of sections) {
|
|
1193
|
+
out.push(
|
|
1194
|
+
ui.dim(m === 'shadow' ? ' shadow: core’s model → the decided one' : ' on: core’s model (est.) → routed'),
|
|
1195
|
+
);
|
|
1196
|
+
for (const [type, c] of rows.sort((a, b) => b[1].agents - a[1].agents)) {
|
|
1197
|
+
const cost = pricedText(`${usd(c.coreUsd)} → ${usd(c.routedUsd)}`, c);
|
|
1198
|
+
out.push(
|
|
1199
|
+
ui.text(
|
|
1200
|
+
` ${type} ${c.agents} agent${c.agents === 1 ? '' : 's'} · ${c.differs} moved · ${(c.requests / c.agents).toFixed(1)} req/agent · ${c.respawns} respawns · ${cost}`,
|
|
1201
|
+
),
|
|
1202
|
+
);
|
|
1203
|
+
const choices = Object.entries(c.choices).map(([choice, n]) => `${choice} ${n}`);
|
|
1204
|
+
if (c.failures) choices.push(`${c.failures} classifier failures`);
|
|
1205
|
+
out.push(...dimLines(ui, ` ${choices.join(' · ')}`, columns));
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (passed.length) {
|
|
1209
|
+
const sum = (field) => passed.reduce((total, reason) => total + store.passed[reason][field], 0);
|
|
1210
|
+
const total = { requests: sum('requests'), pricedRequests: sum('pricedRequests') };
|
|
1211
|
+
out.push(
|
|
1212
|
+
ui.text(` Not routed ${passed.map((reason) => `${reason} ${store.passed[reason].agents}`).join(' · ')}`),
|
|
1213
|
+
ui.dim(` ${pricedText(`${usd(sum('usd'))} on the model they ran on`, total)}`),
|
|
1214
|
+
);
|
|
1215
|
+
}
|
|
1216
|
+
return out;
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1147
1219
|
function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
|
|
1148
1220
|
const metrics = usageMetrics(config, view, usage);
|
|
1149
1221
|
const history = view.history ?? [];
|
|
@@ -1178,6 +1250,8 @@ function usageTab(ui, config, view, usage, actions, _modelOptions, columns) {
|
|
|
1178
1250
|
ui.text(' '),
|
|
1179
1251
|
...activityUsage(ui, config, view, actions, columns),
|
|
1180
1252
|
ui.text(' '),
|
|
1253
|
+
...subagentUsage(ui, config, view, columns),
|
|
1254
|
+
ui.text(' '),
|
|
1181
1255
|
ui.head('ESTIMATES · configured list prices'),
|
|
1182
1256
|
ui.text(`Cache hits saved ${dollars(metrics.cacheBenefit)}`),
|
|
1183
1257
|
Number.isFinite(view.estimate?.taxUsd) ? ui.text(`Switch cost ${dollars(view.estimate.taxUsd)}`) : null,
|
package/lib/route.mjs
CHANGED
|
@@ -317,7 +317,7 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
|
|
|
317
317
|
return { ...loop, decision };
|
|
318
318
|
}
|
|
319
319
|
|
|
320
|
-
export function observeResponse(loop, { usage, requestedModel, effort, stopReason, now }) {
|
|
320
|
+
export function observeResponse(config, loop, { usage, requestedModel, effort, stopReason, now }) {
|
|
321
321
|
let next = loop;
|
|
322
322
|
if (stopReason === 'model_context_window_exceeded') {
|
|
323
323
|
next = { ...next, ineligible: [...new Set([...next.ineligible, requestedModel])] };
|
|
@@ -337,7 +337,7 @@ export function observeResponse(loop, { usage, requestedModel, effort, stopReaso
|
|
|
337
337
|
if (sameModel)
|
|
338
338
|
models = {
|
|
339
339
|
...models,
|
|
340
|
-
[costs.cacheKey(usage.model, effort)]: {
|
|
340
|
+
[costs.cacheKey(config, usage.model, effort)]: {
|
|
341
341
|
lastAt: now,
|
|
342
342
|
prefixTokens: usage.cache_read_input_tokens + usage.cache_creation_input_tokens,
|
|
343
343
|
},
|
package/lib/savings.mjs
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
// the same tokens on the session's own model and effort ("your model"). Counts and dollars only, never text. Pure:
|
|
3
3
|
// the hook feeds each reply and keeps the totals in the view and, per session, in $.store under SAVINGS_PREFIX.
|
|
4
4
|
|
|
5
|
-
import { ratesAt } from './cost.mjs';
|
|
5
|
+
import { effortSharesCache, ratesAt } from './cost.mjs';
|
|
6
6
|
import { HISTORY_SHRINK, isSameModel } from './route.mjs';
|
|
7
7
|
|
|
8
8
|
// Each session keeps its own record under this prefix and its session id: $.store has no atomic update, and Claude
|
|
@@ -59,6 +59,12 @@ export function sessionCacheTtl({ force5m, envTtl, settingTtl, enable1h, subscri
|
|
|
59
59
|
return '5m';
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
// A subagent's prompt-cache lifetime, by Claude Code's rule for requests outside the main conversation: the same order
|
|
63
|
+
// with CLAUDE_CODE_SUBAGENT_PROMPT_CACHE_TTL and `subagentPromptCacheTtl`, and five minutes on a subscription too.
|
|
64
|
+
// Limit: an agent's `experimental.cacheTtl` frontmatter is not visible to a Mod.
|
|
65
|
+
export const subagentCacheTtl = ({ force5m, envTtl, settingTtl, enable1h }) =>
|
|
66
|
+
sessionCacheTtl({ force5m, envTtl, settingTtl, enable1h, subscriber: false });
|
|
67
|
+
|
|
62
68
|
const isCount = (n) => Number.isFinite(n) && n >= 0;
|
|
63
69
|
|
|
64
70
|
// One main reply. `state` is your model's simulated cache: `{ total, at, onYours, model, effort }` after a reply, or
|
|
@@ -87,11 +93,13 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
|
|
|
87
93
|
const total = counts.input + counts.cacheRead + counts.cacheWrite;
|
|
88
94
|
const [servedSpec, yoursSpec] = [modelSpec(config, served.model), modelSpec(config, yours.model)];
|
|
89
95
|
const sameModel = servedSpec ? servedSpec === yoursSpec : served.model === yours.model;
|
|
90
|
-
const onYours = sameModel && (served.effort ?? null) === (yours.effort ?? null);
|
|
91
96
|
const identity = { model: yoursSpec?.id ?? yours.model, effort: yours.effort ?? null };
|
|
97
|
+
// On a model that keeps one cache across efforts (effortSharesCache), an effort-only difference is the same cache.
|
|
98
|
+
const sharedEffort = effortSharesCache(config, identity.model);
|
|
99
|
+
const onYours = sameModel && (sharedEffort || (served.effort ?? null) === identity.effort);
|
|
92
100
|
const next = { total, at: now, onYours, ...identity };
|
|
93
101
|
// A cache simulated for another model or effort of yours is none of this one's: the new one starts cold.
|
|
94
|
-
const prior = state?.model === identity.model && state.effort === identity.effort ? state : null;
|
|
102
|
+
const prior = state?.model === identity.model && (sharedEffort || state.effort === identity.effort) ? state : null;
|
|
95
103
|
const write = config.cache.writeMultiplier[ttl];
|
|
96
104
|
if (
|
|
97
105
|
!servedSpec ||
|