Skip to main content

RouterProvider

Struct RouterProvider 

Source
pub struct RouterProvider { /* private fields */ }
Expand description

Multi-model router — routes requests to different provider+model combos based on a task hint encoded in the model parameter.

The model parameter can be:

  • A regular model name (e.g. “anthropic/claude-sonnet-4”) → uses default provider
  • A hint-prefixed string (e.g. “hint:reasoning”) → resolves via route table

This wraps multiple pre-created providers and selects the right one per request.

Implementations§

Source§

impl RouterProvider

Source

pub fn new( providers: Vec<(String, Box<dyn Provider>)>, routes: Vec<(String, Route)>, default_model: String, ) -> Self

Create a new router with a default provider and optional routes.

providers is a list of (name, provider) pairs. The first one is the default. routes maps hint names to Route structs containing provider_name and model.

Source

pub fn resolve_cost_optimized( &self, model: &str, prices: &HashMap<String, ModelPricing>, required_vision: bool, required_tools: bool, ) -> (usize, String)

Resolve a model parameter to the cheapest qualifying route based on pricing.

If the model starts with "hint:cost-optimized" or "hint:cheapest", this method scores each route by input_price + output_price (a simple proxy for total cost), optionally filtering by capability requirements, and returns the cheapest qualifying route.

Falls back to the default route when no pricing data matches.

Trait Implementations§

Source§

impl Provider for RouterProvider

Source§

fn chat_with_system<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, system_prompt: Option<&'life1 str>, message: &'life2 str, model: &'life3 str, temperature: f64, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

One-shot chat with optional system prompt. Read more
Source§

fn chat_with_history<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, messages: &'life1 [ChatMessage], model: &'life2 str, temperature: f64, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Multi-turn conversation. Default implementation extracts the last user message and delegates to chat_with_system.
Source§

fn chat<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, request: ChatRequest<'life1>, model: &'life2 str, temperature: f64, ) -> Pin<Box<dyn Future<Output = Result<ChatResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Structured chat API for agent loop callers.
Source§

fn chat_with_tools<'life0, 'life1, 'life2, 'life3, 'async_trait>( &'life0 self, messages: &'life1 [ChatMessage], tools: &'life2 [Value], model: &'life3 str, temperature: f64, ) -> Pin<Box<dyn Future<Output = Result<ChatResponse>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait, 'life3: 'async_trait,

Chat with tool definitions for native function calling support. The default implementation falls back to chat_with_history and returns an empty tool_calls vector (prompt-based tool use only).
Source§

fn supports_native_tools(&self) -> bool

Whether provider supports native tool calls over API.
Source§

fn supports_streaming(&self) -> bool

Whether provider supports streaming responses. Default implementation returns false.
Source§

fn supports_streaming_tool_events(&self) -> bool

Whether provider can emit structured tool-call stream events. Read more
Source§

fn stream_chat_with_history( &self, messages: &[ChatMessage], model: &str, temperature: f64, options: StreamOptions, ) -> BoxStream<'static, StreamResult<StreamChunk>>

Streaming chat with history. Default implementation extracts the last user message and delegates to stream_chat_with_system, mirroring the non-streaming chat_with_history.
Source§

fn stream_chat( &self, request: ChatRequest<'_>, model: &str, temperature: f64, options: StreamOptions, ) -> BoxStream<'static, StreamResult<StreamEvent>>

Structured streaming chat interface. Read more
Source§

fn supports_vision(&self) -> bool

Whether provider supports multimodal vision input.
Source§

fn warmup<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Warm up the HTTP connection pool (TLS handshake, DNS, HTTP/2 setup). Default implementation is a no-op; providers with HTTP clients should override.
Source§

fn capabilities(&self) -> ProviderCapabilities

Query provider capabilities. Read more
Source§

fn convert_tools(&self, tools: &[ToolSpec]) -> ToolsPayload

Convert tool specifications to provider-native format. Read more
Source§

fn simple_chat<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, message: &'life1 str, model: &'life2 str, temperature: f64, ) -> Pin<Box<dyn Future<Output = Result<String>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Simple one-shot chat (single user message, no explicit system prompt). Read more
Source§

fn stream_chat_with_system( &self, _system_prompt: Option<&str>, _message: &str, _model: &str, _temperature: f64, _options: StreamOptions, ) -> BoxStream<'static, StreamResult<StreamChunk>>

Streaming chat with optional system prompt. Returns an async stream of text chunks. Default implementation falls back to non-streaming chat.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Sync + Send>

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more