# nvidia-nemo/switchyard architecture

> Architecture diagram of the GitHub repository nvidia-nemo/switchyard, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/nvidia-nemo/switchyard
- Repository: https://github.com/nvidia-nemo/switchyard

## Overview

#### 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: [`crates/switchyard-server/src/lib.rs`](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/lib.rs) (Proxy HTTP routes), [`crates/libsy/src/core/processor.rs`](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/processor.rs) (Request processor), [`crates/switchyard-server/src/main.rs`](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/main.rs) (Server executable), [`crates/switchyard-translation/src/engine.rs`](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-translation/src/engine.rs) (Provider translation)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_server["Standalone Server"]
  node_server_main["Server executable<br/>Rust binary<br/>[main.rs]"]
  node_server_config["CLI &amp; configuration<br/>server setup<br/>[cli.rs]"]
  node_server_routes["Proxy HTTP routes<br/>HTTP API<br/>[lib.rs]"]
  node_server_sse["SSE responses<br/>stream adapter<br/>[sse.rs]"]
  node_server_metrics["Metrics &amp; route stats<br/>operations<br/>[metrics.rs]"]
end

subgraph group_routing["Routing Core"]
  node_processor["Request processor<br/>routing pipeline<br/>[processor.rs]"]
  node_routing_state["Routing state<br/>shared state<br/>[state.rs]"]
  node_algorithm["Routing algorithm<br/>policy interface<br/>[algorithm.rs]"]
  node_algorithm_library["Composable algorithms<br/>algorithm library<br/>[algorithms.rs]"]
end

subgraph group_protocol["Protocol &amp; Translation"]
  node_protocol_types["Neutral protocol types<br/>shared protocol<br/>[lib.rs]"]
  node_envelope["Request envelope<br/>protocol envelope<br/>[envelope.rs]"]
  node_translation["Provider translation<br/>wire translation<br/>[engine.rs]"]
  node_stream_codecs["Buffered &amp; SSE codecs<br/>stream codecs"]
end

subgraph group_execution["Upstream Execution"]
  node_llm_client["LLM client<br/>upstream executor<br/>[client.rs]"]
  node_backend[("Configured backends<br/>provider endpoints<br/>[backend.rs]")]
end

subgraph group_python["Python Integration"]
  node_python_bindings["Python bindings<br/>PyO3 integration<br/>[lib.rs]"]
  node_python_cli["Python CLI &amp; launchers<br/>developer CLI<br/>[launch_command.py]"]
end

node_external_clients(("Clients and agent CLIs<br/>external consumers"))
node_upstream_models(("Upstream model endpoints<br/>external LLMs"))

node_external_clients -->|"HTTP requests"| node_server_routes
node_server_main -->|"parses"| node_server_config
node_server_main -->|"starts"| node_server_routes
node_server_main -->|"initializes"| node_server_metrics
node_server_routes -->|"translates ingress/egress"| node_translation
node_server_routes -->|"dispatches normalized requests"| node_processor
node_translation -->|"uses"| node_protocol_types
node_translation -->|"encodes streams"| node_stream_codecs
node_processor -->|"processes"| node_envelope
node_envelope -->|"contains"| node_protocol_types
node_processor -->|"maintains"| node_routing_state
node_processor -->|"delegates selection"| node_algorithm
node_algorithm -->|"implemented by"| node_algorithm_library
node_processor -->|"executes selected target"| node_llm_client
node_llm_client -->|"selects and calls"| node_backend
node_backend -->|"requests"| node_upstream_models
node_server_routes -->|"streams responses"| node_server_sse
node_server_routes -->|"records operations"| node_server_metrics
node_python_cli -->|"uses"| node_python_bindings
node_python_bindings -.->|"embeds native proxy"| node_server_routes
node_python_cli -->|"launches"| node_external_clients

