Getting Started with Switchyard
Add routing to your existing Rust gateway or agent harness with libsy, Switchyard’s core library. Your application keeps its model clients, credentials, and service.
Start with a local routing decision. Then add tool history or send the selected request through your own model client. Python bindings are covered separately.
Install
You need Rust 1.96.1+ with Cargo and a native build toolchain. See installation requirements. These examples use the Switchyard 0.4.0 Rust API.
Route a request
Replace src/main.rs with this complete example. It selects a model locally, without an API key:
Expected output, after Cargo’s build messages:
This Stage preset yields only Done, with outcome.response set to None. Your application owns the final model call. The loop also shows where other algorithms can ask for intermediate model calls.
0.5 is a signal-confidence threshold, not a measured probability of task success. This preset is Auto in TOML. See the Stage router guide for signal rules, and evaluate your workload before relying on savings.
Try the same prompt after a tool failure
Keep the same program. Insert this block immediately before let mut steps = ..., then run cargo run again:
The critical failure makes Stage select Capable for the same user prompt:
Both runs are local and need no API key. Remove the inserted block to return to the fresh request.
Use libsy with your client
You do not have to use Switchyard’s HTTP client. Keep your existing SDK, or create your own client. The same program can send Stage’s selected request through OpenRouter.
Add the dependencies:
For the complete program, replace src/main.rs with the OpenRouter example. It includes the router above and the adapter below. To extend your existing file instead, apply the edits shown here.
Create an OpenRouter key with access to your chosen models:
In main, replace the two demo model-ID assignments with:
Inside match step?, replace the CallModel and Done arms with these. Keep CallDecision unchanged; it needs a separate decision client.
Append the adapter to the same file:
OpenRouter adapter: append to src/main.rs
This reuses Request from the imports above. It uses reqwest for HTTP and switchyard-translation for provider JSON. It returns a buffered response and requires no client trait implementation.
Run the program again. This makes one billable model call:
For the fresh request, expect this shape; the answer text depends on the model:
With the optional tool-failure history, the selected model is anthropic/claude-opus-4.8. For routing only, keep the original Done arm. Other algorithms may supply an answer in outcome.response; see final outcome handling for how to reuse it.
Your client returns switchyard_protocol::Response and adapts errors to libsy::Result. The optional Switchyard HTTP client provides translation, streaming, retries, and fallback. For concurrent algorithms, poll the Step stream and call futures together; this loop serves calls sequentially.
Next steps
- Understand the exchange: Mechanics of Libsy follows a request through steps, call objects, protocol types, and the final outcome.
- Explore the Rust API: libsy crate and generated API reference.
- Use a gateway integration: LiteLLM or NeMo Relay. Check each guide’s supported algorithms and versions.
- Choose another algorithm: routing overview.
- Tune your configuration: Tuning explains how to define an objective, compare candidate configurations on calibration tasks, and validate the selected one on held-out tasks.
- Try the standalone proxy: local evaluation guide. The server is for demos, evaluation, and personal use, not a supported production path.
- Use Python: Python bindings.
- Use an agent directly: Claude Code example, pi, or Oh My Pi.