@alexeiled/claude-router 0.4.2 → 0.5.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/docs/configuration.md +17 -13
- package/lib/config.mjs +12 -1
- package/lib/gateway.mjs +14 -0
- package/lib/rewrite.mjs +2 -1
- package/package.json +1 -1
- package/scripts/statusline.mjs +4 -1
- package/skills/setup/SKILL.md +8 -8
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "router",
|
|
3
|
-
"displayName": "
|
|
4
|
-
"version": "0.
|
|
3
|
+
"displayName": "jev-router",
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"description": "Auto-picks the right Claude model for each turn — small models for quick edits, mid-tier for code, frontier for hard problems. Uses Jev to classify each request.",
|
|
6
6
|
"author": { "name": "Alexei Ledenev", "url": "https://github.com/alexei-led" },
|
|
7
7
|
"repository": "https://github.com/alexei-led/claude-router",
|
package/docs/configuration.md
CHANGED
|
@@ -20,15 +20,14 @@ starts it again.
|
|
|
20
20
|
|
|
21
21
|
```json
|
|
22
22
|
{
|
|
23
|
-
"model": "jev-router",
|
|
23
|
+
"model": "jev-router[1m]",
|
|
24
24
|
"env": {
|
|
25
|
-
"ANTHROPIC_BASE_URL": "http://127.0.0.1:43170"
|
|
26
|
-
"CLAUDE_CODE_MAX_CONTEXT_TOKENS": "1000000"
|
|
25
|
+
"ANTHROPIC_BASE_URL": "http://127.0.0.1:43170"
|
|
27
26
|
},
|
|
28
27
|
"modelPicker": {
|
|
29
28
|
"options": [
|
|
30
29
|
{
|
|
31
|
-
"model": "jev-router",
|
|
30
|
+
"model": "jev-router[1m]",
|
|
32
31
|
"label": "Jev Router (auto)",
|
|
33
32
|
"description": "Auto-selects the model and effort for each turn",
|
|
34
33
|
"behavesAs": "claude-opus-5-5"
|
|
@@ -47,11 +46,14 @@ routes it, and setup replaces that row.
|
|
|
47
46
|
|
|
48
47
|
Setup writes the file as its last step. Restart Claude Code after it. Until
|
|
49
48
|
the restart, the session sends `jev-router` to Anthropic and shows "There's an
|
|
50
|
-
issue with the selected model (jev-router)".
|
|
49
|
+
issue with the selected model (jev-router[1m])".
|
|
51
50
|
|
|
52
51
|
Claude Code does not know the model `jev-router`, so it assumes a 200K window.
|
|
53
|
-
`
|
|
54
|
-
|
|
52
|
+
The `[1m]` suffix declares 1M, the largest `contextWindow` of the default
|
|
53
|
+
routes; Claude Code strips it before the request. `CLAUDE_CODE_MAX_CONTEXT_TOKENS`
|
|
54
|
+
does not work here: Claude Code ignores it for this model. With the suffix,
|
|
55
|
+
Claude Code sends the 1M context beta header on every request; the gateway
|
|
56
|
+
drops it for a model with a smaller window (Haiku answers it with 400). The gateway sends a turn
|
|
55
57
|
only to a model whose window holds the context with room to spare (80%). A
|
|
56
58
|
large session skips Haiku (200K) and goes to Sonnet or Opus. The
|
|
57
59
|
`/router:status` reason for such a turn is `context-fit`.
|
|
@@ -138,15 +140,17 @@ frontmatter of `skills/<tier>/SKILL.md`. A test makes sure that they agree.
|
|
|
138
140
|
|
|
139
141
|
### models
|
|
140
142
|
|
|
141
|
-
| Alias | ID | Input | Cache Read | Window | Billing | Efforts |
|
|
142
|
-
| -------- | ------------------ | ----- | ---------- | ------ | ------- | ------------- |
|
|
143
|
-
| `opus` | `claude-opus-5-5` | $4 | $0.2 | 1M | plan | all |
|
|
144
|
-
| `sonnet` | `claude-sonnet-5` | $2 | $0.2 | 1M | plan | low–xhigh–max |
|
|
145
|
-
| `haiku` | `claude-haiku-4-5` | $1 | $0.1 | 200k | plan | none |
|
|
143
|
+
| Alias | ID | Input | Cache Read | Window | Max output | Billing | Efforts |
|
|
144
|
+
| -------- | ------------------ | ----- | ---------- | ------ | ---------- | ------- | ------------- |
|
|
145
|
+
| `opus` | `claude-opus-5-5` | $4 | $0.2 | 1M | as sent | plan | all |
|
|
146
|
+
| `sonnet` | `claude-sonnet-5` | $2 | $0.2 | 1M | as sent | plan | low–xhigh–max |
|
|
147
|
+
| `haiku` | `claude-haiku-4-5` | $1 | $0.1 | 200k | 64k | plan | none |
|
|
146
148
|
|
|
147
149
|
`id` is the model id that the gateway sends to Anthropic. `input` and
|
|
148
150
|
`cacheRead` are list prices in USD per million tokens. `contextWindow` is the
|
|
149
|
-
size of the context window in tokens. `
|
|
151
|
+
size of the context window in tokens. `maxOutput`, when set, caps the
|
|
152
|
+
`max_tokens` that Claude Code sends; the API rejects a request above the
|
|
153
|
+
model's output limit. `billing` is `plan` for models that use
|
|
150
154
|
the subscription limits, or `credits` for models that bill usage credits.
|
|
151
155
|
`policy.cashCapUsd` applies to `credits` models only. `efforts` lists the
|
|
152
156
|
levels that the model accepts. An empty list means that the gateway removes
|
package/lib/config.mjs
CHANGED
|
@@ -39,7 +39,16 @@ export const DEFAULTS = {
|
|
|
39
39
|
billing: 'plan',
|
|
40
40
|
efforts: EFFORTS,
|
|
41
41
|
},
|
|
42
|
-
|
|
42
|
+
// `maxOutput` caps `max_tokens`: Claude Code asks for 128K, Haiku 4.5 answers more than 64K with 400.
|
|
43
|
+
haiku: {
|
|
44
|
+
id: 'claude-haiku-4-5',
|
|
45
|
+
input: 1,
|
|
46
|
+
cacheRead: 0.1,
|
|
47
|
+
contextWindow: 200_000,
|
|
48
|
+
maxOutput: 64_000,
|
|
49
|
+
billing: 'plan',
|
|
50
|
+
efforts: [],
|
|
51
|
+
},
|
|
43
52
|
},
|
|
44
53
|
cache: {
|
|
45
54
|
writeMultiplier: { '5m': 1.25, '1h': 2 },
|
|
@@ -108,6 +117,8 @@ function validate(config) {
|
|
|
108
117
|
throw new Error(`models.${alias}.${field} must be a non-negative number`);
|
|
109
118
|
}
|
|
110
119
|
if (typeof model.id !== 'string' || !model.id) throw new Error(`models.${alias}.id is required`);
|
|
120
|
+
if (model.maxOutput !== undefined && !(Number.isInteger(model.maxOutput) && model.maxOutput > 0))
|
|
121
|
+
throw new Error(`models.${alias}.maxOutput must be a positive integer`);
|
|
111
122
|
if (!['plan', 'credits'].includes(model.billing))
|
|
112
123
|
throw new Error(`models.${alias}.billing must be plan or credits`);
|
|
113
124
|
if (!Array.isArray(model.efforts) || model.efforts.some((e) => !EFFORTS.includes(e)))
|
package/lib/gateway.mjs
CHANGED
|
@@ -11,6 +11,19 @@ import { ROUTER_DISPLAY_NAME, STATUS_PATH, statusSnapshot } from './status.mjs';
|
|
|
11
11
|
const HOP_BY_HOP = new Set(['host', 'connection', 'content-length', 'accept-encoding', 'transfer-encoding']);
|
|
12
12
|
const LOOPBACK = new Set(['127.0.0.1', 'localhost', '[::1]']);
|
|
13
13
|
const MESSAGES_PATH = '/v1/messages';
|
|
14
|
+
const LONG_CONTEXT_BETA = /^context-1m-/;
|
|
15
|
+
const LONG_CONTEXT_WINDOW = 1_000_000;
|
|
16
|
+
|
|
17
|
+
// `jev-router[1m]` makes Claude Code send the 1M context beta on every request; a model with a smaller
|
|
18
|
+
// window answers it with 400 ("The long context beta is not yet available"), so drop it for those.
|
|
19
|
+
function withoutLongContextBeta(headers, modelId, config) {
|
|
20
|
+
const model = Object.values(config.models).find((m) => m.id === modelId);
|
|
21
|
+
const beta = headers['anthropic-beta'];
|
|
22
|
+
if (!model || model.contextWindow >= LONG_CONTEXT_WINDOW || typeof beta !== 'string') return;
|
|
23
|
+
const kept = beta.split(',').filter((flag) => !LONG_CONTEXT_BETA.test(flag.trim()));
|
|
24
|
+
if (kept.length) headers['anthropic-beta'] = kept.join(',');
|
|
25
|
+
else delete headers['anthropic-beta'];
|
|
26
|
+
}
|
|
14
27
|
|
|
15
28
|
export function createGateway({
|
|
16
29
|
router,
|
|
@@ -66,6 +79,7 @@ export function createGateway({
|
|
|
66
79
|
for (const [name, value] of Object.entries(req.headers)) if (!HOP_BY_HOP.has(name)) headers[name] = value;
|
|
67
80
|
headers.host = target.host;
|
|
68
81
|
headers['content-length'] = String(body.length);
|
|
82
|
+
if (routed) withoutLongContextBeta(headers, routed.body.model, router.config);
|
|
69
83
|
up = send(
|
|
70
84
|
{ host: target.hostname, port: target.port || undefined, path: req.url, method: req.method, headers },
|
|
71
85
|
(upRes) => relay(upRes, res, { routed, session, turn, clientLeft: () => closedEarly }),
|
package/lib/rewrite.mjs
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// Turn a request for the router alias into a request for a concrete model. Only `model`,
|
|
1
|
+
// Turn a request for the router alias into a request for a concrete model. Only `model`, `max_tokens`,
|
|
2
2
|
// `output_config.effort`, `thinking` and thinking edits in `context_management` change;
|
|
3
3
|
// system, tools and messages are never touched.
|
|
4
4
|
import { EFFORTS } from './config.mjs';
|
|
@@ -7,6 +7,7 @@ export function rewriteRequest(body, tier, config) {
|
|
|
7
7
|
const route = config.routes[tier];
|
|
8
8
|
const model = config.models[route.model];
|
|
9
9
|
const out = { ...body, model: model.id };
|
|
10
|
+
if (model.maxOutput && out.max_tokens > model.maxOutput) out.max_tokens = model.maxOutput;
|
|
10
11
|
if (model.efforts.length === 0) return withoutThinking(out, body.output_config);
|
|
11
12
|
const effort = clampEffort(route.effort ?? body.output_config?.effort, model.efforts);
|
|
12
13
|
if (effort) out.output_config = { ...(body.output_config ?? {}), effort };
|
package/package.json
CHANGED
package/scripts/statusline.mjs
CHANGED
|
@@ -17,7 +17,10 @@ try {
|
|
|
17
17
|
} catch {}
|
|
18
18
|
|
|
19
19
|
const names = [config.gateway.alias, LEGACY_ALIAS, ROUTER_DISPLAY_NAME];
|
|
20
|
-
|
|
20
|
+
// `jev-router[1m]`: the suffix only tells Claude Code the window size.
|
|
21
|
+
const routed = [payload.model?.id, payload.model?.display_name].some((name) =>
|
|
22
|
+
names.includes(name?.replace(/\[1m\]$/, '')),
|
|
23
|
+
);
|
|
21
24
|
const [status] = await Promise.all([
|
|
22
25
|
routed ? fetchStatus(config.gateway.port, payload.session_id) : null,
|
|
23
26
|
command ? runWrapped(command, args, input) : null,
|
package/skills/setup/SKILL.md
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: setup
|
|
3
|
-
description: Point Claude Code at the router gateway. Adds `model
|
|
3
|
+
description: Point Claude Code at the router gateway. Adds `model` with the 1M window, `ANTHROPIC_BASE_URL` and the `/model` picker row to the user settings, and offers the status line.
|
|
4
4
|
disable-model-invocation: true
|
|
5
5
|
allowed-tools: Read, Edit, Write
|
|
6
6
|
---
|
|
7
7
|
|
|
8
|
-
Configure Claude Code for the router gateway. This session does not use the gateway until Claude Code restarts. When the file has `model: "jev-router"`, this session can fail its next request. So ask all questions first, write the file once, and write it last.
|
|
8
|
+
Configure Claude Code for the router gateway. This session does not use the gateway until Claude Code restarts. When the file has `model: "jev-router[1m]"`, this session can fail its next request. So ask all questions first, write the file once, and write it last.
|
|
9
9
|
|
|
10
10
|
1. Read `~/.claude/settings.json`. If the file does not exist, start from `{}`. Read `~/.claude/router.json` if it exists.
|
|
11
11
|
2. The status line can show the model and effort of the last routed turn. The command is `node ${CLAUDE_PLUGIN_ROOT}/scripts/statusline.mjs`, followed by the current status line command if there is one (for example `node ${CLAUDE_PLUGIN_ROOT}/scripts/statusline.mjs claude-powerline`). Show the user the current `statusLine` value and the new one, and ask. Change `statusLine.command` only if the user agrees. Keep the other `statusLine` keys.
|
|
12
12
|
3. Prepare the new settings object in memory:
|
|
13
|
-
- `model`: `"jev-router"`.
|
|
13
|
+
- `model`: `"jev-router[1m]"`. Claude Code does not know the model `jev-router` and assumes a 200K window; the `[1m]` suffix declares 1M. Claude Code strips the suffix before it sends the request. If `router.json` routes no model with a 1M `contextWindow`, use `"jev-router"`.
|
|
14
14
|
- `env.ANTHROPIC_BASE_URL`: `"http://127.0.0.1:43170"`. If `router.json` sets `gateway.port`, use that port.
|
|
15
|
-
- `env.CLAUDE_CODE_MAX_CONTEXT_TOKENS
|
|
16
|
-
- The `/model` picker row. In `modelPicker.options`, replace the row whose `model` is `"jev-router"` or `"router"` (the name before 0.4.2), or append it if there is none:
|
|
17
|
-
`{ "model": "jev-router", "label": "Jev Router (auto)", "description": "Auto-selects the model and effort for each turn", "behavesAs": "claude-opus-5-5" }`.
|
|
15
|
+
- Remove `env.CLAUDE_CODE_MAX_CONTEXT_TOKENS`. Older versions of this setup wrote it; Claude Code ignores it for `jev-router`. The gateway sends a turn only to a model whose window holds the context, and drops the 1M beta header for a model with a smaller window.
|
|
16
|
+
- The `/model` picker row. In `modelPicker.options`, replace the row whose `model` is `"jev-router[1m]"`, `"jev-router"` or `"router"` (the name before 0.4.2), or append it if there is none:
|
|
17
|
+
`{ "model": "jev-router[1m]", "label": "Jev Router (auto)", "description": "Auto-selects the model and effort for each turn", "behavesAs": "claude-opus-5-5" }`.
|
|
18
18
|
If `modelPicker` does not exist, set it to `{ "options": [<the row>] }`. Keep the other rows and `replaceBuiltInOptions`. `behavesAs` names a model that Claude Code knows; without it, Claude Code rejects `jev-router` as a model that is not in its catalog. If `router.json` changes the `high` route, use the `id` of its model.
|
|
19
19
|
- Remove `env.ANTHROPIC_CUSTOM_MODEL_OPTION`, `env.ANTHROPIC_CUSTOM_MODEL_OPTION_NAME` and `env.ANTHROPIC_CUSTOM_MODEL_OPTION_DESCRIPTION`. Older versions of this setup wrote them; the `modelPicker` row replaces them.
|
|
20
20
|
- The `statusLine` change from step 2, if the user agreed.
|
|
21
21
|
- Keep every other key unchanged.
|
|
22
22
|
4. Tell the user, before you write the file:
|
|
23
|
-
- Restart Claude Code now. Until the restart, this session can show "There's an issue with the selected model (jev-router)", because it still sends requests to Anthropic and not to the gateway.
|
|
23
|
+
- Restart Claude Code now. Until the restart, this session can show "There's an issue with the selected model (jev-router[1m])", because it still sends requests to Anthropic and not to the gateway.
|
|
24
24
|
- After the restart, the router serves each turn, and `/router:status` shows the routes and the last turn.
|
|
25
25
|
- The status line path contains the plugin version. After a plugin update, run `/router:setup` again.
|
|
26
|
-
- To stop the routing, remove `model`, `env.ANTHROPIC_BASE_URL
|
|
26
|
+
- To stop the routing, remove `model`, `env.ANTHROPIC_BASE_URL` and the `jev-router[1m]` row of `modelPicker.options`, and restore the status line command.
|
|
27
27
|
5. Write the whole object to `~/.claude/settings.json` with one Write call. Do not use a sequence of edits. Do nothing after this step.
|
|
28
28
|
|
|
29
29
|
Do not change any other file.
|