About

This extension resolves virtual models with NVIDIA NeMo Switchyard routing algorithms. Clients send a virtual model name, such as coding-auto, and the filter decides, for every request, which target model serves it. For example, it can send agent steps that only read tool output to an efficient model and the rest to a capable model.

How It Works

  1. The filter buffers the body of LLM API requests (OpenAI chat completions, OpenAI responses and Anthropic messages).
  2. If the request's model is a configured virtual model, the filter runs its Switchyard algorithm on the request, in process.
  3. The filter rewrites the model field of the body to the selected model and adds x-switchyard-* decision headers.

Filters that route by the model in the body, such as Agent Router's, then route the request to the selected model. On plain Envoy, set model_header and clear_route_cache so that routes can match the selected model on a header.

To let a later filter make the final choice, set rewrite_body to false. The body keeps the virtual model, and the decision is carried only in dynamic metadata, filter state and, when enabled, the decision headers and model_header. A later filter then sees both, and can apply its own policy, such as quotas or fallbacks, before it picks the backend.

Supported Algorithms

All Switchyard algorithms except prefill_router, in Switchyard's own route format. Judges, such as Jev, are called with non-blocking HTTP callouts, either on their own endpoint with their own key (decision_targets, judge_targets) or through the gateway itself (model_calls). vgr, advisor and noop may answer the request themselves, streamed when the client asks.

Sessions

Stateful algorithms keep per-session state in the Envoy process. The session ID comes from the x-switchyard-session-id header, falling back to agent-session-id, and can be scoped by tenant headers. Requests of a session must reach the same Envoy instance to share state.

Observability

  • Request headers x-switchyard-virtual-model, x-switchyard-selected-model and x-switchyard-reason.
  • Dynamic metadata in the io.builtonenvoy.switchyard namespace, for access logs.
  • Filter state under the io.builtonenvoy.switchyard.virtual_model, io.builtonenvoy.switchyard.selected_model and io.builtonenvoy.switchyard.reason keys, which is never sent upstream.
  • The switchyard_decisions counter, by virtual model, selected model and outcome.
  • For judge algorithms, the judge's evidence, the switchyard_judge_calls counter, and the calls and tokens spent on routing.

Using with Agent Router

Agent Router works with this extension unchanged. Agent Router reads the model from the request body, so when this filter runs first and rewrites the body, Agent Router routes the request to the selected model, with that model's credentials, translation, rate limits and costs.

The extension is added to the Gateway with Envoy Gateway's dynamic module support (Envoy Gateway v1.9 or later). Two settings matter:

  • Filter order. The module must run before Agent Router's ext_proc filter, which Agent Router inserts before the first lua, ext_proc or wasm filter of the listener. The EnvoyProxy filterOrder moves the module before them.
  • Gateway-wide policy. Envoy picks the filters for a request from the first route it matches, before the model is known. For Agent Router that is usually the catch-all route-not-found rule, so the EnvoyExtensionPolicy must target the whole Gateway.

The module is loaded from the extension image, mounted as a Kubernetes image volume (enabled by default since Kubernetes 1.35). The image also ships libgcc_s.so.1, which distroless Envoy images lack.

Your AIGatewayRoute needs a rule for each target model, as in any Agent Router setup. A rule for the virtual model is optional: it lists it in /v1/models.

See the examples below to try it in a local kind cluster or to add it to an existing Gateway, and the README for the full setup and telemetry options.

Usage Examples

Try It with Agent Router in kind

Creates a kind cluster, installs Envoy Gateway and Agent Router, deploys two fake model backends and a Gateway with the extension, and checks that coding-auto requests reach the model Switchyard selects. Requires docker, kind, kubectl and helm.

git clone https://github.com/tetratelabs/built-on-envoy.git
cd built-on-envoy
./extensions/switchyard/examples/agent-router/kind.sh

# Then send requests through the gateway (the script leaves the cluster running).
kubectl port-forward -n envoy-gateway-system \
  "$(kubectl get svc -n envoy-gateway-system -l gateway.envoyproxy.io/owning-gateway-name=switchyard -o name)" 8080:80 &
curl -i localhost:8080/v1/chat/completions -H 'content-type: application/json' \
  -d '{"model": "coding-auto", "messages": [{"role": "user", "content": "write a hello world in Go"}]}'

Add It to an Agent Router Gateway

Adds the extension to a Gateway managed by Envoy Gateway and Agent Router. The Gateway must reference this EnvoyProxy in spec.infrastructure.parametersRef, and the AIGatewayRoute must have rules for the gpt-4.1-mini and gpt-5 target models.

kubectl apply -f - <<EOF
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyProxy
metadata:
  name: switchyard
  namespace: default
