Skip to main content

ProviderPreferences

Struct ProviderPreferences 

Source
pub struct ProviderPreferences {
    pub order: Option<Vec<String>>,
    pub only: Option<Vec<String>>,
    pub ignore: Option<Vec<String>>,
    pub allow_fallbacks: Option<bool>,
    pub require_parameters: Option<bool>,
    pub data_collection: Option<DataCollection>,
    pub zdr: Option<bool>,
    pub sort: Option<ProviderSort>,
    pub preferred_min_throughput: Option<ThroughputThreshold>,
    pub preferred_max_latency: Option<LatencyThreshold>,
    pub max_price: Option<MaxPrice>,
    pub quantizations: Option<Vec<Quantization>>,
}
Expand description

Provider preferences for OpenRouter routing.

This struct allows you to control which providers are used and how they are prioritized when making requests through OpenRouter.

See: https://openrouter.ai/docs/guides/routing/provider-selection

§Example

use rig::providers::openrouter::{ProviderPreferences, ProviderSortStrategy, Quantization};

// Create preferences for zero data retention providers, sorted by throughput
let prefs = ProviderPreferences::new()
    .sort(ProviderSortStrategy::Throughput)
    .zdr(true)
    .quantizations([Quantization::Int8])
    .only(["anthropic", "openai"]);

Fields§

§order: Option<Vec<String>>

Try these provider slugs in the given order first. If allow_fallbacks: true, OpenRouter may try other providers after this list is exhausted.

§only: Option<Vec<String>>

Hard allowlist. Only these provider slugs are eligible.

§ignore: Option<Vec<String>>

Blocklist. These provider slugs are never used.

§allow_fallbacks: Option<bool>

If false, the router will not use any providers outside what your constraints permit. Default is true.

§require_parameters: Option<bool>

If true, only route to providers that support all parameters in your request. Default is false.

§data_collection: Option<DataCollection>

Data collection policy. If DataCollection::Deny, restrict routing to providers that do not store user data non-transiently. Default is DataCollection::Allow.

§zdr: Option<bool>

If true, restrict routing to Zero Data Retention endpoints only.

§sort: Option<ProviderSort>

Sorting strategy. Affects ordering, not strict exclusion. If set, default load balancing is disabled.

§preferred_min_throughput: Option<ThroughputThreshold>

Throughput threshold. Endpoints not meeting the threshold are deprioritized.

§preferred_max_latency: Option<LatencyThreshold>

Latency threshold. Endpoints not meeting the threshold are deprioritized.

§max_price: Option<MaxPrice>

Hard price ceiling. If no provider is at or under, the request fails.

§quantizations: Option<Vec<Quantization>>

Restrict routing to providers serving specific quantization levels.

Implementations§

Source§

impl ProviderPreferences

Source

pub fn new() -> Self

Create a new empty provider preferences struct

Source

pub fn order( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Try these provider slugs in the given order first.

If allow_fallbacks is true (default), OpenRouter may try other providers after this list is exhausted.

§Example
use rig::providers::openrouter::ProviderPreferences;

let prefs = ProviderPreferences::new()
    .order(["anthropic", "openai"]);
Source

pub fn only( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Hard allowlist. Only these provider slugs are eligible.

§Example
use rig::providers::openrouter::ProviderPreferences;

let prefs = ProviderPreferences::new()
    .only(["azure", "together"])
    .allow_fallbacks(false);
Source

pub fn ignore( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> Self

Blocklist. These provider slugs are never used.

§Example
use rig::providers::openrouter::ProviderPreferences;

let prefs = ProviderPreferences::new()
    .ignore(["deepinfra"]);
Source

pub fn allow_fallbacks(self, allow: bool) -> Self

Control whether fallbacks are allowed.

If false, the router will not use any providers outside what your constraints permit. Default is true.

Source

pub fn require_parameters(self, require: bool) -> Self

If true, only route to providers that support all parameters in your request.

Default is false, meaning providers may ignore unsupported parameters.

Source

pub fn data_collection(self, policy: DataCollection) -> Self

Set data collection policy.

If Deny, restrict routing to providers that do not store user data non-transiently.

Source

pub fn zdr(self, enable: bool) -> Self

If true, restrict routing to Zero Data Retention endpoints only.

§Example
use rig::providers::openrouter::ProviderPreferences;

let prefs = ProviderPreferences::new()
    .zdr(true);
Source

pub fn sort(self, sort: impl Into<ProviderSort>) -> Self

Set the sorting strategy for providers.

If set, default load balancing is disabled and providers are tried deterministically in the resulting order.

§Example
use rig::providers::openrouter::{ProviderPreferences, ProviderSortStrategy};

let prefs = ProviderPreferences::new()
    .sort(ProviderSortStrategy::Latency);
Source

pub fn preferred_min_throughput(self, threshold: ThroughputThreshold) -> Self

Set preferred minimum throughput threshold.

Endpoints not meeting the threshold are deprioritized (moved later), not excluded.

§Example
use rig::providers::openrouter::{ProviderPreferences, ThroughputThreshold, PercentileThresholds};

// Simple threshold
let prefs = ProviderPreferences::new()
    .preferred_min_throughput(ThroughputThreshold::Simple(50.0));

// Percentile threshold
let prefs = ProviderPreferences::new()
    .preferred_min_throughput(ThroughputThreshold::Percentile(
        PercentileThresholds::new().p90(50.0)
    ));
Source

pub fn preferred_max_latency(self, threshold: LatencyThreshold) -> Self

Set preferred maximum latency threshold.

Endpoints not meeting the threshold are deprioritized, not excluded.

Source

pub fn max_price(self, price: MaxPrice) -> Self

Set maximum price ceiling.

If no eligible provider is at or under the ceiling, the request fails.

Source

pub fn quantizations( self, quantizations: impl IntoIterator<Item = Quantization>, ) -> Self

Restrict routing to providers serving specific quantization levels.

§Example
use rig::providers::openrouter::{ProviderPreferences, Quantization};

let prefs = ProviderPreferences::new()
    .quantizations([Quantization::Int8, Quantization::Fp16]);
Source

pub fn zero_data_retention(self) -> Self

Convenience: Enable Zero Data Retention

Source

pub fn fastest(self) -> Self

Convenience: Sort by throughput (higher tokens/sec first)

Source

pub fn cheapest(self) -> Self

Convenience: Sort by price (cheapest first)

Source

pub fn lowest_latency(self) -> Self

Convenience: Sort by latency (lower latency first)

Source

pub fn to_json(&self) -> Value

Convert to JSON value for use in additional_params

Trait Implementations§

Source§

impl Clone for ProviderPreferences

Source§

fn clone(&self) -> ProviderPreferences

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ProviderPreferences

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ProviderPreferences

Source§

fn default() -> ProviderPreferences

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for ProviderPreferences

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for ProviderPreferences

Source§

fn eq(&self, other: &ProviderPreferences) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for ProviderPreferences

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for ProviderPreferences

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> CloneDebuggableStorage for T

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

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

Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

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> 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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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<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<T> DebuggableStorage for T
where T: Any + Send + Sync + Debug,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> WasmCompatSend for T
where T: Send,

Source§

impl<T> WasmCompatSync for T
where T: Sync,