@alexeiled/claude-router 1.16.0 → 1.17.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 +26 -0
- package/README.md +33 -5
- package/docs/architecture.md +8 -4
- package/docs/configuration.md +68 -6
- package/docs/evaluation.md +34 -0
- package/docs/providers.md +222 -0
- package/docs/user-guide.md +20 -6
- package/hooks/native-router.mjs +69 -32
- package/lib/activity-stats.mjs +5 -3
- package/lib/activity.mjs +8 -3
- package/lib/config-file.mjs +5 -5
- package/lib/config.mjs +61 -11
- package/lib/cost.mjs +26 -4
- package/lib/decisions.mjs +4 -1
- package/lib/display.mjs +15 -6
- package/lib/facts.mjs +10 -0
- package/lib/panel.mjs +24 -9
- package/lib/providers.mjs +116 -0
- package/lib/route.mjs +40 -6
- package/lib/savings.mjs +43 -12
- package/lib/subagents.mjs +4 -3
- package/package.json +3 -2
package/lib/display.mjs
CHANGED
|
@@ -48,12 +48,21 @@ export function modeLever(config, view) {
|
|
|
48
48
|
const UNPRICED_LABEL = { usage: 'no usage', model: 'no price', ttl: 'cache lifetime unknown', error: 'error' };
|
|
49
49
|
export function unpricedLine(metrics) {
|
|
50
50
|
const { unpriced } = readMetrics(metrics);
|
|
51
|
-
const
|
|
52
|
-
|
|
53
|
-
const
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
51
|
+
const missing = UNPRICED_CAUSES.filter((cause) => cause !== 'comparison');
|
|
52
|
+
const total = missing.reduce((n, cause) => n + unpriced[cause], 0);
|
|
53
|
+
const comparison = unpriced.comparison
|
|
54
|
+
? `Comparison unavailable: ${unpriced.comparison} routed repl${unpriced.comparison === 1 ? 'y' : 'ies'}; observed costs are in activity stats`
|
|
55
|
+
: null;
|
|
56
|
+
if (!total) return comparison;
|
|
57
|
+
const causes = missing
|
|
58
|
+
.filter((cause) => unpriced[cause] > 0)
|
|
59
|
+
.map((cause) => `${UNPRICED_LABEL[cause]} ${unpriced[cause]}`);
|
|
60
|
+
return [
|
|
61
|
+
`Unpriced ${total} routed repl${total === 1 ? 'y' : 'ies'} left out of usage: ${causes.join(' · ')}`,
|
|
62
|
+
comparison,
|
|
63
|
+
]
|
|
64
|
+
.filter(Boolean)
|
|
65
|
+
.join(' · ');
|
|
57
66
|
}
|
|
58
67
|
|
|
59
68
|
// Why routing starts off: the session's model is no tier's model. Set at session start, read by the band.
|
package/lib/facts.mjs
CHANGED
|
@@ -7,6 +7,15 @@ const REMINDER = /<system-reminder>[\s\S]*?<\/system-reminder>/g;
|
|
|
7
7
|
const CLIP_MARKER = ' […] ';
|
|
8
8
|
const ANSI_ESCAPE = new RegExp(`${String.fromCharCode(0x1b)}\\[[0-?]*[ -/]*[@-~]`, 'g');
|
|
9
9
|
|
|
10
|
+
// Images can arrive inside a tool result, not just a user message.
|
|
11
|
+
export function hasImages(messages) {
|
|
12
|
+
const contains = (content) =>
|
|
13
|
+
blocks(content).some(
|
|
14
|
+
(block) => block.type === 'image' || (block.type === 'tool_result' && contains(block.content)),
|
|
15
|
+
);
|
|
16
|
+
return messages.some((message) => contains(message.content));
|
|
17
|
+
}
|
|
18
|
+
|
|
10
19
|
export function extractFacts(body, memory, { recentTurns, maxTextChars }) {
|
|
11
20
|
const messages = Array.isArray(body.messages) ? body.messages : [];
|
|
12
21
|
// Claude Code puts hook output and tool additions in `system` messages after the user message, and keeps them in
|
|
@@ -35,6 +44,7 @@ export function extractFacts(body, memory, { recentTurns, maxTextChars }) {
|
|
|
35
44
|
turns.push({ role: message.role, text: clip(text, maxTextChars) });
|
|
36
45
|
});
|
|
37
46
|
return {
|
|
47
|
+
hasImages: hasImages(messages),
|
|
38
48
|
turns: turns.slice(-recentTurns),
|
|
39
49
|
prompt: current === -1 ? '' : clip(textOf(lastBlocks), maxTextChars),
|
|
40
50
|
continuation: last?.role === 'user' && lastBlocks.some((b) => b.type === 'tool_result'),
|
package/lib/panel.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import {
|
|
|
4
4
|
ACTIVITIES,
|
|
5
5
|
ACTIVITY_MODES,
|
|
6
6
|
activeClassifier,
|
|
7
|
-
|
|
7
|
+
defaultsFor,
|
|
8
8
|
effectiveActivities,
|
|
9
9
|
effectiveRoutes,
|
|
10
10
|
ROUTING_MODES,
|
|
@@ -114,6 +114,7 @@ const EFFORT_SHORT = { low: 'low', medium: 'med', high: 'high', xhigh: 'xh', max
|
|
|
114
114
|
export function routeDraftOf(config, view) {
|
|
115
115
|
if (view.routeDraft) return view.routeDraft;
|
|
116
116
|
const base = structuredClone({
|
|
117
|
+
...(config.provider && config.provider !== 'anthropic' ? { provider: config.provider } : {}),
|
|
117
118
|
routes: config.routes,
|
|
118
119
|
baselineTier: config.baselineTier,
|
|
119
120
|
activities: config.activities,
|
|
@@ -185,6 +186,9 @@ export function renderPanel(
|
|
|
185
186
|
flexDirection: 'column',
|
|
186
187
|
children: [
|
|
187
188
|
...header(ui, config, view, tab, actions),
|
|
189
|
+
...(config.provider && config.provider !== 'anthropic'
|
|
190
|
+
? [ui.dim(`Provider: ${config.provider} · ${view.configPath ?? config.nativePath ?? ''}`)]
|
|
191
|
+
: []),
|
|
188
192
|
...body(ui, config, view, usage, actions, modelOptions, columns, now),
|
|
189
193
|
...(view.help ? helpLines(ui) : []),
|
|
190
194
|
...statusBar(ui, config, view, tab, actions),
|
|
@@ -634,7 +638,7 @@ function shortRoute(config, route, narrow) {
|
|
|
634
638
|
return `${route.model}·${narrow ? (EFFORT_SHORT[effort] ?? effort) : effort}`;
|
|
635
639
|
}
|
|
636
640
|
const routeKey = (route) => `${route.model}@${route.effort ?? ''}`;
|
|
637
|
-
const builtIn = (activity, tier) =>
|
|
641
|
+
const builtIn = (config, activity, tier) => defaultsFor(config.provider).activities[activity]?.[tier] ?? null;
|
|
638
642
|
const sharesEffort = (config, route) => effortSharesCache(config, config.models[route.model]?.id);
|
|
639
643
|
// The cache a route runs on: one per model on a model that keeps one cache across efforts, else one per route.
|
|
640
644
|
const cacheOf = (config, route) => (sharesEffort(config, route) ? route.model : routeKey(route));
|
|
@@ -647,7 +651,9 @@ function overrideWarnings(config, uses, activity, tier) {
|
|
|
647
651
|
return [
|
|
648
652
|
above && compareRoutes(config, route, above) > 0 ? 'stronger than the tier above' : null,
|
|
649
653
|
uses.get(cacheOf(config, route)) === 1
|
|
650
|
-
?
|
|
654
|
+
? config.cache.mode === 'automatic'
|
|
655
|
+
? 'a route only this cell uses; cache sharing is unknown'
|
|
656
|
+
: `${sharesEffort(config, route) ? 'a model' : 'a model + effort'} only this cell uses: one more cache`
|
|
651
657
|
: null,
|
|
652
658
|
].filter(Boolean);
|
|
653
659
|
}
|
|
@@ -770,7 +776,7 @@ function routesTab(ui, config, view, _usage, actions, modelOptions, columns) {
|
|
|
770
776
|
const route = resolveRoute(draft, tier, activity);
|
|
771
777
|
const edited = changes.cells.some(([a, t]) => a === activity && t === tier) ? '●' : '';
|
|
772
778
|
if (sameRoute(route, draft.routes[tier])) return cellButton(activity, tier, `·${edited}`, true);
|
|
773
|
-
const preset = builtIn(activity, tier);
|
|
779
|
+
const preset = builtIn(draft, activity, tier);
|
|
774
780
|
const mark = preset && sameRoute(route, { model: preset.model, effort: preset.effort ?? null }) ? '°' : '';
|
|
775
781
|
return cellButton(activity, tier, `${shortRoute(draft, route, narrow)}${mark}${edited}`, mode !== 'on');
|
|
776
782
|
}),
|
|
@@ -781,7 +787,15 @@ function routesTab(ui, config, view, _usage, actions, modelOptions, columns) {
|
|
|
781
787
|
ui.text(' '),
|
|
782
788
|
...cellEditor(ui, draft, changes, selected, cacheUses(draft, true).uses, actions, modelOptions),
|
|
783
789
|
ui.text(' '),
|
|
784
|
-
...
|
|
790
|
+
...(config.cache.mode === 'automatic'
|
|
791
|
+
? [
|
|
792
|
+
ui.dim(
|
|
793
|
+
'Automatic prefix cache: hits are observed; lifetime and sharing across efforts are unknown. No cache-write premium.',
|
|
794
|
+
),
|
|
795
|
+
]
|
|
796
|
+
: config.cache.mode === 'disabled'
|
|
797
|
+
? [ui.dim('Prompt caching disabled: input is priced at the uncached rate.')]
|
|
798
|
+
: cacheLines(ui, uses.size, shared, mode, columns)),
|
|
785
799
|
...LADDER.slice(0, -1)
|
|
786
800
|
.filter((upper, i) => sameRoute(draft.routes[upper], draft.routes[LADDER[i + 1]]))
|
|
787
801
|
.map((upper) =>
|
|
@@ -791,7 +805,7 @@ function routesTab(ui, config, view, _usage, actions, modelOptions, columns) {
|
|
|
791
805
|
),
|
|
792
806
|
),
|
|
793
807
|
ui.Button({ key: 'reset-routes', label: 'Reset routes to defaults', hotkey: 'r', onPress: actions.resetRoutes }),
|
|
794
|
-
ui.dim('A new model ID needs
|
|
808
|
+
ui.dim('A new model ID needs the active file → models: price, window, efforts.'),
|
|
795
809
|
);
|
|
796
810
|
return out;
|
|
797
811
|
}
|
|
@@ -843,7 +857,7 @@ function cellEditor(ui, draft, changes, selected, uses, actions, modelOptions) {
|
|
|
843
857
|
const activity = row;
|
|
844
858
|
const route = resolveRoute(draft, tier, activity);
|
|
845
859
|
const base = draft.routes[tier];
|
|
846
|
-
const preset = builtIn(activity, tier);
|
|
860
|
+
const preset = builtIn(draft, activity, tier);
|
|
847
861
|
const presetRoute = preset ? { model: preset.model, effort: preset.effort ?? null } : null;
|
|
848
862
|
const edited = changes.cells.some(([a, t]) => a === activity && t === tier);
|
|
849
863
|
const own = !sameRoute(route, base);
|
|
@@ -948,7 +962,8 @@ const MODE_HINT = {
|
|
|
948
962
|
function policyTab(ui, config, view, _usage, actions) {
|
|
949
963
|
const changes = routingChanges(config, view);
|
|
950
964
|
const { draft, tuning, policy } = changes;
|
|
951
|
-
const
|
|
965
|
+
const preset = defaultsFor(config.provider);
|
|
966
|
+
const defaults = tuningOf(preset);
|
|
952
967
|
const label = classifierLabel(config);
|
|
953
968
|
const pad = ''.padEnd(13);
|
|
954
969
|
const tune = (key) => choice(ui, key, tuning[key], POLICY_FORMAT[key], (value) => actions.tune(key, value));
|
|
@@ -991,7 +1006,7 @@ function policyTab(ui, config, view, _usage, actions) {
|
|
|
991
1006
|
: draft.baselineTier,
|
|
992
1007
|
' ',
|
|
993
1008
|
marker(ui, changes.baseline),
|
|
994
|
-
draft.baselineTier ===
|
|
1009
|
+
draft.baselineTier === preset.baselineTier ? null : ui.dim(`default ${preset.baselineTier}`),
|
|
995
1010
|
),
|
|
996
1011
|
ui.dim(`${pad}when the session’s model is no tier’s model`),
|
|
997
1012
|
ui.line(
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// Execution presets, not classifier services. Prices are API list-price estimates, never a plan bill.
|
|
2
|
+
// Sources and the limits of these starting routes are in docs/configuration.md#provider-profiles.
|
|
3
|
+
const EFFORTS = ['low', 'high', 'max'];
|
|
4
|
+
const AUTOMATIC = {
|
|
5
|
+
mode: 'automatic',
|
|
6
|
+
effortScope: 'unknown',
|
|
7
|
+
writeMultiplier: {},
|
|
8
|
+
ttlMs: {},
|
|
9
|
+
warmMarginMs: 0,
|
|
10
|
+
};
|
|
11
|
+
const route = (model, effort) => ({ model, effort });
|
|
12
|
+
const agents = (light, heavy) => ({
|
|
13
|
+
types: { Explore: light, Plan: 'classify', 'general-purpose': 'classify' },
|
|
14
|
+
light,
|
|
15
|
+
standard: heavy,
|
|
16
|
+
heavy,
|
|
17
|
+
heavyMass: 0.8,
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
export const PROVIDER_DEFAULTS = {
|
|
21
|
+
deepseek: {
|
|
22
|
+
models: {
|
|
23
|
+
flash: {
|
|
24
|
+
id: 'deepseek-flash[1m]',
|
|
25
|
+
input: 0.3,
|
|
26
|
+
output: 1.2,
|
|
27
|
+
cacheRead: 0.006,
|
|
28
|
+
contextWindow: 1_000_000,
|
|
29
|
+
billing: 'credits',
|
|
30
|
+
efforts: EFFORTS,
|
|
31
|
+
strength: 1,
|
|
32
|
+
imageInput: true,
|
|
33
|
+
},
|
|
34
|
+
},
|
|
35
|
+
routes: {
|
|
36
|
+
micro: route('flash', 'low'),
|
|
37
|
+
low: route('flash', 'high'),
|
|
38
|
+
medium: route('flash', 'high'),
|
|
39
|
+
high: route('flash', 'max'),
|
|
40
|
+
},
|
|
41
|
+
activities: Object.fromEntries(
|
|
42
|
+
['code', 'debug', 'plan', 'review'].map((a) => [a, { medium: route('flash', 'max') }]),
|
|
43
|
+
),
|
|
44
|
+
subagents: agents('flash', 'flash'),
|
|
45
|
+
cache: AUTOMATIC,
|
|
46
|
+
},
|
|
47
|
+
zai: {
|
|
48
|
+
models: {
|
|
49
|
+
flash: {
|
|
50
|
+
id: 'glm-5.3-flash[1m]',
|
|
51
|
+
input: 0.15,
|
|
52
|
+
output: 0.5,
|
|
53
|
+
cacheRead: 0.03,
|
|
54
|
+
contextWindow: 1_000_000,
|
|
55
|
+
billing: 'credits',
|
|
56
|
+
efforts: EFFORTS,
|
|
57
|
+
strength: 1,
|
|
58
|
+
imageInput: true,
|
|
59
|
+
},
|
|
60
|
+
glm: {
|
|
61
|
+
id: 'glm-5.3[1m]',
|
|
62
|
+
input: 1.4,
|
|
63
|
+
output: 4.4,
|
|
64
|
+
cacheRead: 0.26,
|
|
65
|
+
contextWindow: 1_000_000,
|
|
66
|
+
billing: 'credits',
|
|
67
|
+
efforts: EFFORTS,
|
|
68
|
+
strength: 2,
|
|
69
|
+
imageInput: false,
|
|
70
|
+
},
|
|
71
|
+
},
|
|
72
|
+
routes: {
|
|
73
|
+
micro: route('flash', 'low'),
|
|
74
|
+
low: route('flash', 'high'),
|
|
75
|
+
medium: route('glm', 'high'),
|
|
76
|
+
high: route('glm', 'max'),
|
|
77
|
+
},
|
|
78
|
+
activities: {
|
|
79
|
+
...Object.fromEntries(['code', 'debug', 'plan', 'review'].map((a) => [a, { medium: route('glm', 'max') }])),
|
|
80
|
+
...Object.fromEntries(
|
|
81
|
+
['ops', 'explore', 'docs'].map((a) => [a, { medium: route('flash', 'high'), high: route('flash', 'max') }]),
|
|
82
|
+
),
|
|
83
|
+
},
|
|
84
|
+
subagents: agents('flash', 'glm'),
|
|
85
|
+
cache: AUTOMATIC,
|
|
86
|
+
},
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
// A provider is an identifier, never a path. Unknown providers may supply a complete standalone profile.
|
|
90
|
+
export function providerName(value) {
|
|
91
|
+
if (value == null || value === '') return 'anthropic';
|
|
92
|
+
if (typeof value !== 'string' || !/^[a-z][a-z0-9-]{0,63}$/.test(value))
|
|
93
|
+
throw new Error('invalid router provider identifier');
|
|
94
|
+
return value;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function providerFile(value) {
|
|
98
|
+
const provider = providerName(value);
|
|
99
|
+
return provider === 'anthropic' ? 'router.json' : `${provider}-router.json`;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export const canonicalModel = (id) => (typeof id === 'string' ? id.replace(/\[1m\]$/, '') : id);
|
|
103
|
+
|
|
104
|
+
export function executionDefaults(provider, defaults) {
|
|
105
|
+
if (provider === 'anthropic') return defaults;
|
|
106
|
+
return {
|
|
107
|
+
...defaults,
|
|
108
|
+
models: {},
|
|
109
|
+
routes: {},
|
|
110
|
+
activities: {},
|
|
111
|
+
subagentRouting: 'off',
|
|
112
|
+
subagents: { types: {}, light: '', standard: '', heavy: '', heavyMass: 0.8 },
|
|
113
|
+
cache: AUTOMATIC,
|
|
114
|
+
...(Object.hasOwn(PROVIDER_DEFAULTS, provider) ? { subagentRouting: 'on', ...PROVIDER_DEFAULTS[provider] } : {}),
|
|
115
|
+
};
|
|
116
|
+
}
|
package/lib/route.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import { ACTIVITIES, rank, resolveRoute, routeSpec, sameRoute, TIERS } from './c
|
|
|
3
3
|
import * as costs from './cost.mjs';
|
|
4
4
|
import { clip, completedSince, extractFacts } from './facts.mjs';
|
|
5
5
|
import { CONTEXT_FILL, decide, fitTier, initialState, sameTaskOf } from './policy.mjs';
|
|
6
|
+
import { canonicalModel } from './providers.mjs';
|
|
6
7
|
|
|
7
8
|
// Exact observed API snapshot, not a rule that merges different dated versions.
|
|
8
9
|
const SNAPSHOTS = { 'claude-haiku-4-5': 'claude-haiku-4-5-20251001' };
|
|
@@ -13,7 +14,9 @@ export const HISTORY_SHRINK = 0.8;
|
|
|
13
14
|
|
|
14
15
|
// Whether the engine served the requested model, allowing for a dated snapshot of it.
|
|
15
16
|
export function isSameModel(requested, served) {
|
|
16
|
-
|
|
17
|
+
if (typeof requested !== 'string' || typeof served !== 'string') return false;
|
|
18
|
+
const [a, b] = [canonicalModel(requested), canonicalModel(served)];
|
|
19
|
+
return a === b || SNAPSHOTS[a] === b;
|
|
17
20
|
}
|
|
18
21
|
|
|
19
22
|
// The engine may echo our own routed model on a continuation; any third model is its fallback.
|
|
@@ -120,14 +123,19 @@ export function nativeFacts(config, loop, { messages, prompt, effort, turnId, co
|
|
|
120
123
|
};
|
|
121
124
|
}
|
|
122
125
|
|
|
123
|
-
export function isModelAllowed(
|
|
124
|
-
if (
|
|
125
|
-
if (!Array.isArray(
|
|
126
|
+
export function isModelAllowed(requested, allowed, native) {
|
|
127
|
+
if (allowed === undefined) return true;
|
|
128
|
+
if (!Array.isArray(allowed) || !allowed.every((v) => typeof v === 'string')) return false;
|
|
129
|
+
const id = canonicalModel(requested);
|
|
130
|
+
const nativeModel = canonicalModel(native);
|
|
131
|
+
const available = allowed.map(canonicalModel);
|
|
126
132
|
if (available.includes('default') && id === nativeModel) return true;
|
|
127
133
|
const family = /^claude-(opus|sonnet|haiku)-/.exec(id)?.[1];
|
|
128
134
|
const specific = available.filter((v) => family && v.startsWith(`claude-${family}-`));
|
|
129
135
|
if (specific.length) return specific.some((v) => id === v || id.startsWith(`${v}-`));
|
|
130
|
-
return available.some(
|
|
136
|
+
return available.some(
|
|
137
|
+
(v) => id === v || (id.startsWith('claude-') && id.startsWith(`${v}-`)) || (family && v === family),
|
|
138
|
+
);
|
|
131
139
|
}
|
|
132
140
|
|
|
133
141
|
function economicConfig(config, loop) {
|
|
@@ -214,6 +222,7 @@ function routeTurn(config, loop, { facts, advice, pin, nativeModel, contextKnown
|
|
|
214
222
|
const model = specOf(cell);
|
|
215
223
|
return (
|
|
216
224
|
!shrinksUnmeasured(cell) &&
|
|
225
|
+
(!facts.hasImages || model.imageInput !== false) &&
|
|
217
226
|
isModelAllowed(model.id, availableModels, nativeModel) &&
|
|
218
227
|
!loop.ineligible.includes(model.id) &&
|
|
219
228
|
(!contextKnown || costs.nextContextTokens(facts) <= model.contextWindow * CONTEXT_FILL)
|
|
@@ -365,7 +374,11 @@ export function observedMove(config, loop, { observed, facts, contextKnown, now
|
|
|
365
374
|
|
|
366
375
|
// A tool continuation keeps the turn's route and activity; only the context, the model list, or the turn's own tool
|
|
367
376
|
// calls (observedMove) can move it.
|
|
368
|
-
export function continueRoute(
|
|
377
|
+
export function continueRoute(
|
|
378
|
+
config,
|
|
379
|
+
loop,
|
|
380
|
+
{ nativeModel, contextTokens, contextKnown, availableModels, effort, hasImages = false },
|
|
381
|
+
) {
|
|
369
382
|
if (!loop.decision) return loop;
|
|
370
383
|
// A fit to another tier serves this turn only: `loop.route` stays the route of `lastRoute`, as the tier does.
|
|
371
384
|
let decision = loop.decision;
|
|
@@ -388,6 +401,27 @@ export function continueRoute(config, loop, { nativeModel, contextTokens, contex
|
|
|
388
401
|
};
|
|
389
402
|
}
|
|
390
403
|
}
|
|
404
|
+
if (hasImages && decision.tier && routeSpec(config, runningRoute(config, decision))?.imageInput === false) {
|
|
405
|
+
const runningWindow = routeSpec(config, runningRoute(config, decision)).contextWindow;
|
|
406
|
+
const tier = [...TIERS].reverse().find((t) => {
|
|
407
|
+
const model = routeSpec(config, resolveRoute(config, t, decision.activity));
|
|
408
|
+
return (
|
|
409
|
+
model.imageInput !== false &&
|
|
410
|
+
isModelAllowed(model.id, availableModels, nativeModel) &&
|
|
411
|
+
!loop.ineligible.includes(model.id) &&
|
|
412
|
+
(contextKnown ? contextTokens <= model.contextWindow * CONTEXT_FILL : model.contextWindow >= runningWindow)
|
|
413
|
+
);
|
|
414
|
+
});
|
|
415
|
+
const route = tier ? resolveRoute(config, tier, decision.activity) : null;
|
|
416
|
+
decision = {
|
|
417
|
+
...decision,
|
|
418
|
+
tier: tier ?? null,
|
|
419
|
+
route,
|
|
420
|
+
model: route ? routeSpec(config, route).id : nativeModel,
|
|
421
|
+
effort: route ? costs.routeEffort(config, route, effort) : effort,
|
|
422
|
+
reason: 'model-unavailable',
|
|
423
|
+
};
|
|
424
|
+
}
|
|
391
425
|
if (!isModelAllowed(decision.model, availableModels, nativeModel) || loop.ineligible.includes(decision.model)) {
|
|
392
426
|
decision = {
|
|
393
427
|
...decision,
|
package/lib/savings.mjs
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
// records. The records under SAVINGS_PREFIX are undated history from before those, read and never written.
|
|
5
5
|
|
|
6
6
|
import { effortSharesCache, ratesAt } from './cost.mjs';
|
|
7
|
+
import { canonicalModel } from './providers.mjs';
|
|
7
8
|
import { afterResetOf, earlierSince, isRecord } from './records.mjs';
|
|
8
9
|
import { HISTORY_SHRINK, isSameModel } from './route.mjs';
|
|
9
10
|
|
|
@@ -20,8 +21,9 @@ const TTLS = ['5m', '1h'];
|
|
|
20
21
|
// suffix such as `[1m]`. Null when no entry prices it.
|
|
21
22
|
export function modelSpec(config, id) {
|
|
22
23
|
if (typeof id !== 'string') return null;
|
|
23
|
-
const bare = id
|
|
24
|
-
const dated = (model) =>
|
|
24
|
+
const bare = canonicalModel(id);
|
|
25
|
+
const dated = (model) =>
|
|
26
|
+
model.id.startsWith('claude-') && bare.startsWith(model.id) && /^-\d{8}$/.test(bare.slice(model.id.length));
|
|
25
27
|
return Object.values(config.models).find((model) => isSameModel(model.id, bare) || dated(model)) ?? null;
|
|
26
28
|
}
|
|
27
29
|
|
|
@@ -38,11 +40,33 @@ export function modelAlias(config, id) {
|
|
|
38
40
|
|
|
39
41
|
// One request's list price in USD: `counts` are its uncached input, cache read, cache write and output tokens, the
|
|
40
42
|
// writes at `writeMultiplier` times input. Long-context rates follow the whole prompt.
|
|
41
|
-
export function requestUsd(model, { input, cacheRead, cacheWrite, output }, writeMultiplier) {
|
|
43
|
+
export function requestUsd(model, { input, cacheRead, cacheWrite, output }, writeMultiplier, cacheMode = 'explicit') {
|
|
42
44
|
const rates = ratesAt(model, input + cacheRead + cacheWrite);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
45
|
+
const inputUsd =
|
|
46
|
+
cacheMode === 'disabled'
|
|
47
|
+
? rates.input * (input + cacheRead + cacheWrite)
|
|
48
|
+
: rates.input * (input + cacheWrite * writeMultiplier) + rates.cacheRead * cacheRead;
|
|
49
|
+
return (inputUsd + rates.output * output) / 1e6;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Actual observed counters can be priced without inventing the cache on another route.
|
|
53
|
+
export function observedReplyUsd(config, usage, ttl) {
|
|
54
|
+
const counts = {
|
|
55
|
+
input: usage?.input_tokens,
|
|
56
|
+
cacheRead: usage?.cache_read_input_tokens,
|
|
57
|
+
cacheWrite: usage?.cache_creation_input_tokens,
|
|
58
|
+
output: usage?.output_tokens,
|
|
59
|
+
};
|
|
60
|
+
const model = modelSpec(config, usage?.model);
|
|
61
|
+
const write = config.cache.mode === 'explicit' ? config.cache.writeMultiplier[ttl] : 1;
|
|
62
|
+
if (
|
|
63
|
+
!model ||
|
|
64
|
+
!Number.isFinite(model.output) ||
|
|
65
|
+
!Number.isFinite(write) ||
|
|
66
|
+
!Object.values(counts).every((n) => Number.isFinite(n) && n >= 0)
|
|
67
|
+
)
|
|
68
|
+
return null;
|
|
69
|
+
return requestUsd(model, counts, write, config.cache.mode);
|
|
46
70
|
}
|
|
47
71
|
|
|
48
72
|
const truthy = (value) => /^(1|true|yes|on)$/i.test(String(value ?? '').trim());
|
|
@@ -100,7 +124,7 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
|
|
|
100
124
|
const next = { total, at: now, onYours, ...identity };
|
|
101
125
|
// A cache simulated for another model or effort of yours is none of this one's: the new one starts cold.
|
|
102
126
|
const prior = state?.model === identity.model && (sharedEffort || state.effort === identity.effort) ? state : null;
|
|
103
|
-
const write = config.cache.writeMultiplier[ttl];
|
|
127
|
+
const write = config.cache.mode === 'explicit' ? config.cache.writeMultiplier[ttl] : 1;
|
|
104
128
|
if (
|
|
105
129
|
!servedSpec ||
|
|
106
130
|
!yoursSpec ||
|
|
@@ -109,16 +133,22 @@ export function compareReply(config, state, { usage, served, yours, ttl, now })
|
|
|
109
133
|
!Number.isFinite(write)
|
|
110
134
|
)
|
|
111
135
|
return { state: next, reply: null };
|
|
112
|
-
|
|
136
|
+
// Implicit caches have no guaranteed TTL or cross-effort hit rate. Only identical observed routes compare exactly.
|
|
137
|
+
if (config.cache.mode === 'automatic' && !(onYours && (prior?.onYours ?? true))) return { state: next, reply: null };
|
|
138
|
+
const routedUsd = requestUsd(servedSpec, counts, write, config.cache.mode);
|
|
113
139
|
let yoursUsd = routedUsd;
|
|
114
140
|
let servedOnYoursUsd = routedUsd;
|
|
115
141
|
if (!(onYours && (prior?.onYours ?? true))) {
|
|
116
142
|
const warm =
|
|
117
|
-
|
|
143
|
+
config.cache.mode !== 'disabled' &&
|
|
144
|
+
prior &&
|
|
145
|
+
now - prior.at < config.cache.ttlMs[ttl] &&
|
|
146
|
+
total >= prior.total * HISTORY_SHRINK &&
|
|
147
|
+
prior.total > 0;
|
|
118
148
|
const cacheRead = Math.max(counts.cacheRead, warm ? Math.min(prior.total, total - counts.input) : 0);
|
|
119
149
|
const single = { ...counts, cacheRead, cacheWrite: total - counts.input - cacheRead };
|
|
120
|
-
yoursUsd = requestUsd(yoursSpec, single, write);
|
|
121
|
-
servedOnYoursUsd = requestUsd(servedSpec, single, write);
|
|
150
|
+
yoursUsd = requestUsd(yoursSpec, single, write, config.cache.mode);
|
|
151
|
+
servedOnYoursUsd = requestUsd(servedSpec, single, write, config.cache.mode);
|
|
122
152
|
}
|
|
123
153
|
const modelUsd = servedOnYoursUsd - yoursUsd;
|
|
124
154
|
return {
|
|
@@ -149,7 +179,8 @@ export function unpricedCause(config, { usage, served, yours, ttl }) {
|
|
|
149
179
|
const [servedSpec, yoursSpec] = [modelSpec(config, served.model), modelSpec(config, yours.model)];
|
|
150
180
|
if (!servedSpec || !yoursSpec || !Number.isFinite(servedSpec.output) || !Number.isFinite(yoursSpec.output))
|
|
151
181
|
return 'model';
|
|
152
|
-
|
|
182
|
+
if (config.cache.mode === 'automatic') return 'ttl';
|
|
183
|
+
return config.cache.mode === 'disabled' || Number.isFinite(config.cache.writeMultiplier[ttl]) ? null : 'ttl';
|
|
153
184
|
}
|
|
154
185
|
|
|
155
186
|
// One main reply against every configured model as yours, at your effort, so the session readout can follow a
|
package/lib/subagents.mjs
CHANGED
|
@@ -180,15 +180,16 @@ export function addAgentReply(config, agent, usage, ttl) {
|
|
|
180
180
|
output: usage?.output_tokens,
|
|
181
181
|
};
|
|
182
182
|
const next = { ...agent, requests: agent.requests + 1 };
|
|
183
|
-
const write = config.cache.writeMultiplier[ttl];
|
|
183
|
+
const write = config.cache.mode === 'explicit' ? config.cache.writeMultiplier[ttl] : 1;
|
|
184
184
|
const actual = modelSpec(config, usage?.model);
|
|
185
185
|
const other = agent.pass ? null : modelSpec(config, agent.coreModel);
|
|
186
186
|
const priced = (spec) => spec && Number.isFinite(spec.output);
|
|
187
187
|
if (!Object.values(counts).every(isCount) || !Number.isFinite(write) || !priced(actual)) return next;
|
|
188
188
|
if (!agent.pass && !priced(other)) return next;
|
|
189
|
+
if (config.cache.mode === 'automatic' && other && actual !== other) return next;
|
|
189
190
|
next.pricedRequests += 1;
|
|
190
|
-
next.actualUsd += requestUsd(actual, counts, write);
|
|
191
|
-
if (other) next.otherUsd += requestUsd(other, counts, write);
|
|
191
|
+
next.actualUsd += requestUsd(actual, counts, write, config.cache.mode);
|
|
192
|
+
if (other) next.otherUsd += requestUsd(other, counts, write, config.cache.mode);
|
|
192
193
|
return next;
|
|
193
194
|
}
|
|
194
195
|
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexeiled/claude-router",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Native Claude Code
|
|
3
|
+
"version": "1.17.0",
|
|
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",
|
|
7
7
|
"repository": {
|
|
@@ -34,6 +34,7 @@
|
|
|
34
34
|
"typecheck": "tools/node_modules/.bin/tsc -p tsconfig.json",
|
|
35
35
|
"validate": "claude plugin validate .claude-plugin/plugin.json && claude plugin validate .claude-plugin/marketplace.json",
|
|
36
36
|
"test:plugin": "bash scripts/plugin-test.sh",
|
|
37
|
+
"test:providers": "node experiments/mod-router/scripts/provider-acceptance.mjs",
|
|
37
38
|
"format": "tools/node_modules/.bin/biome format --write .",
|
|
38
39
|
"docs:images": "node scripts/render-doc-images.mjs",
|
|
39
40
|
"docs:modes": "node scripts/mode-matrix.mjs",
|