Skip to main content

MultiProvider

Struct MultiProvider 

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

Intelligent routing provider with fallback support.

MultiProvider implements the Provider trait and provides automatic model selection based on task complexity, with circuit breaker protection and ordered fallback for resilience.

§Type Parameters

  • R: The complexity router type (default: DefaultRouter)
  • F: The fallback chain type (default: FallbackChain)

Implementations§

Source§

impl MultiProvider

Source

pub fn new(config: MultiProviderConfig) -> Self

Create a new MultiProvider with the given configuration.

Uses DefaultRouter for complexity-based routing.

§Example
let config = MultiProviderConfig::default();
let provider = MultiProvider::new(config);
Source

pub fn with_router(router: impl ComplexityRouter + 'static) -> Self

Create a new MultiProvider with a custom router.

Allows using a custom implementation of ComplexityRouter.

§Example
let router = MyCustomRouter::new();
let provider = MultiProvider::with_router(router);
Source

pub fn with_config_and_router( config: MultiProviderConfig, router: impl ComplexityRouter + 'static, ) -> Self

Create a new MultiProvider with custom config and router.

§Example
let config = MultiProviderConfig::default()
    .with_auto_routing(false)
    .with_max_retries(2);
let router = DefaultRouter::new();
let provider = MultiProvider::with_config_and_router(config, router);
Source

pub fn set_router(self, router: impl ComplexityRouter + 'static) -> Self

Replace the router with a new implementation.

§Example
let provider = multi_provider.set_router(new_router);
Source

pub fn with_fallback(self, fallback: FallbackChain) -> Self

Set the fallback chain.

The fallback chain is used when the primary model fails or is unavailable.

§Example
let fallback = FallbackChain::from_ids(&["anthropic/claude-sonnet-4"])?;
let provider = multi_provider.with_fallback(fallback);
Source

pub fn set_fallback(&mut self, fallback: FallbackChain)

Set the fallback chain by reference.

Source

pub fn register_provider(&mut self, name: &str, provider: Arc<dyn Provider>)

Register a provider with this MultiProvider.

The provider can be referenced by name when calling stream(). Each provider gets its own circuit breaker instance.

§Arguments
  • name - Provider identifier (e.g., “openai”, “anthropic”)
  • provider - The provider implementation
§Example
let openai_provider = Arc::new(OpenAiProvider::new());
multi_provider.register_provider("openai", openai_provider);
Source

pub fn unregister_provider(&mut self, name: &str) -> bool

Unregister a provider.

Removes the provider and its associated circuit breaker.

§Arguments
  • name - Provider identifier to remove
§Returns

true if the provider was found and removed.

Source

pub fn get_provider(&self, name: &str) -> Option<&Arc<dyn Provider>>

Get a provider by name.

§Arguments
  • name - Provider identifier
§Returns

Option containing the provider if found.

Source

pub fn get_breaker( &self, provider_name: &str, ) -> Option<Arc<ProviderCircuitBreaker>>

Get the circuit breaker for a provider.

§Arguments
  • provider_name - Provider identifier
§Returns

Arc<ProviderCircuitBreaker> if the provider is registered.

Source

pub fn provider_names(&self) -> Vec<&str>

Get all registered provider names.

Source

pub fn circuit_breaker_diagnostics(&self) -> Vec<CircuitBreakerDiagnostics>

Get diagnostic information for all circuit breakers.

§Returns

Vector of diagnostics for each registered provider.

Source

pub fn router(&self) -> &Arc<dyn ComplexityRouter>

Get the router used for complexity-based routing.

Source

pub fn fallback(&self) -> &FallbackChain

Get a reference to the fallback chain.

Source

pub fn config(&self) -> &MultiProviderConfig

Get a reference to the configuration.

Source

pub fn diagnostics(&self) -> MultiProviderDiagnostics

Get diagnostic summary of the multi-provider state.

Trait Implementations§

Source§

impl Provider for MultiProvider

Source§

fn stream<'life0, 'life1, 'life2, 'async_trait>( &'life0 self, model: &'life1 Model, context: &'life2 Context, options: Option<StreamOptions>, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = ProviderEvent> + Send>>, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait, 'life1: 'async_trait, 'life2: 'async_trait,

Stream assistant message events with intelligent routing.

This method implements the priority order logic:

  1. If auto_routing=true: classify complexity and select router’s best model
  2. Try the incoming model (if registered and circuit breaker allows)
  3. Try fallback chain models in order

For each candidate model:

  • Check circuit breaker (skip if open)
  • Call provider.stream()
  • On retryable error: record failure, retry or move to next
  • On non-retryable error: return immediately
  • On success: record success to breaker, return stream
Source§

fn name(&self) -> &str

Returns “multi-provider” as the provider name.

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> 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> 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
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,