Auto Routing
An auto routing policy is a model name you can call like any other model, such as auto/coder. Behind it, the gateway reads the first user message of each conversation, asks a small decision model which kind of request it is, and forwards the conversation to the model the administrator mapped to that kind. Simple work goes to a cheap, fast model; hard work goes to the strongest one. The choice is then kept for the whole session.
Coding agents such as Claude Code, Codex or Cursor cannot pass routing parameters in a request; they only know a model name. A policy packs the routing into that name.
Calling a policy
Use the policy name in the model field. It works on the Chat Completions, Claude Messages and Responses APIs, with streaming and tools, and it appears in GET /v1/models like any other model.
There are two kinds of policies, both listed under Routing policies in the workbench sidebar:
- Managed policies are published by the site administrators. Call them by their name, for example
auto/coder. - Your own policies, and those of your organization, are called with the
policy/prefix, for examplepolicy/coder. The prefix only reaches policies of your own key: a personal key reaches your personal policies, an organization key the organization's. Apolicy/name that matches none of them fails with model not found; it never falls through to a managed policy.
An own policy may have the same name as a managed one. The two never get in each other's way: policy/coder is always your own policy, coder always the managed one.
curl -X POST https://your-tokenpipes-domain.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-proj-1234567890" \
-d '{
"model": "auto/coder",
"messages": [{"role": "user", "content": "Add a --verbose flag to the CLI and log the parsed arguments"}]
}'The response model field is the model that actually answered (unless the site is configured to echo the requested name). Every routed response also carries a header:
X-TokenPipes-Route: policy=auto/coder; label=edit; model=claude-haiku-5-5; confidence=0.90; scoring=logprobs; reason=classifiedconfidence and scoring appear when the decision model picked a label. scoring says how the confidence was measured: decision (the probability a decision model gave the label), logprobs (the label's probability) or self_reported (see Scoring).
| Reason | Meaning |
|---|---|
classified | The decision model picked a label with confidence at or above the policy threshold. |
pinned | A later turn of a session that was already routed; no new decision was made. |
below_threshold | A label was picked but with too little confidence; the fallback model answered. |
decision_error | The decision call failed, timed out or answered with an unknown label; the fallback model answered. |
empty_input | The first user message had no text (for example only an image); the fallback model answered. |
auxiliary | A Claude Code side request (such as generating the session title) arrived before the session was routed; the fallback model answered and no decision was made. |
A request to a policy is never rejected because of routing: whenever the decision cannot be made, the fallback model answers.
Sessions
The decision is made once per session and reused for later turns, so a long conversation never switches models halfway and the upstream prompt cache keeps working. A session is recognised, in this order, by the Claude Code session in metadata.user_id, a session header (session_id, session-id, x-session-id, x-claude-code-session-id), the OpenAI prompt_cache_key, or the prompt prefix (system prompt plus first user message). Sending a stable session id is the most reliable way to keep a conversation on one model; a new id starts a new decision. Only a real classification is pinned: after a decision_error or empty_input fallback the next turn is classified again, so a brief outage of the decision model does not hold new sessions on the fallback model.
How long a session stays pinned is set per policy by the administrator (one hour by default).
Using a policy in coding agents
Claude Code
export ANTHROPIC_BASE_URL=https://your-tokenpipes-domain.com/claude
export ANTHROPIC_AUTH_TOKEN=sk-proj-1234567890
export ANTHROPIC_MODEL=auto/coder
claudeCodex CLI — set the model in ~/.codex/config.toml for the provider that points at the gateway:
model = "auto/coder"Cursor and other OpenAI-compatible clients — add the gateway as an OpenAI-compatible endpoint and enter auto/coder as a custom model name.
Coding agents add their own context to the conversation: Claude Code puts <system-reminder> blocks into the first message, and Codex sends <environment_context> and your AGENTS.md instructions as a separate message ahead of your prompt. The gateway strips these blocks and skips messages that contain nothing else, so the decision is based on what you actually typed. Claude Code also sends side requests with the same model, such as generating the session title; they carry no tools and are not classified. Before the session is routed they go to the fallback model; after that they follow the session.
Billing and logs
- The routed request is billed at the price of the model that answered, not at a price of the policy itself. On the model catalog, a policy shows "billed by target model" instead of its own prices.
- The decision call is billed to you once per session at the decision model's price, as its own log entry marked as a decision call. With automatic scoring, the first session on a channel that does not return logprobs makes two decision calls (one with logprobs, then one self-reported), and both are billed. The site administrator can turn decision billing off.
- In your logs, the routed request shows the policy, the chosen label, the confidence, the scoring method (with every label's probability when logprobs were used) and the reason; you can filter logs by routing policy.
Setting up a policy
Every user creates their own policies under Routing policies → Auto routing in the workbench sidebar (with an organization selected, owners and admins manage the organization's policies there). Site administrators publish managed policies under Platform admin → Gateway → Routing policies. A policy consists of:
- Model name — what clients call, for example
coder:policy/coderfor an own policy,coderfor a managed one. Letters, digits and. _ / -only; it must not start withpolicy/or collide with an existing model. Once created it shows up in/v1/modelsand can be added to token and organization allow lists like any model. Allow lists are checked against the policy name; the target models do not have to be in the caller's allow list, so you can keep expensive or hidden models reachable only through a policy. - Decision model — the model that classifies the first message. The picker lists decision models first, and they are the recommended choice: the gateway asks one multiple-choice question on
/v1/decisions(your question, with each label and its description as an option) and the model answers with a probability for every label. A small, fast chat model that follows instructions well also works. Image, embedding, speech and other models are refused, and it must not itself be a policy. - Question and routes — one question and two to sixteen labels, each with a short description and a target model. The decision model sees only the question and the label descriptions, never the target models, so write each description as a test it can apply to the message: "read code and answer a question; nothing is edited" works better than "cheap model". If two labels often fit the same message, the probability is shared between them and neither may reach the threshold; merge them or sharpen the descriptions, and check with Try it which labels compete. Seven templates fill the whole policy from the models you can use, among them the size of a code change, the kind of task for a coding agent, support triage, reasoning depth and the cost of a mistake.
- Scoring — how a chat model's confidence is measured; see Scoring. Auto is the default. A decision model always gives probabilities, so the setting is hidden for it.
- Fallback model and threshold — the fallback answers every request the decision cannot settle, whatever kind it is, so choose a model that handles all of your labels well: for a coding agent, usually the model you would use without a policy. The threshold (0 to 1, start at 0.5) is compared with the confidence of the chosen label. If the policy statistics show many
below_thresholdrequests, lower it or make the labels clearer; raise it when cheap targets get requests they cannot handle. With logprobs the confidence is a probability; a self-reported number only orders the labels. - Advanced — how long a session stays pinned (0 classifies every request), how many characters of the first message are sent to the decision model, and the decision timeout.
The Try it tab runs only the decision step on a pasted first message, optionally comparing several decision models side by side, without forwarding, billing or pinning anything. Each result shows the scoring method and, with logprobs, every label's probability. Test with real first messages from your logs before enabling a policy.
Scoring
A decision model needs no scoring setting: its answer gives every label a probability, and the threshold is compared with the chosen label's probability. A chat model can report its confidence in two ways:
- Logprobs — the labels are presented as lettered options and the model answers with one letter. The gateway reads the model's output probability for each letter, so every label gets a real probability and the threshold means what it says. This needs a non-reasoning model on a channel that returns logprobs: OpenAI-compatible APIs such as GPT-4.1, DeepSeek in non-thinking mode, or a self-hosted vLLM server. Claude, reasoning models and most aggregator gateways do not return logprobs.
- Self-reported — the model answers with a label and a confidence number it chooses itself. It works with any chat model, but the number is not calibrated.
| Setting | Behaviour |
|---|---|
| Auto (default) | Uses logprobs. On a channel where the decision model does not return them, that request is decided again with self-reported confidence, and the channel uses self-reported confidence for the next 24 hours. |
| Logprobs only | Uses logprobs; on a channel that does not return them, the fallback model answers. |
| Self-reported only | Always uses self-reported confidence. |
Check support next to the setting sends one small test request to the decision model and tells you whether the channel it was routed to returns logprobs.
The decision billing switch is under Platform admin → Platform settings → Gateway → Routing policies.
Fallback policies
A fallback policy is a model name for a list of models tried in order, for example auto/steady → claude-opus-5-5, then claude-sonnet-5-5. Every request goes to the first model. Only when every line of a model has failed (or none can take the request) does it move on to the next model, which gets its own lines and retries. Choose Fallback at the top of New policy; it needs a name and two to eight models, and starts with the strongest model and a balanced one behind it.
- The request does not move on when the client has gone, when the retry time is used up, when a streamed answer has already started, or for errors of the request itself, such as an insufficient balance.
- The request is billed at the price of the model that answered. Its log shows the policy and
routing_step(0 for the first model); theX-TokenPipes-Routeheader carriesstep=once the request has moved on. - A fallback policy has no decision model, so it has no Try it tab, threshold or session pinning.
- An auto policy falls back the same way: when every line of the chosen target fails, the request moves on to the policy's fallback model.
Fallback models in the request
Without a policy, a request can list fallback models of its own in a top-level models array, the way OpenRouter takes it:
{"model": "claude-opus-5-5", "models": ["claude-sonnet-5-5", "gpt-5-mini"], "messages": [...]}The listed models are tried in order with the same rules as a fallback policy. Each must be a model your key could call by itself: models outside the key's or organization's allow list, hidden models and routing policies are dropped. Up to eight are used. The field is removed before the request goes upstream, on every API.
Personal and organization policies
A site administrator turns own policies on or off with Let users and organizations create their own routing policies under Platform admin → Platform settings → Gateway → Routing policies (on by default). The same card sets how many policies each user (20 by default) and each organization (20 by default) may create.
- Personal policies are created by any signed-in user and only apply to that user's personal API keys. They also show up in the user's Playground and model lists as
policy/<name>. - Organization policies are managed by the organization's owners and admins while the organization is selected. Members see them read-only on the same page and call them with an organization API key.
- The decision model, targets and fallback must be models the key's account (the user, or the organization) can already call and that the catalog does not hide. If the site hides a target later, requests go to the fallback (reason
target_unavailable). If the fallback is hidden too, the request fails with model not found. - Try it and Check support call the decision model for real and are charged to your balance (personal) or the organization (organization policies). They do not count in the policy statistics. Replaying first messages from the logs is available for your personal policies, not for organization ones.
- The Managed tab lists the managed policies your key can call.
- Creating, changing, switching and deleting an organization policy is recorded in the organization's audit log.
Site administrators can review own policies by setting Scope to Organizations or Personal on Platform admin → Gateway → Routing policies. The list shows the owner of each policy. These policies are read-only there: an administrator can open one and switch it off, but not edit or delete it.

