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
impl ProviderPreferences
Sourcepub fn new() -> ProviderPreferences
pub fn new() -> ProviderPreferences
Create a new empty provider preferences struct
Sourcepub fn order(
self,
providers: impl IntoIterator<Item = impl Into<String>>,
) -> ProviderPreferences
pub fn order( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> ProviderPreferences
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"]);Sourcepub fn only(
self,
providers: impl IntoIterator<Item = impl Into<String>>,
) -> ProviderPreferences
pub fn only( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> ProviderPreferences
Hard allowlist. Only these provider slugs are eligible.
§Example
use rig::providers::openrouter::ProviderPreferences;
let prefs = ProviderPreferences::new()
.only(["azure", "together"])
.allow_fallbacks(false);Sourcepub fn ignore(
self,
providers: impl IntoIterator<Item = impl Into<String>>,
) -> ProviderPreferences
pub fn ignore( self, providers: impl IntoIterator<Item = impl Into<String>>, ) -> ProviderPreferences
Blocklist. These provider slugs are never used.
§Example
use rig::providers::openrouter::ProviderPreferences;
let prefs = ProviderPreferences::new()
.ignore(["deepinfra"]);Sourcepub fn allow_fallbacks(self, allow: bool) -> ProviderPreferences
pub fn allow_fallbacks(self, allow: bool) -> ProviderPreferences
Control whether fallbacks are allowed.
If false, the router will not use any providers outside what your constraints permit.
Default is true.
Sourcepub fn require_parameters(self, require: bool) -> ProviderPreferences
pub fn require_parameters(self, require: bool) -> ProviderPreferences
If true, only route to providers that support all parameters in your request.
Default is false, meaning providers may ignore unsupported parameters.
Sourcepub fn data_collection(self, policy: DataCollection) -> ProviderPreferences
pub fn data_collection(self, policy: DataCollection) -> ProviderPreferences
Set data collection policy.
If Deny, restrict routing to providers that do not store user data non-transiently.
Sourcepub fn zdr(self, enable: bool) -> ProviderPreferences
pub fn zdr(self, enable: bool) -> ProviderPreferences
If true, restrict routing to Zero Data Retention endpoints only.
§Example
use rig::providers::openrouter::ProviderPreferences;
let prefs = ProviderPreferences::new()
.zdr(true);Sourcepub fn sort(self, sort: impl Into<ProviderSort>) -> ProviderPreferences
pub fn sort(self, sort: impl Into<ProviderSort>) -> ProviderPreferences
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);Sourcepub fn preferred_min_throughput(
self,
threshold: ThroughputThreshold,
) -> ProviderPreferences
pub fn preferred_min_throughput( self, threshold: ThroughputThreshold, ) -> ProviderPreferences
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)
));Sourcepub fn preferred_max_latency(
self,
threshold: LatencyThreshold,
) -> ProviderPreferences
pub fn preferred_max_latency( self, threshold: LatencyThreshold, ) -> ProviderPreferences
Set preferred maximum latency threshold.
Endpoints not meeting the threshold are deprioritized, not excluded.
Sourcepub fn max_price(self, price: MaxPrice) -> ProviderPreferences
pub fn max_price(self, price: MaxPrice) -> ProviderPreferences
Set maximum price ceiling.
If no eligible provider is at or under the ceiling, the request fails.
Sourcepub fn quantizations(
self,
quantizations: impl IntoIterator<Item = Quantization>,
) -> ProviderPreferences
pub fn quantizations( self, quantizations: impl IntoIterator<Item = Quantization>, ) -> ProviderPreferences
Restrict routing to providers serving specific quantization levels.
§Example
use rig::providers::openrouter::{ProviderPreferences, Quantization};
let prefs = ProviderPreferences::new()
.quantizations([Quantization::Int8, Quantization::Fp16]);Sourcepub fn zero_data_retention(self) -> ProviderPreferences
pub fn zero_data_retention(self) -> ProviderPreferences
Convenience: Enable Zero Data Retention
Sourcepub fn fastest(self) -> ProviderPreferences
pub fn fastest(self) -> ProviderPreferences
Convenience: Sort by throughput (higher tokens/sec first)
Sourcepub fn cheapest(self) -> ProviderPreferences
pub fn cheapest(self) -> ProviderPreferences
Convenience: Sort by price (cheapest first)
Sourcepub fn lowest_latency(self) -> ProviderPreferences
pub fn lowest_latency(self) -> ProviderPreferences
Convenience: Sort by latency (lower latency first)
Trait Implementations§
Source§impl Clone for ProviderPreferences
impl Clone for ProviderPreferences
Source§fn clone(&self) -> ProviderPreferences
fn clone(&self) -> ProviderPreferences
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more