Model-Router: Run Claude Code Subagents on Open-Weight Models — Part 1
A reverse-proxy pattern that reads the model field of every request before anything else runs — Claude traffic goes straight to Anthropic untouched, everything else gets rerouted to cheap open-weight models, with the Claude-bypass coded as an invariant instead of a convention.
Running a fleet of Claude Code subagents gets expensive fast once more than a
couple of them are doing genuinely mechanical work — running the test suite,
editing docs, applying bulk lint-shaped fixes. The obvious move is to shift
that templated work off Claude and onto a cheap open-weight model instead.
The obvious move is also where the real pain starts. Point everything at one
shared multi-model gateway and nothing structurally stops a genuine Claude
request from silently slipping down the same path meant for open-weight
traffic — and that quietly breaks in two separate ways at once. Billing
breaks first: a flat Claude subscription and a metered OpenRouter passthrough
of the same models are not the same bill, so a leaked request double-bills
without anyone noticing until the invoice arrives. Auth breaks second, and
worse: requests bound for Anthropic are supposed to keep their real
OAuth/API-key credentials untouched, while requests bound for OpenRouter get
those credentials swapped for an OpenRouter key — so a Claude request that
leaks through the wrong branch would run with its credentials silently
replaced. Underneath both failures sits a third, quieter one: nothing stops a
hand-edited model: field in a subagent’s own file from drifting out of sync
with whatever routing intent was written down anywhere else, so the
documentation and the code can silently disagree for weeks before anything
forces the mismatch into view.
This is Part 1 of a two-part series on running a mixed Claude + open-weight subagent fleet without either failure mode creeping in. Part 2, published separately, covers a LiteLLM-based variant of the same problem — this entry sticks to a hand-rolled reverse proxy. The pattern below (call it Model-Router) is the shape that survived: route by the model name in the request, never by which agent is asking, with the Claude-bypass as the very first, unconditional check the proxy runs.
Architecture: how Model-Router decides before anything else happens
The clearest way to see the shape is the same way it actually runs — a single Claude Code turn that fans two generic subagents out to two different backends in one dispatch. Click through the steps below.
Both dispatches in that turn hit the exact same proxy. docs-writer’s
request falls through the Claude-bypass check (no claude-/anthropic/
prefix), gets resolved against the routing table, and is forwarded to
OpenRouter with its auth swapped in — that’s the one metered step, priced
here at Z.ai’s own current GLM-5.2 output rate ($4.40 per million tokens).
reviewer’s request hits the same proxy a moment later, but its model
string matches the Claude-bypass immediately — no table lookup, no swapped
auth, no metered tick, because it never leaves the flat Claude subscription
in the first place. The diagram is built so that distinction is something
you watch happen (the token/cost meter ticks once and then holds), not just
a claim in the prose.
The companion repo behind this diagram (a generic proxy skeleton, a routing table, a generator, and a lint gate) is real and runnable — see Install below to clone it and run it yourself.
Why the bypass has to be code, not a convention
There are three separate, concrete reasons the Claude-bypass has to be a coded, tested, first-checked invariant rather than a documented convention someone is expected to remember:
- Billing correctness. A flat Claude subscription/OAuth and a metered OpenRouter passthrough of the same underlying models are priced completely differently. Any leaked request silently double-bills, and nothing about a shared gateway’s normal operation would ever surface that.
- Auth safety. Requests bound for Anthropic pass their real OAuth/API-key credentials straight through, untouched. Requests bound for OpenRouter get those credentials swapped for an OpenRouter key. A leaked Claude request would silently run under swapped-out credentials — the kind of failure that doesn’t announce itself.
- Blast-radius containment. A runtime “switch the active model” override is meant to affect only non-Claude traffic. If the Claude-check isn’t structurally first and unconditional — ahead of that override, ahead of any per-session model switch — a mistyped override value could in theory catch Claude traffic too.
None of these are things a person needs to remember correctly forever. They’re things the first line the proxy runs has to get right, every time, before anything else executes.
Three attempts before this held
This wasn’t the first design — it’s the third. Click through each stage: what was tried, what broke, what it led to.
Pointed ANTHROPIC_BASE_URL at a generic multi-model gateway and trusted it to sort Claude calls from everything else.
Nothing enforced the split structurally. There was no invariant standing between a genuine Claude request and the third-party path — just an assumption that the gateway would sort it out.
Trusting a shared gateway's behavior isn't a safeguard, it's a hope. The Claude-bypass has to be a check the code always runs, not a property a multi-model gateway happens to exhibit today.
Try routing a request yourself
Type or pick a model string and watch it hit the Claude-bypass check first — then branch to the real Anthropic API with untouched auth, or to the secondary backend with auth swapped, or get denied outright if it matches neither.
One table, two consumers: a generator and a lint gate
The routing intent lives in exactly one place — a small table mapping agent name to model slug. Everything else is generated from that table, never hand-maintained alongside it:
// routes.json — the single source of truth
{
"coder": "z-ai/glm-5.2",
"reviewer": "claude-sonnet-5",
"docs-writer": "z-ai/glm-5.2"
}
The proxy itself never asks “which agent is this?” — it only ever looks at the model name already resolved into the request, with the Claude check running first, unconditionally, before the table is even consulted:
# model_router.py — a generic reverse-proxy skeleton
CLAUDE_PREFIX = ("claude-", "anthropic/")
def route(request):
model = request["model"]
if model.startswith(CLAUDE_PREFIX):
# unconditional, first check — real auth passes through untouched
return forward_to_anthropic(request)
# no Claude match: forward to the swappable secondary backend
return forward_to_secondary(request, auth=swap_auth(OPENROUTER_API_KEY))
A generator stamps model: frontmatter into every subagent definition file
from the table — never the other way around:
# generate_agent_frontmatter.py
table = json.load(open("routes.json"))
for agent, model in table.items():
stamp_frontmatter(f"agents/{agent}.agent.md", model=model)
And a lint check regenerates that frontmatter into a scratch location and
diffs it against what’s actually committed, failing the build on any
mismatch — the enforced rule is: never hand-edit a subagent’s model:
field, edit the table and regenerate instead:
# lint_routes.py — fails the build on drift
scratch = regenerate_frontmatter_from(table="routes.json")
committed = read_committed_frontmatter("agents/")
if scratch != committed:
sys.exit("routing table and committed agent files disagree — "
"regenerate and commit, never hand-edit model: directly")
This is “code is law” applied to infra: a routing decision that’s mechanically checkable belongs in code that runs on every build, not in a comment telling someone to remember something.
The shape of it
It’s a small proxy: a routing table (routes.json) maps each subagent role to
a model, a generator writes every subagent’s model: frontmatter from that
table — never hand-edited — and lint_routes.py runs as a build gate to catch
drift before it ships. Point Claude Code (or any Anthropic-SDK-compatible
harness) at the proxy via ANTHROPIC_BASE_URL, and every dispatch resolves
through the table instead of whatever a subagent’s frontmatter happened to say.
Technicals
- Agentic harness: Claude Code
- Skill/pattern name: Model-Router
- Memory / RAG: None
This is the place to start if you’re running a mixed Claude + open-weight subagent fleet and want the enforcement, not just the idea. Part 2 of this series covers the same problem through a LiteLLM-based proxy instead of a hand-rolled one — worth reading once you’ve decided whether you want to own the routing logic yourself or lean on an existing gateway that already speaks both dialects.