pub struct RouterLLM { /* private fields */ }Expand description
A chat model pool with routing and fallback.
See the module docs for design rationale.
Implementations§
Source§impl RouterLLM
impl RouterLLM
Sourcepub fn new(strategy: RoutingStrategy) -> Self
pub fn new(strategy: RoutingStrategy) -> Self
Create an empty router with the given strategy. Add models via the
with_* builder methods.
Sourcepub fn with_budget(self, budget: Arc<RouterBudget>) -> Self
pub fn with_budget(self, budget: Arc<RouterBudget>) -> Self
Attaches a shared RouterBudget. Every subsequent call projects its
prompt spend against the remaining budget, skips paid slots once the
breaker is tripped (free slots stay reachable as fallback), and
records the model-reported token usage after a successful call.
Sourcepub fn with_registry(self, registry: Arc<ModelRegistry>) -> Self
pub fn with_registry(self, registry: Arc<ModelRegistry>) -> Self
Attaches a model registry. Keyed slots (RouterLLM::with_model_as)
then take their RoutingStrategy::LowestCost weight from the
registry’s blended per-1K price. An explicit cost from
RouterLLM::with_cost always wins; a key the registry cannot resolve
sorts last (treated as infinitely expensive).
Sourcepub fn with_name(self, name: impl Into<String>) -> Self
pub fn with_name(self, name: impl Into<String>) -> Self
Set a human-readable name returned by model_name.
Sourcepub fn with_model<M>(self, model: M) -> Self
pub fn with_model<M>(self, model: M) -> Self
Register a model.
Sourcepub fn with_cost<M>(self, model: M, cost: f64) -> Self
pub fn with_cost<M>(self, model: M, cost: f64) -> Self
Register a model with a relative cost used by RoutingStrategy::LowestCost.
Sourcepub fn with_model_as<M, K>(self, model: M, registry_key: K) -> Self
pub fn with_model_as<M, K>(self, model: M, registry_key: K) -> Self
Register a model keyed as "<provider>/<model-id>" in the attached
ModelRegistry (see RouterLLM::with_registry). Under
RoutingStrategy::LowestCost the slot is weighted by the registry’s
blended per-1K price; other strategies ignore the key.
Sourcepub fn with_priced_model<M>(self, model: M, price: ModelPrice) -> Self
pub fn with_priced_model<M>(self, model: M, price: ModelPrice) -> Self
Register a model with an explicit per-token ModelPrice. The price
both participates in RoutingStrategy::LowestCost (blended) and
prices projected/actual spend for an attached RouterBudget.
Sourcepub fn with_model_rate_limited<M>(self, model: M, limit: ModelRateLimit) -> Self
pub fn with_model_rate_limited<M>(self, model: M, limit: ModelRateLimit) -> Self
Register a model behind a per-model ModelRateLimit (B13). Saturated
slots queue callers FIFO; when the queue is full or the configured
wait timeout elapses, the call skips to the next candidate model.
Sourcepub fn with_last_price(self, price: ModelPrice) -> Self
pub fn with_last_price(self, price: ModelPrice) -> Self
Attaches a ModelPrice to the most recently registered slot.
Use to combine registration styles, e.g. a keyed slot whose registry entry lacks pricing. A no-op with a warning when no slot exists.
Sourcepub fn with_last_rate_limit(self, limit: ModelRateLimit) -> Self
pub fn with_last_rate_limit(self, limit: ModelRateLimit) -> Self
Attaches a ModelRateLimit to the most recently registered slot.
A no-op with a warning when no slot exists.
Sourcepub fn with_fallbacks<M>(primary: M, fallbacks: Vec<M>) -> Self
pub fn with_fallbacks<M>(primary: M, fallbacks: Vec<M>) -> Self
Convenience constructor for primary-first fallback over models of the same type. The primary is tried first; each fallback is tried in order until one succeeds.
Trait Implementations§
Source§impl BaseChatModel for RouterLLM
impl BaseChatModel for RouterLLM
Source§fn chat<'life0, 'async_trait>(
&'life0 self,
messages: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn chat<'life0, 'async_trait>(
&'life0 self,
messages: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn stream_chat<'life0, 'async_trait>(
&'life0 self,
messages: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream_chat<'life0, 'async_trait>(
&'life0 self,
messages: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<StreamChunk, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn chat_with_system<'life0, 'async_trait>(
&'life0 self,
system: String,
messages: Vec<Message>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
fn chat_with_system<'life0, 'async_trait>(
&'life0 self,
system: String,
messages: Vec<Message>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: Sync + 'async_trait,
'life0: 'async_trait,
Source§fn bind_tools(
&self,
_tools: Vec<ToolDefinition>,
) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>
fn bind_tools( &self, _tools: Vec<ToolDefinition>, ) -> Option<Box<dyn BaseChatModel<Error = Self::Error> + Send + Sync>>
Source§impl BaseLanguageModel<Vec<Message>, LLMResult> for RouterLLM
impl BaseLanguageModel<Vec<Message>, LLMResult> for RouterLLM
Source§fn model_name(&self) -> &str
fn model_name(&self) -> &str
Source§fn with_temperature(self, _temp: f32) -> Self
fn with_temperature(self, _temp: f32) -> Self
Source§fn with_max_tokens(self, _max: usize) -> Self
fn with_max_tokens(self, _max: usize) -> Self
Source§fn temperature(&self) -> Option<f32>
fn temperature(&self) -> Option<f32>
Source§fn max_tokens(&self) -> Option<usize>
fn max_tokens(&self) -> Option<usize>
Source§impl Runnable<Vec<Message>, LLMResult> for RouterLLM
impl Runnable<Vec<Message>, LLMResult> for RouterLLM
Source§type Error = RouterError
type Error = RouterError
Source§fn invoke<'life0, 'async_trait>(
&'life0 self,
input: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn invoke<'life0, 'async_trait>(
&'life0 self,
input: Vec<Message>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<LLMResult, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn batch<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<Output>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn batch_as_completed<'life0, 'async_trait>(
&'life0 self,
inputs: Vec<Input>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Vec<(usize, Output)>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn stream<'life0, 'async_trait>(
&'life0 self,
input: Input,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn transform<'life0, 'async_trait>(
&'life0 self,
input: Pin<Box<dyn Stream<Item = Result<Input, Self::Error>> + Send>>,
config: Option<RunnableConfig>,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<Output, Self::Error>> + Send + '_>>, Self::Error>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Auto Trait Implementations§
impl !Freeze for RouterLLM
impl !RefUnwindSafe for RouterLLM
impl !UnwindSafe for RouterLLM
impl Send for RouterLLM
impl Sync for RouterLLM
impl Unpin for RouterLLM
impl UnsafeUnpin for RouterLLM
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<M> StreamingStructuredOutputExt for Mwhere
M: BaseChatModel,
impl<M> StreamingStructuredOutputExt for Mwhere
M: BaseChatModel,
Source§fn stream_structured_output<'life0, 'life1, 'async_trait, T>(
&'life0 self,
schema: Value,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<T, StructuredOutputError>> + Send>>, StructuredOutputError>> + Send + 'async_trait>>
fn stream_structured_output<'life0, 'life1, 'async_trait, T>( &'life0 self, schema: Value, prompt: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<Pin<Box<dyn Stream<Item = Result<T, StructuredOutputError>> + Send>>, StructuredOutputError>> + Send + 'async_trait>>
Source§impl<M> StructuredOutputExt for Mwhere
M: BaseChatModel,
impl<M> StructuredOutputExt for Mwhere
M: BaseChatModel,
Source§fn with_structured_output<'life0, 'life1, 'async_trait, T>(
&'life0 self,
schema: Value,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<T, StructuredOutputError>> + Send + 'async_trait>>where
T: 'async_trait + DeserializeOwned + Serialize + Send + Sync + 'static,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
fn with_structured_output<'life0, 'life1, 'async_trait, T>(
&'life0 self,
schema: Value,
prompt: &'life1 str,
) -> Pin<Box<dyn Future<Output = Result<T, StructuredOutputError>> + Send + 'async_trait>>where
T: 'async_trait + DeserializeOwned + Serialize + Send + Sync + 'static,
Self: Sync + 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
T. Read more