How nvidia-nemo/switchyard works
Purpose and entry points
Switchyard is a Rust LLM traffic proxy and embeddable routing library. It accepts OpenAI Chat, OpenAI Responses, and Anthropic Messages requests; selects a configured backend; translates formats; proxies streaming or buffered responses; and emits operational metrics. Main executable entry point: crates/switchyard-server/src/main.rs. Python users enter through switchyard/cli/switchyard_cli.py; Rust embedders use crates/libsy/src/lib.rs.
Server composition and lifecycle
The standalone server parses CLI/configuration, initializes routes, metrics, observability, and shutdown handling, then exposes proxy/health/statistics endpoints. Its runtime boundary is the HTTP server versus configured upstream model endpoints.
• crates/switchyard-server/src/main.rs
• crates/switchyard-server/src/cli.rs
• crates/switchyard-server/src/config.rs
• crates/switchyard-server/src/lib.rs
Request routing pipeline
A request is normalized into shared protocol types, matched by configured model/route, and processed by a routing algorithm that chooses a target or requests subagent/judge work. The selected target is invoked, with fallback/control decisions remaining in the routing layer rather than the translation layer.
• crates/libsy/src/core/processor.rs
• crates/libsy/src/core/algorithm.rs
• crates/libsy/src/core/state.rs
• crates/protocol/src/envelope.rs
Routing algorithms
libsy provides composable algorithms: passthrough, random traffic splitting, LLM classification, and signal-driven stages; escalation is built around classifier/judge utilities. Algorithms deliberately do not own HTTP or model invocation: callers provide model-call execution, enabling use in the server or an external gateway/runtime.
• crates/libsy/src/algorithms.rs
• crates/libsy/src/algorithms/llm_class.rs
• crates/libsy/src/algorithms/stage.rs
• crates/libsy/src/algorithms/util/escalation.rs
Protocol and translation boundary
The protocol crate defines provider-neutral request, response, model-ID, metadata, and stream representations. The translation crate converts at ingress/egress between Anthropic Messages, OpenAI Chat, and OpenAI Responses, including buffered and SSE stream codecs. This isolates provider wire compatibility from routing policy.
• crates/protocol/src/lib.rs
• crates/protocol/src/stream.rs
• crates/switchyard-translation/src/engine.rs
• crates/switchyard-translation/src/codecs/
Upstream LLM execution
switchyard-llm-client owns calls to configured upstream providers/endpoints, backend selection details, errors, observations, and client-level metrics. It is the optional execution counterpart to libsy; upstreams can include OpenAI-compatible services such as vLLM, NVIDIA NIM, Ollama, and OpenRouter.
• crates/libsy-llm-client/src/client.rs
• crates/libsy-llm-client/src/backend.rs
• crates/libsy-llm-client/src/run.rs
• crates/libsy-llm-client/src/observability.rs
Streaming, observability, and operations
Server responses support SSE, while routing logs, Prometheus-oriented metrics, usage metrics, and route statistics capture request/error/latency/token/routing behavior. Docker packaging supports deployment; benchmark assets exercise proxy behavior and model-routing experiments.
• crates/switchyard-server/src/sse.rs
• crates/switchyard-server/src/metrics.rs
• crates/switchyard-server/src/stats.rs
• Dockerfile
Python distribution and agent launchers
PyO3-style bindings expose the Rust server and libsy to Python. The Python CLI starts/manages a native proxy and launches Claude Code, Codex CLI, or OpenClaw against it, with live stats and health monitoring. This is an integration layer, not the proxy’s core runtime.
• crates/switchyard-py/src/lib.rs
• crates/switchyard-py/src/server_bindings.rs
• switchyard/cli/launch_command.py
• switchyard/cli/launchers/native_server.py
Main entry points: lib.rs Proxy HTTP routes · processor.rs Request processor · main.rs Server executable · engine.rs Provider translation
Components 19
Standalone Server
- Server executable · Initializes the proxy lifecycle and HTTP runtime.
crates/switchyard-server/src/main.rs - CLI & configuration
crates/switchyard-server/src/cli.rs - Proxy HTTP routes · Exposes proxy, health, and statistics endpoints.
crates/switchyard-server/src/lib.rs - SSE responses
crates/switchyard-server/src/sse.rs - Metrics & route stats · Captures Prometheus metrics, usage, and routing statistics.
crates/switchyard-server/src/metrics.rs
Routing Core
- Request processor · Normalizes requests, matches routes, and coordinates decisions.
crates/libsy/src/core/processor.rs - Routing state
crates/libsy/src/core/state.rs - Routing algorithm · Selects targets and can request classifier, judge, or subagent work.
crates/libsy/src/core/algorithm.rs - Composable algorithms · Provides passthrough, splitting, classification, and signal-driven stages.
crates/libsy/src/algorithms.rs
Protocol & Translation
- Neutral protocol types · Provider-neutral requests, responses, model IDs, metadata, and streams.
crates/protocol/src/lib.rs - Request envelope
crates/protocol/src/envelope.rs - Provider translation · Converts OpenAI Chat, OpenAI Responses, and Anthropic Messages at ingress and egress.
crates/switchyard-translation/src/engine.rs - Buffered & SSE codecs
Upstream Execution
- LLM client · Invokes configured upstream backends and records client observations.
crates/libsy-llm-client/src/client.rs - Configured backends · Includes OpenAI-compatible services such as vLLM, NIM, Ollama, and OpenRouter.
crates/libsy-llm-client/src/backend.rs
Python Integration
- Python bindings · Exposes the Rust server and routing library to Python.
crates/switchyard-py/src/lib.rs - Python CLI & launchers · Manages a native proxy and launches supported agent CLIs with monitoring.
switchyard/cli/launch_command.py
Other components
- Clients and agent CLIs
- Upstream model endpoints
Connections 21
- Clients and agent CLIs → Proxy HTTP routes: HTTP requests
- Server executable → CLI & configuration: parses
- Server executable → Proxy HTTP routes: starts
- Server executable → Metrics & route stats: initializes
- Proxy HTTP routes → Provider translation: translates ingress/egress
- Proxy HTTP routes → Request processor: dispatches normalized requests
- Provider translation → Neutral protocol types: uses
- Provider translation → Buffered & SSE codecs: encodes streams
- Request processor → Request envelope: processes
- Request envelope → Neutral protocol types: contains
- Request processor → Routing state: maintains
- Request processor → Routing algorithm: delegates selection
- Routing algorithm → Composable algorithms: implemented by
- Request processor → LLM client: executes selected target
- LLM client → Configured backends: selects and calls
- Configured backends → Upstream model endpoints: requests
- Proxy HTTP routes → SSE responses: streams responses
- Proxy HTTP routes → Metrics & route stats: records operations
- Python CLI & launchers → Python bindings: uses
- Python bindings → Proxy HTTP routes: embeds native proxy
- Python CLI & launchers → Clients and agent CLIs: launches