click node_server_main "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/main.rs"
click node_server_config "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/cli.rs"
click node_server_routes "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/lib.rs"
click node_server_sse "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/sse.rs"
click node_server_metrics "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/metrics.rs"
click node_processor "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/processor.rs"
click node_routing_state "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/state.rs"
click node_algorithm "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/algorithm.rs"
click node_algorithm_library "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/algorithms.rs"
click node_protocol_types "https://github.com/nvidia-nemo/switchyard/blob/main/crates/protocol/src/lib.rs"
click node_envelope "https://github.com/nvidia-nemo/switchyard/blob/main/crates/protocol/src/envelope.rs"
click node_translation "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-translation/src/engine.rs"
click node_llm_client "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy-llm-client/src/client.rs"
click node_backend "https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy-llm-client/src/backend.rs"
click node_python_bindings "https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-py/src/lib.rs"
click node_python_cli "https://github.com/nvidia-nemo/switchyard/blob/main/switchyard/cli/launch_command.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_server_main,node_server_config,node_server_routes,node_server_sse,node_server_metrics toneBlue
class node_processor,node_routing_state,node_algorithm,node_algorithm_library toneAmber
class node_protocol_types,node_envelope,node_translation,node_stream_codecs toneMint
class node_llm_client,node_backend toneRose
class node_python_bindings,node_python_cli toneIndigo
class node_external_clients,node_upstream_models toneNeutral
```

## Components

### Standalone Server

- [Server executable](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/main.rs): Rust binary · Initializes the proxy lifecycle and HTTP runtime. · `crates/switchyard-server/src/main.rs`
- [CLI & configuration](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/cli.rs): server setup · `crates/switchyard-server/src/cli.rs`
- [Proxy HTTP routes](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/lib.rs): HTTP API · Exposes proxy, health, and statistics endpoints. · `crates/switchyard-server/src/lib.rs`
- [SSE responses](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/sse.rs): stream adapter · `crates/switchyard-server/src/sse.rs`
- [Metrics & route stats](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-server/src/metrics.rs): operations · Captures Prometheus metrics, usage, and routing statistics. · `crates/switchyard-server/src/metrics.rs`

### Routing Core

- [Request processor](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/processor.rs): routing pipeline · Normalizes requests, matches routes, and coordinates decisions. · `crates/libsy/src/core/processor.rs`
- [Routing state](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/state.rs): shared state · `crates/libsy/src/core/state.rs`
- [Routing algorithm](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/core/algorithm.rs): policy interface · Selects targets and can request classifier, judge, or subagent work. · `crates/libsy/src/core/algorithm.rs`
- [Composable algorithms](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy/src/algorithms.rs): algorithm library · Provides passthrough, splitting, classification, and signal-driven stages. · `crates/libsy/src/algorithms.rs`

### Protocol & Translation

- [Neutral protocol types](https://github.com/nvidia-nemo/switchyard/blob/main/crates/protocol/src/lib.rs): shared protocol · Provider-neutral requests, responses, model IDs, metadata, and streams. · `crates/protocol/src/lib.rs`
- [Request envelope](https://github.com/nvidia-nemo/switchyard/blob/main/crates/protocol/src/envelope.rs): protocol envelope · `crates/protocol/src/envelope.rs`
- [Provider translation](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-translation/src/engine.rs): wire translation · Converts OpenAI Chat, OpenAI Responses, and Anthropic Messages at ingress and egress. · `crates/switchyard-translation/src/engine.rs`
- **Buffered & SSE codecs**: stream codecs

### Upstream Execution

- [LLM client](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy-llm-client/src/client.rs): upstream executor · Invokes configured upstream backends and records client observations. · `crates/libsy-llm-client/src/client.rs`
- [Configured backends](https://github.com/nvidia-nemo/switchyard/blob/main/crates/libsy-llm-client/src/backend.rs): provider endpoints · Includes OpenAI-compatible services such as vLLM, NIM, Ollama, and OpenRouter. · `crates/libsy-llm-client/src/backend.rs`

### Python Integration

- [Python bindings](https://github.com/nvidia-nemo/switchyard/blob/main/crates/switchyard-py/src/lib.rs): PyO3 integration · Exposes the Rust server and routing library to Python. · `crates/switchyard-py/src/lib.rs`
- [Python CLI & launchers](https://github.com/nvidia-nemo/switchyard/blob/main/switchyard/cli/launch_command.py): developer CLI · Manages a native proxy and launches supported agent CLIs with monitoring. · `switchyard/cli/launch_command.py`

### Other components

- **Clients and agent CLIs**: external consumers
- **Upstream model endpoints**: external LLMs

## Connections

- 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
