pub struct ModelRouter(/* private fields */);Expand description
Routes each invocation among a fixed set of candidate models (ModelRouter).
Pass it as the agent’s model: the strategy picks the opening candidate before the first model
call (in model input middleware, so BeforeModelCallEvent reports the default model and
AfterModelCallEvent the routed one), and after each failure no retry strategy claimed it
picks the next. The attempt count restarts at 1 when the candidate changes. agent.model()
stays the first candidate’s model, which token estimation and conversation management use.
The API is provisional.
use strands::Agent;
use strands::models::routing::{ModelRouter, ModelRouterOptions};
use strands::testing::MockModel;
let router = ModelRouter::new(
[MockModel::new().with_turn("primary"), MockModel::new().with_turn("fallback")],
ModelRouterOptions::default(),
)?;
let agent = Agent::builder().model(router).printer(false).build()?;Implementations§
Source§impl ModelRouter
impl ModelRouter
Sourcepub fn new(
models: impl IntoIterator<Item = impl Into<RoutingCandidate>>,
options: ModelRouterOptions,
) -> Result<Self>
pub fn new( models: impl IntoIterator<Item = impl Into<RoutingCandidate>>, options: ModelRouterOptions, ) -> Result<Self>
A router over models: models, nested routers or RoutingCandidates, in declaration
order.
§Errors
No candidates (ModelRouter requires at least one candidate model), a stateful model, a
repeated candidate, model or name.
Sourcepub fn candidates(&self) -> &[RoutingCandidate]
pub fn candidates(&self) -> &[RoutingCandidate]
The candidates, in declaration order.
Sourcepub fn default_model(&self) -> Arc<dyn Model> ⓘ
pub fn default_model(&self) -> Arc<dyn Model> ⓘ
The first candidate’s concrete model, found without asking a strategy.
Trait Implementations§
Source§impl Clone for ModelRouter
impl Clone for ModelRouter
Source§impl Debug for ModelRouter
impl Debug for ModelRouter
Source§impl From<ModelRouter> for ModelSource
impl From<ModelRouter> for ModelSource
Source§fn from(router: ModelRouter) -> Self
fn from(router: ModelRouter) -> Self
Source§impl From<ModelRouter> for CandidateModel
impl From<ModelRouter> for CandidateModel
Source§fn from(router: ModelRouter) -> Self
fn from(router: ModelRouter) -> Self
Source§impl From<ModelRouter> for RoutingCandidate
impl From<ModelRouter> for RoutingCandidate
Source§fn from(router: ModelRouter) -> Self
fn from(router: ModelRouter) -> Self
Auto Trait Implementations§
impl !RefUnwindSafe for ModelRouter
impl !UnwindSafe for ModelRouter
impl Freeze for ModelRouter
impl Send for ModelRouter
impl Sync for ModelRouter
impl Unpin for ModelRouter
impl UnsafeUnpin for ModelRouter
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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