@alexeiled/claude-router 1.23.1 → 1.25.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 +1 -1
- package/CHANGELOG.md +62 -1
- package/README.md +39 -7
- package/docs/architecture.md +27 -4
- package/docs/auto-engine.md +152 -0
- package/docs/configuration.md +47 -3
- package/docs/evaluation.md +222 -3
- package/docs/native-router.md +11 -3
- package/docs/providers.md +28 -0
- package/docs/router-band.svg +124 -133
- package/docs/router-pane-classifier.svg +24 -23
- package/docs/router-pane-now.svg +9 -7
- package/docs/router-pane-policy.svg +4 -4
- package/docs/router-pane-routes.svg +8 -8
- package/docs/router-pane-usage-whatif.svg +14 -14
- package/docs/router-pane-usage.svg +20 -20
- package/docs/routing-modes.md +13 -4
- package/docs/user-guide.md +79 -19
- package/hooks/native-router.mjs +1194 -215
- package/lib/activity-stats.mjs +23 -4
- package/lib/activity.mjs +7 -5
- package/lib/auto-engine.mjs +414 -0
- package/lib/auto-route.mjs +253 -0
- package/lib/band.mjs +35 -440
- package/lib/classifier-client.mjs +12 -8
- package/lib/config-file.mjs +7 -2
- package/lib/config.mjs +64 -15
- package/lib/cost.mjs +5 -1
- package/lib/costs.mjs +4 -2
- package/lib/decisions.mjs +80 -5
- package/lib/display.mjs +69 -20
- package/lib/facts.mjs +30 -0
- package/lib/modes.mjs +8 -4
- package/lib/panel.mjs +75 -2163
- package/lib/policy.mjs +7 -6
- package/lib/providers.mjs +7 -0
- package/lib/replay-trace.mjs +550 -0
- package/lib/replay.mjs +718 -0
- package/lib/route-line.mjs +46 -0
- package/lib/route.mjs +35 -21
- package/lib/savings.mjs +14 -3
- package/lib/subagents.mjs +41 -11
- package/lib/subcommands.mjs +60 -0
- package/lib/ui/fallback.mjs +22 -0
- package/lib/ui/graphical/band.mjs +256 -0
- package/lib/ui/graphical/charts.mjs +163 -0
- package/lib/ui/graphical/classifier.mjs +153 -0
- package/lib/ui/graphical/frame.mjs +227 -0
- package/lib/ui/graphical/kit.mjs +156 -0
- package/lib/ui/graphical/meter.mjs +25 -0
- package/lib/ui/graphical/now.mjs +353 -0
- package/lib/ui/graphical/policy.mjs +56 -0
- package/lib/ui/graphical/routes.mjs +264 -0
- package/lib/ui/graphical/squares.mjs +29 -0
- package/lib/ui/graphical/usage.mjs +376 -0
- package/lib/ui/model/band.mjs +439 -0
- package/lib/ui/model/classifier.mjs +308 -0
- package/lib/ui/model/draft.mjs +62 -0
- package/lib/ui/model/format.mjs +41 -0
- package/lib/ui/model/frame.mjs +359 -0
- package/lib/ui/model/now.mjs +390 -0
- package/lib/ui/model/policy.mjs +194 -0
- package/lib/ui/model/routes.mjs +392 -0
- package/lib/ui/model/usage-agents.mjs +311 -0
- package/lib/ui/model/usage-breakdown.mjs +237 -0
- package/lib/ui/model/usage-shared.mjs +58 -0
- package/lib/ui/model/usage.mjs +330 -0
- package/lib/ui/terminal/band.mjs +220 -0
- package/lib/ui/terminal/classifier.mjs +142 -0
- package/lib/ui/terminal/frame.mjs +169 -0
- package/lib/ui/terminal/kit.mjs +133 -0
- package/lib/ui/terminal/now.mjs +300 -0
- package/lib/ui/terminal/policy.mjs +52 -0
- package/lib/ui/terminal/routes.mjs +167 -0
- package/lib/ui/terminal/usage.mjs +363 -0
- package/lib/ui/terminal/wrap.mjs +37 -0
- package/lib/ui/tokens.mjs +106 -0
- package/lib/usage.mjs +5 -5
- package/lib/view.mjs +5 -2
- package/package.json +3 -2
- package/types/index.d.ts +15 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
// The Usage tab's subagents: the strip of every agent by model, the trend, and the routed and passed-through types.
|
|
2
|
+
import { costText, modelName, percent, signedUsd } from '../../display.mjs';
|
|
3
|
+
import {
|
|
4
|
+
modelTotals,
|
|
5
|
+
PASS_LABEL,
|
|
6
|
+
PASS_REASONS,
|
|
7
|
+
PASS_WHY,
|
|
8
|
+
readSubagentStore,
|
|
9
|
+
summarizeSubagents,
|
|
10
|
+
} from '../../subagents.mjs';
|
|
11
|
+
import { dayKey, periodRange } from '../../usage.mjs';
|
|
12
|
+
import { modelTone as familyTone, savingTone } from '../tokens.mjs';
|
|
13
|
+
|
|
14
|
+
import { dayLabel, plural, sentence } from './format.mjs';
|
|
15
|
+
import { cents, chartOf, NO_DIFF, PERIOD_PHRASE, pricedText, saving, spanText } from './usage-shared.mjs';
|
|
16
|
+
|
|
17
|
+
/** @typedef {import('../tokens.mjs').Tone} Tone */
|
|
18
|
+
/** @typedef {{ text: string, tone: Tone }} Toned */
|
|
19
|
+
|
|
20
|
+
// A subagent row's squares at most: past this the rest is a count.
|
|
21
|
+
const ROW_SQUARES = 9;
|
|
22
|
+
// The summary strip's squares at most: past this each model gets its share of them, not one per agent.
|
|
23
|
+
const AGENT_STRIP = 25;
|
|
24
|
+
// The models in the order their squares go, by family; any other after them by name.
|
|
25
|
+
const FAMILIES = ['haiku', 'sonnet', 'opus', 'fable'];
|
|
26
|
+
|
|
27
|
+
// Why Claude Code picked a pass-through's model, on a card, where the terminal's words name a router.json key.
|
|
28
|
+
const PASS_CARD_WHY = { unlisted: 'Not among the subagent types the settings file routes' };
|
|
29
|
+
|
|
30
|
+
const familyRank = (id) => {
|
|
31
|
+
const rank = FAMILIES.findIndex((family) => id.includes(family));
|
|
32
|
+
return rank < 0 ? FAMILIES.length : rank;
|
|
33
|
+
};
|
|
34
|
+
const byFamily = (a, b) => familyRank(a[0]) - familyRank(b[0]) || (a[0] < b[0] ? -1 : a[0] > b[0] ? 1 : 0);
|
|
35
|
+
// Each model's agents in a period and how many the router moved, across the routed types and the pass-throughs.
|
|
36
|
+
function agentsByModel(s) {
|
|
37
|
+
const models = new Map();
|
|
38
|
+
for (const kind of ['routed', 'passed'])
|
|
39
|
+
for (const counts of Object.values(s[kind]))
|
|
40
|
+
for (const [id, m] of Object.entries(counts.models)) {
|
|
41
|
+
const sum = models.get(id) ?? { agents: 0, moved: 0 };
|
|
42
|
+
models.set(id, { agents: sum.agents + m.agents, moved: sum.moved + (m.moved ?? 0) });
|
|
43
|
+
}
|
|
44
|
+
return [...models].filter(([, m]) => m.agents > 0).sort(byFamily);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Squares for `models` ([id, { agents, moved }]), toned by model, filled for the agents the router moved. `scale`:
|
|
48
|
+
// one per agent up to `max`, then each model's share of `max`, at least one. Else one per agent up to `max`, and
|
|
49
|
+
// `rest` counts the agents past them. Each run is its tone, filled or not, its count and its glyphs.
|
|
50
|
+
function squaresOf(models, max, scale) {
|
|
51
|
+
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
52
|
+
const shown = (n) => (!scale || total <= max || !n ? n : Math.max(1, Math.round((n / total) * max)));
|
|
53
|
+
const runs = [];
|
|
54
|
+
let left = max;
|
|
55
|
+
for (const [id, m] of models) {
|
|
56
|
+
const n = Math.min(shown(m.agents), left);
|
|
57
|
+
const filled = Math.min(n, shown(m.moved));
|
|
58
|
+
const tone = modelTone(id);
|
|
59
|
+
if (filled) runs.push({ tone, filled: true, n: filled, text: '■'.repeat(filled) });
|
|
60
|
+
if (n > filled) runs.push({ tone, filled: false, n: n - filled, text: '□'.repeat(n - filled) });
|
|
61
|
+
left -= n;
|
|
62
|
+
}
|
|
63
|
+
const rest = scale ? 0 : total - (max - left);
|
|
64
|
+
return { runs, width: max - left, rest: rest ? `+${rest}` : null };
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// "routing −$0.21 (−5%) vs Claude Code’s picks on 7 routed agents", in the session readout's words: the routed
|
|
68
|
+
// agents' cost against Claude Code's picks.
|
|
69
|
+
function verdictOf(routed) {
|
|
70
|
+
const agents = routed.reduce((n, [, c]) => n + c.agents, 0);
|
|
71
|
+
const totals = routed.map(([, c]) => modelTotals(c));
|
|
72
|
+
const cost = totals.reduce((n, t) => n + t.usd, 0);
|
|
73
|
+
const core = totals.reduce((n, t) => n + t.coreUsd, 0);
|
|
74
|
+
const counts = {
|
|
75
|
+
requests: routed.reduce((n, [, c]) => n + c.requests, 0),
|
|
76
|
+
pricedRequests: routed.reduce((n, [, c]) => n + c.pricedRequests, 0),
|
|
77
|
+
};
|
|
78
|
+
if (!counts.pricedRequests) {
|
|
79
|
+
const text = `${plural(agents, 'routed agent')}, not priced`;
|
|
80
|
+
return { priced: false, text, graphical: text };
|
|
81
|
+
}
|
|
82
|
+
const difference = cents(cost - core);
|
|
83
|
+
const on = pricedText(` on ${plural(agents, 'routed agent')}`, counts);
|
|
84
|
+
const share = core > 0 && difference ? ` (${difference < 0 ? '−' : '+'}${percent(Math.abs(difference) / core)})` : '';
|
|
85
|
+
return {
|
|
86
|
+
priced: true,
|
|
87
|
+
text: `routing ${signedUsd(difference)}${share}`,
|
|
88
|
+
tone: savingTone(difference),
|
|
89
|
+
versus: ' vs Claude Code’s picks',
|
|
90
|
+
on,
|
|
91
|
+
graphical: `Routing ${signedUsd(difference)}${share} vs Claude Code’s picks${on}`,
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The strip of every agent by model, what the router moved, and how routing paid off.
|
|
96
|
+
function agentSummaryOf(s) {
|
|
97
|
+
const models = agentsByModel(s);
|
|
98
|
+
const total = models.reduce((n, [, m]) => n + m.agents, 0);
|
|
99
|
+
const moved = models.reduce((n, [, m]) => n + m.moved, 0);
|
|
100
|
+
const strip = squaresOf(models, AGENT_STRIP, true);
|
|
101
|
+
const names = models.map(([id, m]) => ({
|
|
102
|
+
tone: modelTone(id),
|
|
103
|
+
text: `${modelName(id)} ${m.agents}`,
|
|
104
|
+
graphical: `${modelName(id)} · ${m.agents}`,
|
|
105
|
+
}));
|
|
106
|
+
const routed = Object.entries(s.routed).filter(([, c]) => c.agents > 0);
|
|
107
|
+
const kept = total - moved;
|
|
108
|
+
return {
|
|
109
|
+
strip,
|
|
110
|
+
alt: `${plural(total, 'agent')}: ${models.map(([id, m]) => `${modelName(id)} ${m.agents}, ${m.moved} moved`).join('; ')}`,
|
|
111
|
+
names,
|
|
112
|
+
total: plural(total, 'agent'),
|
|
113
|
+
moved: { terminal: `■ ${moved} moved by the router`, graphical: `${moved} moved by the router` },
|
|
114
|
+
kept: { terminal: `□ ${kept} on Claude Code’s pick`, graphical: `${kept} on Claude Code’s pick` },
|
|
115
|
+
verdict: routed.length ? verdictOf(routed) : null,
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// Agents per day from the period's first day, or the first recorded one, to today, with the busiest day.
|
|
120
|
+
function agentTrendOf(s, period, now) {
|
|
121
|
+
if (period === 'session' || period === 'today' || !s.perDay.length) return null;
|
|
122
|
+
const counts = new Map(s.perDay);
|
|
123
|
+
const start = periodRange(period, now).from ?? s.perDay[0][0];
|
|
124
|
+
const [y, m, d] = start.split('-').map(Number);
|
|
125
|
+
const days = [];
|
|
126
|
+
for (let i = 0; ; i += 1) {
|
|
127
|
+
const key = dayKey(new Date(y, m - 1, d + i).getTime());
|
|
128
|
+
if (key > dayKey(now)) break;
|
|
129
|
+
days.push([key, counts.get(key) ?? 0]);
|
|
130
|
+
}
|
|
131
|
+
if (days.length < 2) return null;
|
|
132
|
+
const [peakDay, peak] = days.reduce((top, day) => (day[1] > top[1] ? day : top));
|
|
133
|
+
const values = days.map(([, n]) => n);
|
|
134
|
+
return {
|
|
135
|
+
label: 'per day',
|
|
136
|
+
values,
|
|
137
|
+
peak: { terminal: `peak ${peak} on ${dayLabel(peakDay)}`, graphical: `Peak ${peak} on ${dayLabel(peakDay)}` },
|
|
138
|
+
title: 'Agents per day',
|
|
139
|
+
chart: chartOf('Agents per day', values, { all: days.map(([key]) => dayLabel(key)) }, String),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// "+$0.44 ▲" against Claude Code's pick, in the savings tones; a dash when it rounds to nothing.
|
|
144
|
+
const pickOf = (value) =>
|
|
145
|
+
value
|
|
146
|
+
? { ...saving(value), text: `${signedUsd(value)} ${value > 0 ? '▲' : '▼'}`, dash: false }
|
|
147
|
+
: { ...NO_DIFF, dash: true };
|
|
148
|
+
|
|
149
|
+
// What a routed model row holds: "2 moved up", or "1 kept, 2 down".
|
|
150
|
+
function moveText(m) {
|
|
151
|
+
const kept = m.agents - m.moved;
|
|
152
|
+
const other = m.moved - m.up - m.down;
|
|
153
|
+
const parts = [
|
|
154
|
+
kept ? `${kept} kept` : null,
|
|
155
|
+
m.up ? `${m.up} up` : null,
|
|
156
|
+
m.down ? `${m.down} down` : null,
|
|
157
|
+
other ? `${other} moved` : null,
|
|
158
|
+
].filter(Boolean);
|
|
159
|
+
if (parts.length > 1 || kept) return parts.join(', ');
|
|
160
|
+
return `${m.moved} moved${m.up ? ' up' : m.down ? ' down' : ''}`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// One row of the subagent table; open, the models it ran on with their cost, and its requests and retry proxies.
|
|
164
|
+
function agentRowOf(view, actions, { id, name, label = name, counts, routed, why, billing }) {
|
|
165
|
+
const open = view.usageOpen === id;
|
|
166
|
+
const models = Object.entries(counts.models)
|
|
167
|
+
.filter(([, m]) => m.agents > 0)
|
|
168
|
+
.sort(byFamily);
|
|
169
|
+
const t = modelTotals(counts);
|
|
170
|
+
// A row none of whose requests had a price says so rather than $0.00.
|
|
171
|
+
const priced = counts.pricedRequests > 0;
|
|
172
|
+
/** @returns {Toned} */
|
|
173
|
+
const costOf = (value) =>
|
|
174
|
+
priced ? { text: costText(value, billing), tone: 'primary' } : { text: 'no price', tone: 'muted' };
|
|
175
|
+
const notes = [
|
|
176
|
+
`${(counts.requests / counts.agents).toFixed(1)} requests per agent`,
|
|
177
|
+
routed ? `${counts.respawns || 'none'} respawned within 10 min` : null,
|
|
178
|
+
counts.failures ? plural(counts.failures, 'classifier failure') : null,
|
|
179
|
+
counts.pricedRequests < counts.requests ? `${counts.requests - counts.pricedRequests} requests not priced` : null,
|
|
180
|
+
].filter(Boolean);
|
|
181
|
+
return {
|
|
182
|
+
key: `row-${id}`,
|
|
183
|
+
open,
|
|
184
|
+
toggle: () => actions.usageOpen(open ? null : id),
|
|
185
|
+
name: { terminal: name, graphical: label },
|
|
186
|
+
squares: squaresOf(
|
|
187
|
+
models.map(([model, m]) => [model, { agents: m.agents, moved: m.moved ?? 0 }]),
|
|
188
|
+
ROW_SQUARES,
|
|
189
|
+
false,
|
|
190
|
+
),
|
|
191
|
+
alt: `${label}: ${models.map(([model, m]) => `${modelName(model)} ${m.agents}, ${m.moved ?? 0} moved`).join('; ')}`,
|
|
192
|
+
agents: String(counts.agents),
|
|
193
|
+
cost: costOf(t.usd),
|
|
194
|
+
pick: routed && priced ? pickOf(cents(t.usd - t.coreUsd)) : null,
|
|
195
|
+
why: routed ? null : (why ?? null),
|
|
196
|
+
children: open
|
|
197
|
+
? models.map(([model, m]) => ({
|
|
198
|
+
tone: modelTone(model),
|
|
199
|
+
filled: Boolean(m.moved),
|
|
200
|
+
text: `${m.moved ? '■' : '□'} ${modelName(model)}`,
|
|
201
|
+
name: modelName(model),
|
|
202
|
+
detail: routed ? moveText(m) : plural(m.agents, 'agent'),
|
|
203
|
+
cost: costOf(m.usd),
|
|
204
|
+
pick: routed && priced ? pickOf(cents(m.usd - m.coreUsd)) : null,
|
|
205
|
+
}))
|
|
206
|
+
: [],
|
|
207
|
+
notes: open ? { text: notes.join(' · '), graphical: sentence(notes.join(' · ')) } : null,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// Subagents in the chosen period: this session's record, or every session's in every project (one $.store per
|
|
212
|
+
// plugin install). A strip of every agent by the model it ran on, filled where the router moved it off Claude Code's
|
|
213
|
+
// pick, and the routed agents' cost against those picks. Then the routed types and the pass-throughs, each with its
|
|
214
|
+
// squares, agents and cost, opening to its models; a pass-through says why Claude Code picked.
|
|
215
|
+
export function agentsOf(config, view, actions, period, now, billing) {
|
|
216
|
+
const mode = config.subagentRouting;
|
|
217
|
+
const record = readSubagentStore(period === 'session' ? view.subagentSession : view.subagentStore);
|
|
218
|
+
const s = summarizeSubagents(record, periodRange(period, now));
|
|
219
|
+
const first = Number.isFinite(record.since) ? dayKey(record.since) : null;
|
|
220
|
+
const scope =
|
|
221
|
+
period === 'session' ? 'this session' : `all projects · ${spanText(period, now, first) ?? 'nothing yet'}`;
|
|
222
|
+
const base = {
|
|
223
|
+
head: 'SUBAGENTS',
|
|
224
|
+
scope: ` · ${scope} subagent routing ${mode}`,
|
|
225
|
+
title: 'Subagents',
|
|
226
|
+
hint: `${sentence(scope)} · Subagent routing is ${mode}`,
|
|
227
|
+
};
|
|
228
|
+
const routed = Object.entries(s.routed)
|
|
229
|
+
.filter(([, c]) => c.agents > 0)
|
|
230
|
+
.sort((a, b) => b[1].agents - a[1].agents || (a[0] < b[0] ? -1 : 1));
|
|
231
|
+
const passed = PASS_REASONS.filter((reason) => s.passed[reason]?.agents > 0).sort(
|
|
232
|
+
(a, b) => s.passed[b].agents - s.passed[a].agents,
|
|
233
|
+
);
|
|
234
|
+
const undatedText = s.undated
|
|
235
|
+
? `+ ${plural(s.undated.agents, 'agent')} from before 1.14.0, not split by model · ${costText(s.undated.usd, billing)}`
|
|
236
|
+
: null;
|
|
237
|
+
const undated = undatedText ? { text: undatedText, graphical: undatedText } : null;
|
|
238
|
+
if (!routed.length && !passed.length) {
|
|
239
|
+
const older = !s.undated && periodRange(period, now).from !== null && readSubagentStore(view.subagentStore).undated;
|
|
240
|
+
const none = `No subagents ${PERIOD_PHRASE[period]}.`;
|
|
241
|
+
return {
|
|
242
|
+
...base,
|
|
243
|
+
columns: [],
|
|
244
|
+
empty:
|
|
245
|
+
mode === 'off'
|
|
246
|
+
? {
|
|
247
|
+
text: 'Not recorded: subagentRouting is off in router.json.',
|
|
248
|
+
graphical: 'Not recorded: subagent routing is off in the settings file.',
|
|
249
|
+
}
|
|
250
|
+
: { text: none, graphical: none },
|
|
251
|
+
undated,
|
|
252
|
+
older: older ? 'Agents from before 1.14.0 have no date: All shows them.' : null,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const rowOf = (fields) => agentRowOf(view, actions, { billing, ...fields });
|
|
256
|
+
const groups = [
|
|
257
|
+
routed.length
|
|
258
|
+
? {
|
|
259
|
+
head: 'ROUTED',
|
|
260
|
+
hint: { terminal: 'the router picked the model at spawn', graphical: 'The router picked the model at spawn' },
|
|
261
|
+
title: 'Routed',
|
|
262
|
+
header: { type: 'type', agents: 'agents', cost: 'cost', pick: 'vs CC' },
|
|
263
|
+
labels: { name: '', squares: '', agents: 'Agents', cost: 'Cost', pick: 'Vs Claude Code' },
|
|
264
|
+
rows: routed.map(([type, counts]) => rowOf({ id: `subagent:${type}`, name: type, counts, routed: true })),
|
|
265
|
+
}
|
|
266
|
+
: null,
|
|
267
|
+
passed.length
|
|
268
|
+
? {
|
|
269
|
+
head: 'NOT ROUTED',
|
|
270
|
+
hint: { terminal: 'Claude Code picked the model', graphical: 'Claude Code picked the model' },
|
|
271
|
+
title: 'Not routed',
|
|
272
|
+
header: null,
|
|
273
|
+
labels: { name: '', squares: '', agents: 'Agents', cost: 'Cost', pick: '' },
|
|
274
|
+
rows: passed.map((reason) =>
|
|
275
|
+
rowOf({
|
|
276
|
+
id: `subagent-pass:${reason}`,
|
|
277
|
+
name: PASS_LABEL[reason],
|
|
278
|
+
label: sentence(PASS_LABEL[reason]),
|
|
279
|
+
counts: s.passed[reason],
|
|
280
|
+
routed: false,
|
|
281
|
+
why: { terminal: PASS_WHY[reason], graphical: PASS_CARD_WHY[reason] ?? sentence(PASS_WHY[reason]) },
|
|
282
|
+
}),
|
|
283
|
+
),
|
|
284
|
+
}
|
|
285
|
+
: null,
|
|
286
|
+
].filter(Boolean);
|
|
287
|
+
return {
|
|
288
|
+
...base,
|
|
289
|
+
empty: null,
|
|
290
|
+
summary: agentSummaryOf(s),
|
|
291
|
+
trend: agentTrendOf(s, period, now),
|
|
292
|
+
undated,
|
|
293
|
+
groups,
|
|
294
|
+
columns: [{ key: 'name' }, { key: 'squares' }, { key: 'agents' }, { key: 'cost', show: 'wide' }, { key: 'pick' }],
|
|
295
|
+
legend: [
|
|
296
|
+
{
|
|
297
|
+
text: '■ moved by the router · □ Claude Code’s pick · color: the model',
|
|
298
|
+
graphical: 'Filled squares: moved by the router · outlined: Claude Code’s pick · each model in its own color',
|
|
299
|
+
},
|
|
300
|
+
routed.length
|
|
301
|
+
? {
|
|
302
|
+
text: 'vs CC: the same tokens on Claude Code’s pick, at configured API rates',
|
|
303
|
+
graphical: 'Vs Claude Code: the same tokens on Claude Code’s pick, at configured API rates',
|
|
304
|
+
}
|
|
305
|
+
: null,
|
|
306
|
+
].filter(Boolean),
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// A model's tone by family; muted for a model of no known family.
|
|
311
|
+
const modelTone = (id) => familyTone(id) ?? 'muted';
|
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
// The Usage tab's cost by activity, model or tier, What if, and what is worth a look.
|
|
2
|
+
import { ACTIVITIES, TIERS } from '../../config.mjs';
|
|
3
|
+
import { costText, formatTokens, modelName, percent, signedUsd, UNROUTED, usd } from '../../display.mjs';
|
|
4
|
+
import { breakdown, cellsWhere, INSIGHT_REPLIES, insights, mostlyOn } from '../../usage.mjs';
|
|
5
|
+
import { sentence, tierLabel } from './format.mjs';
|
|
6
|
+
import { cents, finite, NO_DIFF, routeText, saving } from './usage-shared.mjs';
|
|
7
|
+
|
|
8
|
+
/** @typedef {import('../tokens.mjs').Tone} Tone */
|
|
9
|
+
/** @typedef {{ text: string, tone: Tone }} Toned */
|
|
10
|
+
|
|
11
|
+
const GROUPS = [
|
|
12
|
+
['activity', 'Activity', 'By activity'],
|
|
13
|
+
['model', 'Model', 'By model'],
|
|
14
|
+
['tier', 'Tier', 'By tier'],
|
|
15
|
+
];
|
|
16
|
+
// What an open row lists: the routes of an activity, the activities of a model or a tier.
|
|
17
|
+
const CHILD_BY = { activity: 'route', model: 'activity', tier: 'activity' };
|
|
18
|
+
// Tiers in their own order; `none` (not routed) last.
|
|
19
|
+
const TIER_RANK = (name) => (TIERS.includes(name) ? TIERS.indexOf(name) : TIERS.length);
|
|
20
|
+
// A breakdown row's name: an activity as its verb, a tier as its bars, a model or a route by name; `none` in words.
|
|
21
|
+
function nameOf(by, name) {
|
|
22
|
+
if (name === 'none') {
|
|
23
|
+
const text = by === 'activity' ? 'no activity' : 'not routed';
|
|
24
|
+
return { kind: 'none', text, graphical: sentence(text) };
|
|
25
|
+
}
|
|
26
|
+
if (by === 'activity') return { kind: by, text: name, graphical: name };
|
|
27
|
+
if (by === 'tier') return { kind: by, text: name, graphical: tierLabel(name) };
|
|
28
|
+
const text = by === 'model' ? modelName(name) : routeText(name);
|
|
29
|
+
return { kind: 'plain', text, graphical: text };
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// The period's cost by activity, model or tier, most first; `g` presses the next grouping. A row opens to what it
|
|
33
|
+
// holds and its cost parts. A narrow terminal drops "mostly on" (or the share) before any number.
|
|
34
|
+
export function breakdownOf(view, actions, summary, compare, billing) {
|
|
35
|
+
const by = GROUPS.some(([id]) => id === view.usageGroup) ? view.usageGroup : 'activity';
|
|
36
|
+
const next = GROUPS[(GROUPS.findIndex(([id]) => id === by) + 1) % GROUPS.length]?.[0];
|
|
37
|
+
const group = {
|
|
38
|
+
value: by,
|
|
39
|
+
options: GROUPS.map(([id, label, title]) => ({
|
|
40
|
+
value: id,
|
|
41
|
+
key: `group-${id}`,
|
|
42
|
+
label: { terminal: label, graphical: title },
|
|
43
|
+
selected: id === by,
|
|
44
|
+
...(id === next ? { hotkey: 'g' } : {}),
|
|
45
|
+
})),
|
|
46
|
+
pick: (id) => actions.usageGroup(id),
|
|
47
|
+
head: 'BY ',
|
|
48
|
+
hint: 'g next',
|
|
49
|
+
};
|
|
50
|
+
const cells = summary.observed.cells;
|
|
51
|
+
// Tiers read low to high, as the band's bars; activities and models lead with the costliest.
|
|
52
|
+
const groups =
|
|
53
|
+
by === 'tier' ? breakdown(cells, by).sort((a, b) => TIER_RANK(a.name) - TIER_RANK(b.name)) : breakdown(cells, by);
|
|
54
|
+
const missing = new Map(breakdown(summary.observed.uncompared, by).map((g) => [g.name, g]));
|
|
55
|
+
const base = { title: 'Breakdown', group };
|
|
56
|
+
if (!groups.length)
|
|
57
|
+
return {
|
|
58
|
+
...base,
|
|
59
|
+
// Defensive: the Usage tab shows its empty Summary before a period with no replies reaches the breakdown.
|
|
60
|
+
columns: [],
|
|
61
|
+
empty: { text: 'no routed replies in this period', graphical: 'Nothing to break down in this period.' },
|
|
62
|
+
};
|
|
63
|
+
const routedUsd = summary.observed.routedUsd;
|
|
64
|
+
const numbers = (sums, unavailable) => ({
|
|
65
|
+
replies: String(sums[0]),
|
|
66
|
+
cost: costText(sums[1], billing),
|
|
67
|
+
diff: !compare ? null : unavailable ? NO_DIFF : saving(cents(sums[1] - sums[2])),
|
|
68
|
+
});
|
|
69
|
+
const rows = groups.map((one) => {
|
|
70
|
+
const id = `${by}:${one.name}`;
|
|
71
|
+
const open = view.usageOpen === id;
|
|
72
|
+
const route = by === 'model' ? null : mostlyOn(cellsWhere(cells, by, one.name));
|
|
73
|
+
const fraction = routedUsd > 0 ? one.sums[1] / routedUsd : 0;
|
|
74
|
+
const [, , , cheaper, stronger, switched, input, , output] = one.sums;
|
|
75
|
+
const note = missing.has(one.name)
|
|
76
|
+
? `comparison unavailable · input ${formatTokens(input)} · output ${formatTokens(output)}`
|
|
77
|
+
: `cheaper ${signedUsd(cents(cheaper))} · stronger ${signedUsd(cents(stronger))} · cache rebuilds ${signedUsd(cents(switched))} · input ${formatTokens(input)} · output ${formatTokens(output)}`;
|
|
78
|
+
return {
|
|
79
|
+
key: `row-${id}`,
|
|
80
|
+
open,
|
|
81
|
+
toggle: () => actions.usageOpen(open ? null : id),
|
|
82
|
+
name: nameOf(by, one.name),
|
|
83
|
+
...numbers(one.sums, missing.has(one.name)),
|
|
84
|
+
// The terminal's last column where it fits: the share of a model, else the route an activity or tier mostly ran.
|
|
85
|
+
tail: by === 'model' ? percent(fraction) : route ? routeText(route) : null,
|
|
86
|
+
// A card's bar: the row's share of the routed cost, with its figure.
|
|
87
|
+
share: { value: finite(fraction), text: percent(finite(fraction)) },
|
|
88
|
+
children: open
|
|
89
|
+
? one.children.map((child) => ({
|
|
90
|
+
name: nameOf(CHILD_BY[by], child.name),
|
|
91
|
+
...numbers(
|
|
92
|
+
child.sums,
|
|
93
|
+
missing.get(one.name)?.children.some((c) => c.name === child.name),
|
|
94
|
+
),
|
|
95
|
+
}))
|
|
96
|
+
: [],
|
|
97
|
+
note: open ? { text: note, graphical: sentence(note) } : null,
|
|
98
|
+
};
|
|
99
|
+
});
|
|
100
|
+
return {
|
|
101
|
+
...base,
|
|
102
|
+
empty: null,
|
|
103
|
+
header: {
|
|
104
|
+
replies: { terminal: 'replies', graphical: 'Replies' },
|
|
105
|
+
cost: { terminal: 'cost', graphical: 'Cost' },
|
|
106
|
+
diff: compare ? { terminal: `vs ${UNROUTED}`, graphical: `Vs ${UNROUTED}` } : null,
|
|
107
|
+
tail: by === 'model' ? 'share' : 'mostly on',
|
|
108
|
+
},
|
|
109
|
+
labels: { name: '', replies: 'Replies', cost: 'Cost', diff: `Vs ${UNROUTED}`, share: 'Share' },
|
|
110
|
+
columns: [
|
|
111
|
+
{ key: 'name' },
|
|
112
|
+
{ key: 'replies', show: 'wide' },
|
|
113
|
+
{ key: 'cost' },
|
|
114
|
+
...(compare ? [{ key: 'diff' }] : []),
|
|
115
|
+
{ key: 'share', show: 'wide' },
|
|
116
|
+
],
|
|
117
|
+
rows,
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// What the period's tokens would cost on each model alone, as your model for every reply (compareModels), beside what
|
|
122
|
+
// routing spent and your own models; a model some reply had no price on is left out. `toggle` switches the terminal
|
|
123
|
+
// between this and the trend, and shows or hides these rows on a card.
|
|
124
|
+
export function compareOf(view, actions, summary, billing) {
|
|
125
|
+
const on = Boolean(view.usageCompare);
|
|
126
|
+
const base = {
|
|
127
|
+
on,
|
|
128
|
+
toggle: {
|
|
129
|
+
key: 'usage-compare',
|
|
130
|
+
label: { terminal: on ? 'Trend' : 'What if', graphical: 'What if' },
|
|
131
|
+
hotkey: 'c',
|
|
132
|
+
press: actions.usageCompare,
|
|
133
|
+
},
|
|
134
|
+
head: 'WHAT IF',
|
|
135
|
+
hint: { terminal: 'the same tokens on one model', graphical: 'The same tokens on one model alone' },
|
|
136
|
+
title: 'What if',
|
|
137
|
+
};
|
|
138
|
+
if (summary.observed.uncomparedReplies)
|
|
139
|
+
return {
|
|
140
|
+
...base,
|
|
141
|
+
unavailable: 'What if unavailable: some replies have no comparable cache history.',
|
|
142
|
+
columns: [],
|
|
143
|
+
rows: [],
|
|
144
|
+
note: null,
|
|
145
|
+
};
|
|
146
|
+
const yours = Object.keys(summary.yours);
|
|
147
|
+
const single = yours.length === 1 ? yours[0] : null;
|
|
148
|
+
const rows = Object.entries(summary.whatIf)
|
|
149
|
+
.filter(([, [n]]) => n === summary.replies)
|
|
150
|
+
.map(([model, [, value]]) => ({
|
|
151
|
+
label: `${modelName(model)}${model === single ? ' · yours' : ''}`,
|
|
152
|
+
value,
|
|
153
|
+
routed: false,
|
|
154
|
+
yours: model === single,
|
|
155
|
+
}));
|
|
156
|
+
if (!rows.some((row) => row.yours))
|
|
157
|
+
rows.push({
|
|
158
|
+
label: yours.length > 1 ? 'Your models' : 'Your model',
|
|
159
|
+
value: summary.yoursUsd,
|
|
160
|
+
routed: false,
|
|
161
|
+
yours: true,
|
|
162
|
+
});
|
|
163
|
+
rows.push({ label: 'Routed', value: summary.routedUsd, routed: true, yours: false });
|
|
164
|
+
rows.sort((a, b) => a.value - b.value);
|
|
165
|
+
const max = Math.max(...rows.map((row) => row.value));
|
|
166
|
+
const note = 'same tokens and output on each model alone · answer quality not measured';
|
|
167
|
+
return {
|
|
168
|
+
...base,
|
|
169
|
+
unavailable: null,
|
|
170
|
+
rows: rows.map((row) => ({
|
|
171
|
+
label: row.label,
|
|
172
|
+
strong: row.routed,
|
|
173
|
+
// The bar's fill as a share of the costliest row, in the routed or the plain tone.
|
|
174
|
+
fill: max > 0 ? row.value / max : 0,
|
|
175
|
+
tone: /** @type {Tone} */ (row.routed ? 'accent' : 'muted'),
|
|
176
|
+
cost: costText(row.value, billing),
|
|
177
|
+
diff: row.routed ? null : row.yours ? NO_DIFF : saving(cents(row.value - summary.yoursUsd)),
|
|
178
|
+
})),
|
|
179
|
+
// A wide card draws each cost as its bar's figure.
|
|
180
|
+
columns: [{ key: 'label' }, { key: 'bar', show: 'wide' }, { key: 'cost', show: 'narrow' }, { key: 'diff' }],
|
|
181
|
+
labels: { label: '', bar: 'Cost', cost: 'Cost', diff: 'Vs yours' },
|
|
182
|
+
note: { text: note, graphical: sentence(note) },
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// The evidence worth a look, each with the tab that changes it: the cell that carried most of the cost, the switch
|
|
187
|
+
// writes against the saving, the cell that ran above your model. Never which model to use.
|
|
188
|
+
export function insightsOf(summary, actions) {
|
|
189
|
+
if (summary.observed.uncomparedReplies || summary.replies < INSIGHT_REPLIES) return null;
|
|
190
|
+
const list = insights(summary);
|
|
191
|
+
if (!list.length) return null;
|
|
192
|
+
return {
|
|
193
|
+
head: 'WORTH A LOOK',
|
|
194
|
+
hint: {
|
|
195
|
+
terminal: 'from the numbers; answer quality is not measured',
|
|
196
|
+
graphical: 'From the numbers; answer quality is not measured',
|
|
197
|
+
},
|
|
198
|
+
title: 'Worth a look',
|
|
199
|
+
items: list.map((insight) => insightOf(insight, actions)),
|
|
200
|
+
};
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// One finding with the tab that changes it.
|
|
204
|
+
function insightOf(insight, actions) {
|
|
205
|
+
if (insight.kind === 'switch') {
|
|
206
|
+
const text = `Cache rebuilds used ${percent(insight.share)} of the cheaper-model saving`;
|
|
207
|
+
return {
|
|
208
|
+
kind: 'switch',
|
|
209
|
+
text,
|
|
210
|
+
graphical: text,
|
|
211
|
+
button: { key: 'insight-switch', label: 'Policy', press: () => actions.tab('policy') },
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
const tier = TIERS.includes(insight.tier) ? insight.tier : null;
|
|
215
|
+
const words =
|
|
216
|
+
insight.kind === 'cost'
|
|
217
|
+
? `carried ${percent(insight.share)} of the cost`
|
|
218
|
+
: `ran ${usd(insight.usd)} above your model`;
|
|
219
|
+
const route = insight.route ? `mostly ${routeText(insight.route)}` : null;
|
|
220
|
+
return {
|
|
221
|
+
kind: insight.kind,
|
|
222
|
+
activity: ACTIVITIES.includes(insight.activity) ? insight.activity : null,
|
|
223
|
+
tier,
|
|
224
|
+
tierLabel: tier ? tierLabel(tier) : null,
|
|
225
|
+
notRouted: { terminal: 'not routed', graphical: 'Not routed' },
|
|
226
|
+
text: words,
|
|
227
|
+
route,
|
|
228
|
+
button: tier
|
|
229
|
+
? {
|
|
230
|
+
key: `insight-${insight.kind}`,
|
|
231
|
+
label: 'Routes',
|
|
232
|
+
press: () =>
|
|
233
|
+
actions.openRoute(ACTIVITIES.includes(insight.activity) ? `${insight.activity}.${tier}` : `tier.${tier}`),
|
|
234
|
+
}
|
|
235
|
+
: null,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// What the Usage model's parts share: a difference in its tone, a chart's data, a route in words, a period's span.
|
|
2
|
+
import { routeLabel, signedUsd } from '../../display.mjs';
|
|
3
|
+
import { periodRange } from '../../usage.mjs';
|
|
4
|
+
import { savingTone } from '../tokens.mjs';
|
|
5
|
+
import { dayLabel } from './format.mjs';
|
|
6
|
+
|
|
7
|
+
/** @typedef {import('../tokens.mjs').Tone} Tone */
|
|
8
|
+
/** @typedef {{ text: string, tone: Tone }} Toned */
|
|
9
|
+
|
|
10
|
+
export const PERIOD_PHRASE = {
|
|
11
|
+
session: 'this session',
|
|
12
|
+
today: 'today',
|
|
13
|
+
'7d': 'in the last 7 days',
|
|
14
|
+
'30d': 'in the last 30 days',
|
|
15
|
+
month: 'this month',
|
|
16
|
+
all: 'yet',
|
|
17
|
+
};
|
|
18
|
+
/** @returns {Toned} A difference against no routing, signed, in its savings tone. */
|
|
19
|
+
export const saving = (value) => ({ text: signedUsd(value), tone: savingTone(value) });
|
|
20
|
+
// A comparison that is not available.
|
|
21
|
+
/** @type {Toned} */
|
|
22
|
+
export const NO_DIFF = { text: '—', tone: 'muted' };
|
|
23
|
+
// A cents difference, so a row never reads "+$0.00" for a fraction of a cent.
|
|
24
|
+
export const cents = (value) => Math.round(value * 100) / 100;
|
|
25
|
+
// A figure that is never NaN on a chart.
|
|
26
|
+
export const finite = (value) => (Number.isFinite(value) ? value : 0);
|
|
27
|
+
|
|
28
|
+
// "Opus 5.5 · high" for a cell's `id@effort`.
|
|
29
|
+
export function routeText(route) {
|
|
30
|
+
const at = route.lastIndexOf('@');
|
|
31
|
+
const effort = route.slice(at + 1);
|
|
32
|
+
return routeLabel(route.slice(0, at), effort === 'session' ? null : effort);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// "Oct 7 – Oct 13" for a period's span, or "since Oct 10", its `first` day, for one without a range; null without one.
|
|
36
|
+
export function spanText(period, now, first) {
|
|
37
|
+
const { from, to } = periodRange(period, now);
|
|
38
|
+
if (period === 'today') return `today, ${dayLabel(to)}`;
|
|
39
|
+
if (from) return `${dayLabel(from)} – ${dayLabel(to)}`;
|
|
40
|
+
return first ? `since ${dayLabel(first)}` : null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// A chart of `points`, its first and last bucket labels, and an alt that states the first, last and peak values.
|
|
44
|
+
export function chartOf(title, points, labels, money, at = 'on') {
|
|
45
|
+
const values = points.map(finite);
|
|
46
|
+
const peak = Math.max(...values);
|
|
47
|
+
const when = labels.all[values.indexOf(peak)];
|
|
48
|
+
return {
|
|
49
|
+
points: values,
|
|
50
|
+
labels: [labels.all[0], labels.all.at(-1)],
|
|
51
|
+
alt: `${title}, ${labels.all[0]} to ${labels.all.at(-1)}: first ${money(values[0])}, last ${money(values.at(-1))}, peak ${money(peak)} ${at} ${when}`,
|
|
52
|
+
tone: /** @type {Tone} */ ('accent'),
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Prices summed over the priced requests, and how many were not priced (a model without a price in router.json).
|
|
57
|
+
export const pricedText = (text, counts) =>
|
|
58
|
+
counts.pricedRequests < counts.requests ? `${text} (${counts.requests - counts.pricedRequests} unpriced)` : text;
|