spec:
  dynamicModules:
    - name: switchyard
      source:
        type: Local
        local:
          path: /var/boe/extensions/switchyard/libswitchyard.so
      doNotClose: true
  # Run the module before Agent Router's ext_proc filter.
  filterOrder:
    - {name: envoy.filters.http.dynamic_modules, before: envoy.filters.http.lua}
    - {name: envoy.filters.http.dynamic_modules, before: envoy.filters.http.ext_proc}
    - {name: envoy.filters.http.dynamic_modules, before: envoy.filters.http.wasm}
  provider:
    type: Kubernetes
    kubernetes:
      envoyDeployment:
        pod:
          volumes:
            - name: switchyard
              image:
                reference: ghcr.io/tetratelabs/built-on-envoy/extension-switchyard:0.1.1
        container:
          volumeMounts:
            - name: switchyard
              mountPath: /var/boe/extensions/switchyard
              readOnly: true
---
apiVersion: gateway.envoyproxy.io/v1alpha1
kind: EnvoyExtensionPolicy
metadata:
  name: switchyard
  namespace: default
spec:
  # Must target the whole Gateway.
  targetRefs:
    - group: gateway.networking.k8s.io
      kind: Gateway
      name: my-gateway
  dynamicModule:
    - name: switchyard
      filterName: switchyard
      config:
        virtual_models:
          - name: coding-auto
            targets:
              efficient: gpt-4.1-mini
              capable: gpt-5
            algorithm:
              type: auto
              capable_target: capable
              efficient_target: efficient
            default_model: gpt-4.1-mini
EOF

Automatic Tier Selection

Route coding-auto requests to an efficient or a capable model

boe run --extension switchyard --config '
  {
    "virtual_models": [
      {
        "name": "coding-auto",
        "targets": {"efficient": "gpt-4.1-mini", "capable": "gpt-5"},
        "algorithm": {"type": "auto", "capable_target": "capable", "efficient_target": "efficient"},
        "default_model": "gpt-4.1-mini"
      }
    ]
  }'

Judge Routing with Jev

Ask Jev whether each new session needs the capable model

boe run --extension switchyard --cluster api.typesafe.ai:443 --config '
  {
    "decision_targets": {
      "jev": {"format": "system_one", "model": "jev-latest", "cluster": "api.typesafe.ai:443",
              "authority": "api.typesafe.ai", "api_key": {"file": "/tmp/jev-api-key"}}
    },
    "virtual_models": [
      {
        "name": "coding-auto",
        "targets": {"efficient": "gpt-4.1-mini", "capable": "gpt-5"},
        "algorithm": {"type": "llm_classifier", "mode": "capability", "classifier_target": "jev",
                      "strong_target": "capable", "weak_target": "efficient", "classify_trigger": "new_session",
                      "decision": {"cutoff": 0.05, "candidates": {"candidate_a": "capable", "candidate_b": "efficient"}, "evidence": {}}},
        "default_model": "gpt-4.1-mini"
      }
    ]
  }'

Weighted Split Routed by Header

Split traffic between two models and expose the selected one in a header for routing

boe run --extension switchyard --config '
  {
    "virtual_models": [
      {
        "name": "chat-split",
        "targets": {"a": "model-a", "b": "model-b"},
        "algorithm": {"type": "random", "targets": ["a", "b"], "weights": [0.9, 0.1]}
      }
    ],
    "model_header": "x-model",
    "clear_route_cache": true
  }'

Decision Only, for a Later Filter

Keep the virtual model in the body and pass the decision to later filters as filter state

boe run --extension switchyard --config '
  {
    "virtual_models": [
      {
        "name": "coding-auto",
        "targets": {"efficient": "gpt-4.1-mini", "capable": "gpt-5"},
        "algorithm": {"type": "auto", "capable_target": "capable", "efficient_target": "efficient"}
      }
    ],
    "rewrite_body": false,
    "decision_headers": false
  }'

Tenant-Scoped Sessions

Use per-tenant session state and a cheaper policy for the free tier

boe run --extension switchyard --config '
  {
    "virtual_models": [
      {
        "name": "coding-auto",
        "match_headers": [{"name": "x-tier", "value": "free"}],
        "targets": {"only": "gpt-4.1-mini"},
        "algorithm": {"type": "passthrough", "target": "only"}
      },
      {
        "name": "coding-auto",
        "targets": {"efficient": "gpt-4.1-mini", "capable": "gpt-5"},
        "algorithm": {"type": "auto", "capable_target": "capable", "efficient_target": "efficient"},
        "default_model": "gpt-4.1-mini"
      }
    ],
    "session": {"scope_headers": ["x-tenant-id"]}
  }'