Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.
Rust
feat(libsy): Add passthrough algorithm (#157)
Graham King5ac6ff1247e2
diff --git a/crates/libsy/src/algorithms.rs b/crates/libsy/src/algorithms.rsindex 25f692e25..1b6538625 100644--- a/crates/libsy/src/algorithms.rs+++ b/crates/libsy/src/algorithms.rs@@ -9,11 +9,13 @@pub mod fall_through;pub mod llm_class;pub mod noop;+pub mod passthrough;pub mod rand;pub use fall_through::{FallThrough, FallThroughDecision};pub use llm_class::{ClassifierDecision, ClassifierTier, LlmClassifier};pub use noop::{Noop, NoopDecision};+pub use passthrough::{Passthrough, PassthroughDecision};pub use rand::{Random, RandomDecision};pub use util::{AffinityRouter, SubagentOverride};diff --git a/crates/libsy/src/algorithms/passthrough.rs b/crates/libsy/src/algorithms/passthrough.rsnew file mode 100644index 000000000..96e45ddab--- /dev/null+++ b/crates/libsy/src/algorithms/passthrough.rs@@ -0,0 +1,118 @@+// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.+// SPDX-License-Identifier: Apache-2.0++//! Router that only routes to a single backend, always.+//! - For testing if issues come from backend or from Switchyard+//! - For logging request / responses for RL++use std::sync::Arc;++use switchyard_protocol::{Request, Response};++use crate::{Algorithm, Context, Decision, Driver, LlmTarget, Result};++/// See module comment+pub struct Passthrough {+ target: LlmTarget,+}++impl Passthrough {+ pub fn new(target: LlmTarget) -> Self {+ Passthrough { target }+ }+}++pub struct PassthroughDecision {+ model_id: String,+}++impl Decision for PassthroughDecision {+ fn selected_model(&self) -> &str {+ &self.model_id+ }+ fn reasoning(&self) -> Option<&str> {+ // There is no decision to take, so no reasoning+ None+ }+ fn as_any(&self) -> &dyn std::any::Any {+ self+ }+}++#[async_trait::async_trait]+impl Algorithm for Passthrough {+ fn name(&self) -> &str {+ "passthrough"+ }++ async fn create_run_task(+ self: Arc<Self>,+ ctx: Context,+ driver: Driver,+ request: Request,+ ) -> Result<Response> {+ let decision: Arc<dyn Decision> = Arc::new(PassthroughDecision {+ model_id: self.target.semantic_name.clone(),+ });+ driver.info(ctx.clone(), decision.clone()).await?;+ driver+ .call_llm_target(ctx, &self.target, request, decision)+ .await+ }+}++#[cfg(test)]+mod tests {+ use std::sync::Arc;++ use crate::{+ Algorithm, Context, Decision, LlmResponse, LlmTarget, Request, Response, RoutedLlmClient,+ };+ use switchyard_protocol::{completion_text, text_request, text_response};++ /// Echoes the selected target so tests can inspect which target was called.+ /// TODO: Duplicated from rand.rs+ struct EchoClient;++ #[async_trait::async_trait]+ impl RoutedLlmClient for EchoClient {+ async fn call(+ &self,+ _ctx: Context,+ _request: Request,+ decision: Arc<dyn Decision>,+ ) -> std::result::Result<Response, switchyard_protocol::LlmClientError> {+ Ok(Response {+ llm_response: LlmResponse::Agg(text_response(None, decision.selected_model())),+ metadata: None,+ })+ }+ }++ #[tokio::test]+ async fn test_passthrough() -> crate::error::Result<()> {+ const MODEL_ID: &str = "testing/passthrough";+ let request = Request {+ llm_request: text_request(Some("auto".to_string()), "hi"),+ raw_request: None,+ metadata: None,+ };+ let algorithm = Arc::new(super::Passthrough::new(LlmTarget {+ semantic_name: MODEL_ID.to_string(),+ llm_client: Some(Arc::new(EchoClient)),+ }));+ let (trace, response) = algorithm.run(Context::default(), request).await?;++ assert_eq!(+ response+ .llm_response+ .as_agg()+ .map(completion_text)+ .unwrap_or_default(),+ MODEL_ID+ );+ assert_eq!(trace.len(), 1);+ assert_eq!(trace[0].selected_model(), MODEL_ID);+ Ok(())+ }+}diff --git a/crates/switchyard-server/README.md b/crates/switchyard-server/README.mdindex de5f16f41..611f7b207 100644--- a/crates/switchyard-server/README.md+++ b/crates/switchyard-server/README.md@@ -35,6 +35,11 @@ classifier_target = "model_a"strong_target = "model_a"weak_target = "model_b"threshold = 0.5++[routes.passthrough]+id = "switchyard/passthrough"+type = "passthrough"+target = "model_a"``````bash@@ -47,9 +52,9 @@ upstream, and a route's `id` is the model clients send to select that algorithm.Each target references an entry under `llm_clients`. All configured clients use`TranslatingLlmClient`; supported formats are `openai_chat`, `openai_responses`, and-`anthropic_messages`. Supported algorithms are `noop`, `random`, and `llm_classifier`. An-`api_key_env` value names an environment variable; the TOML never contains the secret itself. If-omitted, the client sends no authentication.+`anthropic_messages`. Supported algorithms are `noop`, `random`, `passthrough`, and+`llm_classifier`. An `api_key_env` value names an environment variable; the TOML+never contains the secret itself. If omitted, the client sends no authentication.Random-route `weights` are relative, follow target order, and do not need to sum to one. Omit themfor equal weighting. The optional `seed` reproduces the selection sequence for the same call order.diff --git a/crates/switchyard-server/src/config.rs b/crates/switchyard-server/src/config.rsindex cd8f62b98..f7a0eba6d 100644--- a/crates/switchyard-server/src/config.rs+++ b/crates/switchyard-server/src/config.rs@@ -8,7 +8,7 @@ use std::fs;use std::path::Path;use std::sync::Arc;-use libsy::algorithms::{LlmClassifier, Noop, Random};+use libsy::algorithms::{LlmClassifier, Noop, Passthrough, Random};use libsy::{Algorithm, LlmTarget, LlmTargetSet, RoutedLlmClient};use serde::Deserialize;use switchyard_llm_client::{Backend, HttpBackendConfig, ModelConfig, TranslatingLlmClient};@@ -171,6 +171,10 @@ enum RouteConfig {weights: Option<Vec<f64>>,seed: Option<u64>,},+ Passthrough {+ id: String,+ target: String,+ },LlmClassifier {id: String,classifier_target: String,@@ -182,8 +186,11 @@ enum RouteConfig {impl RouteConfig {fn id(&self) -> &str {+ use RouteConfig::*;match self {- Self::Noop { id } | Self::Random { id, .. } | Self::LlmClassifier { id, .. } => id,+ Noop { id } | Random { id, .. } | LlmClassifier { id, .. } | Passthrough { id, .. } => {+ id+ }}}}@@ -248,6 +255,10 @@ fn build_algorithm(.map_err(|error| ServerError::new(format!("random route {route_name}: {error}")))?;Ok(Arc::new(algorithm))}+ RouteConfig::Passthrough { target, .. } => {+ let target = resolve_target(route_name, target, targets)?;+ Ok(Arc::new(Passthrough::new(target)))+ }RouteConfig::LlmClassifier {classifier_target,strong_target,@@ -355,6 +366,11 @@ classifier_target = "classifier"strong_target = "strong"weak_target = "weak"threshold = 0.5++[routes.passthrough]+id = "switchyard/passthrough"+type = "passthrough"+target = "weak""#;fn error_message(toml: &str) -> String {@@ -367,12 +383,14 @@ threshold = 0.5#[test]fn builds_all_supported_algorithm_types() -> ServerResult<()> {let state = server_state_from_toml(VALID_CONFIG)?;+ // The model id array is sorted alphabeticallyassert_eq!(state.models().collect::<Vec<_>>(),["switchyard/classifier","switchyard/noop",- "switchyard/random"+ "switchyard/passthrough",+ "switchyard/random",]);Ok(())diff --git a/crates/switchyard-server/tests/server.rs b/crates/switchyard-server/tests/server.rsindex 91334dd24..f3fe06667 100644--- a/crates/switchyard-server/tests/server.rs+++ b/crates/switchyard-server/tests/server.rs@@ -239,6 +239,11 @@ classifier_target = "classifier"strong_target = "strong"weak_target = "weak"threshold = 0.5++[routes.passthrough]+id = "switchyard/passthrough"+type = "passthrough"+target = "weak""#,base_url = upstream.base_url))?;@@ -247,6 +252,7 @@ threshold = 0.5for (route, selected) in [("switchyard/random", "model/weak"),("switchyard/classifier", "model/strong"),+ ("switchyard/passthrough", "model/weak"),] {let response = send(&app,@@ -269,10 +275,11 @@ threshold = 0.5}let calls = upstream.calls.lock().await;- assert_eq!(calls.len(), 3);+ assert_eq!(calls.len(), 4);assert_eq!(calls[0]["model"], "model/weak");assert_eq!(calls[1]["model"], "model/classifier");assert_eq!(calls[2]["model"], "model/strong");+ assert_eq!(calls[3]["model"], "model/weak");Ok(())}