Advanced routing
Run the vLLM Semantic Router, let the classifier read more of the conversation, keep sensitive requests local, and read routing metrics and audit rows.
This page covers routing options beyond the first setup: the vLLM Semantic Router classifier, conversation context, sensitive requests and what routing records.
Configure the vLLM Semantic Router
DefenseClaw runs a pinned vLLM Semantic Router container and asks it to classify each request. It needs Docker running Linux containers (macOS or Linux), or a remote router.
Add a routing block
name is the alias the router returns; decisions pick one by keyword.
routing:
enabled: true
version: "0.3.0"
port: 8080
algorithm: static
models:
- name: fast
provider: openai
model: gpt-4o-mini
base_url: https://api.openai.com
api_key_env: OPENAI_API_KEY
- name: deep
provider: openai
model: gpt-4.1
base_url: https://api.openai.com
api_key_env: OPENAI_API_KEY
signals:
keywords:
- name: code-request
keywords: ["debug", "implement", "refactor"]
operator: OR
decisions:
- name: code
priority: 100
conditions:
- type: keyword
name: code-request
model_refs: [deep]
- name: default
priority: 1
model_refs: [fast]Store the backend key
defenseclaw keys set OPENAI_API_KEYEnable the router and restart the gateway
defenseclaw setup routing --enable
defenseclaw-gateway restartTo use a router you run yourself, set routing.remote.endpoint (HTTPS for non-loopback endpoints) instead of starting the container.
What the router gets and how it runs:
- The classifier receives message roles and content. Keys, headers, tools and user ids are not sent.
- Port 8080 is the loopback classifier API, not a chat endpoint. Clients keep using their normal DefenseClaw connector endpoint.
- The managed container is the digest-pinned v0.3.0 image, with a read-only config mount, a read-only root filesystem, all Linux capabilities dropped and
no-new-privileges. vllm_srreads the newest message only, so it does not acceptrouting.context.
Route on the conversation
By default the classifier reads only the newest message the user typed. In an agent loop most requests end with a tool result, so the classifier sees the last thing the user said and nothing of the loop. routing.context lets the rules and systemone classifiers read a little more. It is off by default.
routing:
classifier: rules
context:
recent_user_turns: 2 # older user messages, 0 to 8
include_first_user_turn: true # the message that started the conversation
max_bytes: 8192 # older messages together, at most 65536
per_turn_bytes: 2048 # one older message, at most max_bytes
include_tool_names: true # names of the tools the loop calledWhat it reads:
- Read: user messages, and with
include_tool_namesthe tool names (no arguments or results; MCP tool names are chosen by the tool server). - Left out: tool results, assistant replies, system prompts, and user-role blocks that agents write with a known marker. Examples:
<system-reminder>,<environment_context>,<user_instructions>, command, hook and IDE tags,# AGENTS.md instructions,# CLAUDE.md, compaction summaries, skill loads and the OpenClaw context and startup messages. - Each content block is checked on its own, so a typed prompt in the same message as agent blocks is still read. Text an agent writes without such a marker can still be read.
- For a custom slash command the classifier reads what you typed after the command name and skips the expanded command file.
How it matches:
- The newest message keeps its 8 KiB limit.
per_turn_bytesandmax_bytesbound the older messages. - Keyword signals match the newest message and the context, newest first, one message per paragraph, with no recency weighting. An older message can match a higher-priority rule than the newest one.
- With
include_tool_namessignals also see a line such astools: Bash,Read, and the request conditiontool_loopmatches when the conversation called a tool. routing.sensitive.signalsmatch the newest message only.systemonegets the same context, at most 8 KiB.- When a limit cuts the context, the decision carries the warning
context_truncated:bytes(itemspast 32 tool names), shown bydefenseclaw routing explain. The newest message is never dropped, and a cut never removes a signal the newest message matched.
Sensitive requests stay local
Set routing.sensitive with aliases plus severity_at_least, signals or both. A request that the guardrail severity or a listed signal marks as sensitive is then sent only to a routing.sensitive.aliases model. It is never sent to the model the agent asked for, or to another alias, on any install.
What is checked:
- The newest user turn (its first 8 KiB). Tool results and earlier turns are not read.
- Once a turn is sensitive, the following turns of that conversation stay local too, because they carry it in their history. Each of them renews the mark, which ends 12 hours after the last such turn.
- Marks are kept in memory, so a gateway restart clears them.
- A conversation is the agent session id (the DefenseClaw session header, or the Claude Code or Codex session header). A request without one is judged alone.
- The check is best effort: keywords and the regex guardrail miss personal data written in prose. See Ask the decision model too.
| Case | What happens |
|---|---|
No local model can take it: its key is missing, it does not speak the request API, an administrator rule does not allow it, it is unreachable on the route-only listener, or the request is a signed Bedrock body or an API DefenseClaw does not re-route (such as count_tokens) | Refused with routing_sensitive_local_unavailable and a message that names the cause. An audit row is written (action routing.sensitive_refused, target routing.sensitive), and defenseclaw routing explain <id> shows denied. |
The local model refuses the request, for example Ollama and the additional_tools input item Codex sends when its catalog entry uses use_responses_lite | Not replaced by the cloud model. The agent gets HTTP 400 with a message that names routing.sensitive, the local alias and the model's own reason. Use a local model that accepts the request, or a Codex catalog entry without use_responses_lite. |
A keyless local model: its own loopback base_url and no api_key_env or credential (Ollama, vLLM on this host) | Called with no credential, also when the agent signs in with a login or a Bedrock bearer key. A sensitive request tries its local models this way at any address. |
| A body over 64 MiB on the route-only listener | Refused with HTTP 413. While its shared request buffer is full it answers HTTP 503 with Retry-After, instead of sending the request unchecked. |
Ask the decision model too
The regex floor finds structured data (card and ID numbers, keys, tokens) but misses personal data written in prose, such as diagnoses, HR cases or unreleased numbers. Set routing.sensitive.check: auto (or decision_model or judge) to also ask the decision model one yes/no question per request. A positive from either check keeps the request local.
- The check adds a model call to every routed request, because the route waits for it. Benchmarks: model routing has the measured effect and latency.
- A check that fails or times out prefers the local models (
on_unavailable: local) and never blocks. When no local model can take the request, it keeps its normal route. - A profile whose
allowed_aliasesare local models only leaks nothing in any mode. Use that for regulated groups. - On a managed host the administrator sets and locks
routing.sensitivein the bundle.
Privacy. The check sends request text to the decision model, or, with judge or auto and no decision model, to the guardrail judge, on every routed request. On the route-only and passthrough listeners that is the newest user turn; on the chat listener the newest turns up to max_bytes; never system prompts. Choose a local or in-tenant endpoint for it.
Metrics and audit rows
Each routed request records one defenseclaw.semantic_routing.decision count and a defenseclaw.semantic_routing.latency measurement, from both listeners. Tokenomics lists the labels. The error.code label is one of none, timeout, upstream_status, decode_failure, unknown_alias, credential_failure, configuration_failure, filtered, breaker_open, low_confidence or no_candidate. Prompt text, credentials, endpoints and the header reason string are never labels.
Routing writes its own audit actions. None of them counts as a guardrail block, so guardrail block counts and dashboards do not include them.
| Action | Target | When |
|---|---|---|
routing.listener_refused | routing.listener | A route-only request whose caller cannot be verified is answered 401. Details are connector=NAME reason=CODE, with reasons such as route_credential_invalid (a wrong or replayed route credential), not_gateway_owner (another account) or an account that is not enrolled. |
routing.sensitive_refused | routing.sensitive | A sensitive request no local model can take. |
routing.admin_denied | routing.admin | Any other administrator-rule or budget refusal. |
For routing.listener_refused, one row is written per connector and reason every 10 s, with repeated=N for the refusals it stands for. The connection-limit answer (429) is not audited, and no credential is recorded.
Next steps
- Semantic model routing: first setup, verify, disable
- Routing rules and budgets
- Decision model
Semantic model routing
Route each model call with a built-in rules classifier, System One or the vLLM Semantic Router, including route-only mode for Claude Code and Codex.
Routing rules and budgets
Limit which models each group may use and cap model spend per user, group or connector, on one device or across the fleet with the opt-in budget store.