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.

~/.defenseclaw/config.yaml
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_KEY

Enable the router and restart the gateway

defenseclaw setup routing --enable
defenseclaw-gateway restart

To 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_sr reads the newest message only, so it does not accept routing.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.

~/.defenseclaw/config.yaml
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 called

What it reads:

  • Read: user messages, and with include_tool_names the 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_bytes and max_bytes bound 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_names signals also see a line such as tools: Bash,Read, and the request condition tool_loop matches when the conversation called a tool.
  • routing.sensitive.signals match the newest message only.
  • systemone gets the same context, at most 8 KiB.
  • When a limit cuts the context, the decision carries the warning context_truncated:bytes (items past 32 tool names), shown by defenseclaw 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.
CaseWhat 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_liteNot 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 listenerRefused 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_aliases are local models only leaks nothing in any mode. Use that for regulated groups.
  • On a managed host the administrator sets and locks routing.sensitive in 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.

ActionTargetWhen
routing.listener_refusedrouting.listenerA 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_refusedrouting.sensitiveA sensitive request no local model can take.
routing.admin_deniedrouting.adminAny 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