Documentation
¶
Overview ¶
Package inferencerouter is the inference-router plugin: it sends a coding agent's new sessions to the inference server chosen for that agent, and keeps every session on the server it started on.
The agent needs no setting of its own for that beyond going through the proxy. A request addressed to a server's host is routed, and so is a routed agent's inference request addressed anywhere else — its provider, Anthropic, OpenCode Zen, Bob's gateway — which is captured: sent to the agent's server at the server URL's path prefix, then the server's path for its API format (see session.CapturePath), by how the path the agent named ends. Only a POST inference-parser read as inference is captured, since an agent's own tools can POST to an API whose path happens to end the same way.
The choice is plugin config, changed by editing it and letting the proxy hot-reload. For a request, the order is: the session's sessions: entry, the session's pin, the agent's agents: entry, the key_routes: entry of the server whose key the request sent, that server, not routed. So a request that sends a server's key, from an agent with no agents: entry, is routed to that server. A request with no User-Agent is not routed by its key.
Sessions are pinned in the process's durable store (storage.StoreConsumer) — on a local install a file under ~/.cortex — which neither a reload nor a restart loses, so changing an agent's server moves only the sessions it has not started yet. A process with no durable store pins in its process-scoped store (pctx.Shared), which a reload keeps and a restart empties.
Place it last in the outbound chain, which is where agentop puts it. A redirect moves pctx.Host, so the plugins before it decide on the host the client asked for and any plugin after it would see the server's host instead. Nothing should follow it that keys on the host.
A routed request is always redirected to its server's own scheme and host, even when it already names that host. pctx.Host is the request's Host header, the client's word, and on a TLS-bridged request the forward proxy dials the CONNECT authority, which need not be that host. A router that skipped the redirect because the Host header already named the server would hand the server's key to whatever the client CONNECTed to. With the redirect the listener dials RedirectTarget, the server, whatever the Host header or the CONNECT said.
The server's key replaces the client's only when pctx.Redirected() reports that the redirect took effect. Under on_error: observe, Redirect returns nil and moves nothing, so a key set on the strength of that nil would go to the host the client named.
A session is pinned by where the request that decides it went, not by the choice made for it: to the server when the request was redirected there; to "not routed" when it stayed where the client sent it, because the agent is not routed or because the router runs under on_error: observe; and not at all when the redirect failed or the request was refused for its model, so the session's next request decides again. A pin by choice would hold a session started under observe to a server it never used, and turning enforce on would then move it there mid-conversation, which is the switch the pin exists to prevent.
The first request the router sees is not always a session's first. A session already running when routing is first configured, quiet while it was, has no pin, and neither has one whose pin lapsed. The session's history tells such a session from a new one: on a pin miss for a routed agent, the latest earlier inference request, not a side request, that agent sent in the session decides. One to a server's host keeps the session on that server. One the router would capture, recorded where the agent sent it, was made while the router was not routing the agent — before it was routed, or under observe — so the session ran unrouted and is not routed now: switching an agent onto a server must not move a conversation already running on its provider. Any other request is no evidence. Only a session with no evidence is new and goes to its agent's current server. Only a non-empty history is evidence: a view is empty for a session the store has recorded nothing of yet.
With no pin and no history, a Claude Code inference request says itself whether the router is seeing its conversation begin. Its system prompt states the caller, and the main conversation carries its tool manifest: an opening turn has no assistant message yet and goes to the agent's current server, and a continuation has some, so the router did not see it begin — it started before routing was set up, or lost its pin to a restart of a process that keeps pins only in memory — and it is not routed and pinned so, rather than taken for new and moved mid-conversation. The one-shots Claude Code interleaves with a conversation carry no tools, and a subagent's requests are its own conversation's; neither says whether the session began, so neither decides: each leaves the session unpinned. A one-shot still goes to the agent's current server. It is no part of the conversation, so answering it there moves nothing, and a new session's first request can be one — Claude Code's title request reaches the router before the opening turn — which, left where Claude Code sent it, put a new session's title on the provider the agent was routed away from. A subagent's request is a conversation of its own, which may be running already — a /clear moves background subagents onto the new session id — so it stays where Claude Code sent it unless the router captured it. Only Claude Code is read this way, because no other agent states its role, and one that switches providers, as OpenCode does, sends earlier turns that went elsewhere. A session routed to a server keeps it by its pin and its history, so a restart that keeps neither sends such a conversation back to where Claude Code sends it. So does a move to a new session id — Claude Code's continued-in hand-off, or a background job forked from a conversation — which neither follows.
A pin is its agent's. The session id is the listener's answer, and another agent's request can be filed under it — by process attribution, which files a command an agent runs under that agent's session and is on by default on a laptop; by the ActiveSession fallback with client affinity off; or by a header id two clients share. Such a request is decided as if the session were unpinned, from its own agent's history and choice, and leaves the pin alone. Honouring the pin for it would hand it the session's server and key.
The listener's synthetic sessions are never pinned: the default bucket, and the pending:<agent> buckets an agent's calls collect in before its session is known. Each holds many conversations rather than one, so a pin would hold every later conversation filed there to the first one's server. Their requests follow the agent's current server, unpinned, as a request with no session does.
The model is the agent's to choose and the server's to answer, and the router does not read a model list to judge it: a server serves aliases it does not list. A routed request goes with the model the agent asked for. When the server refuses that model, the refusal is recorded for an hour and the request is sent again, once, before the agent sees anything, with the server's substitute: its main model for a request with tools, its helper for one without, each the newest model on the server's list whose name contains the word configured for it (routerconfig.Resolve). While the refusal is recorded, requests for that model are sent with the substitute from the start. A session keeps the substitute it was given, so a conversation never changes model part-way when a refusal lapses or a newer model is listed. A request already sent with a substitute is never sent again: a refused substitute reaches the agent, and the session drops it, so its next request is decided again.
A model the router sends to can have a smaller output limit or context window than the one the agent was built for. A refusal that states the limit — Bedrock's, Anthropic's and vLLM's wording, behind LiteLLM — is learned for the server and model, kept in the durable store for 30 days, and the request is fitted and sent again before the agent sees the refusal. Later requests are fitted before they are sent: the output field is lowered, never raised, nor to or below the body's thinking.budget_tokens; and once an Anthropic messages or OpenAI chat-completions request passes 85% of the window its oldest turns after the first user message are dropped to under 60%, with a note saying how many. A responses-format request only has its output field lowered, as does one with a single turn larger than the window. Only requests the router switched — to another host or another model — are fitted, and each kind of refusal is resent at most once per request.
Index ¶
- Constants
- type Router
- func (p *Router) Capabilities() pipeline.PluginCapabilities
- func (p *Router) ConfigSchema() []pipeline.FieldSchema
- func (p *Router) Configure(raw json.RawMessage) error
- func (p *Router) Init(context.Context) error
- func (p *Router) Name() string
- func (p *Router) OnRequest(_ context.Context, pctx *pipeline.Context) pipeline.Action
- func (p *Router) OnResponse(_ context.Context, _ *pipeline.Context) pipeline.Action
- func (p *Router) Resend(_ context.Context, pctx *pipeline.Context, status int, body []byte) bool
- func (p *Router) SetHistory(h session.History)
- func (p *Router) SetStore(st storage.Store)
- func (p *Router) Shutdown(context.Context) error
Constants ¶
const Name = "inference-router"
Name is the plugin's registered name.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is the plugin. Built by Configure; the zero value routes nothing.
func (*Router) Capabilities ¶
func (p *Router) Capabilities() pipeline.PluginCapabilities
func (*Router) ConfigSchema ¶
func (p *Router) ConfigSchema() []pipeline.FieldSchema
ConfigSchema implements pipeline.SchemaProvider. servers and agents are maps, which pipeline.SchemaOf renders as type "unknown".
func (*Router) Configure ¶
func (p *Router) Configure(raw json.RawMessage) error
Configure decodes and validates the config, then builds the routing tables. A failed Configure leaves the previous tables in place; BuildWithDeps discards the instance anyway.
func (*Router) Init ¶
Init implements pipeline.Initializer: each server's model list is read back from the durable store, so a substitute resolves before the first fetch answers, and then fetched now and every catalogEvery until Shutdown.
func (*Router) OnRequest ¶
OnRequest routes one request. See the package doc for what it decides and why.
func (*Router) OnResponse ¶
func (*Router) Resend ¶
Resend implements pipeline.Resender. A refusal of the model the agent asked for is answered with the server's substitute, as decideResend says; a refusal for a limit teaches the router that limit for the model, and the request is fitted and sent again. decideResend decides which, and each kind is answered at most once per request.
func (*Router) SetHistory ¶
SetHistory implements session.HistoryConsumer: the router reads a session's archived events for where it went before this process, when the listener gives no table it can vouch for (see wentTo).
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package routerconfig is the inference-router plugin's configuration: its shape, its validation, and the host rules the plugin routes by.
|
Package routerconfig is the inference-router plugin's configuration: its shape, its validation, and the host rules the plugin routes by. |