chat-router
A provider router for chat-rs that wraps multiple CompletionProvider instances with automatic fallback on transient errors.
Usage
use ;
let claude = new
.with_model
.build;
let gemini = new
.with_model
.build;
// Tries Claude first, falls back to Gemini on rate-limit / network errors
let router = new
.add_provider
.add_provider
.build;
let mut chat = new
.with_model
.with_max_steps
.build;
let mut messages = default;
messages.push;
let response = chat.complete.await?;
Fallback Behavior
Providers are tried in the order they are added. On a retryable error (RateLimited, Network), the router advances to the next provider. On a non-retryable error (Provider, InvalidResponse, etc.), the error is returned immediately.
Routing Strategy
The RoutingStrategy trait allows custom provider ordering based on the messages. Implement RoutingStrategy::rank() and pass it to RouterBuilder::with_strategy() to control provider selection. See the router-keyword and router-capability examples.
Circuit Breaker
Enable the optional circuit breaker to automatically skip providers that have failed repeatedly:
use ;
let router = new
.add_provider
.add_provider
.circuit_breaker
.build;
When a provider's circuit opens, the router skips it until the recovery timeout elapses, then allows a single probe request. A successful probe closes the circuit; a failed probe resets the timer.
Streaming
Enable the stream feature to use StreamRouterBuilder, which wraps providers implementing ChatProvider:
= { = "0.1.0", = ["router", "claude", "gemini", "stream"] }
Scope
The router supports completions and streaming. Embeddings are not supported — use a single provider directly for those.
Feature Flags
Enable via the root crate:
= { = "0.1.0", = ["router", "claude", "gemini"] }