Skip to main content

RouterBudget

Struct RouterBudget 

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

A circuit breaker over cumulative spend and/or token usage.

Construct with one of the constructors, wrap in an Arc, and attach via super::RouterLLM::with_budget. Cloning is not supported; share by Arc so every call of one run observes the same totals.

Implementations§

Source§

impl RouterBudget

Source

pub fn with_cost_limit(max_cost_usd: f64) -> Self

Only a USD spend cap.

Source

pub fn with_token_limit(max_tokens: u64) -> Self

Only a cumulative token cap.

Source

pub fn with_cost_and_token_limits( max_cost_usd: f64, max_tokens: u64, ) -> Arc<Self>

Both a USD cap and a token cap; either one tripping skips the slot.

Source

pub fn max_cost_usd(&self) -> Option<f64>

Configured USD cap, if any.

Source

pub fn max_tokens(&self) -> Option<u64>

Configured token cap, if any.

Source

pub fn spent_usd(&self) -> f64

Cumulative recorded USD spend.

Source

pub fn tokens(&self) -> u64

Cumulative recorded token usage.

Source

pub fn trips(&self) -> usize

Number of times a precheck or a post-call record crossed a cap.

Source

pub fn is_tripped(&self) -> bool

Whether either cap is already crossed.

Source

pub fn precheck( &self, projected_cost_usd: f64, projected_tokens: u64, ) -> Result<(), BudgetExceeded>

Projects one pending call and returns the dimension that would be exceeded, if any.

projected_cost_usd is the prompt priced against the slot’s price (0.0 for free/unpriced slots); projected_tokens is the prompt-token estimate. A projected zero-cost call always passes the cost dimension even while the breaker is tripped — that is what lets free fallbacks keep serving. The token dimension is price-agnostic.

Source

pub fn record(&self, cost_usd: f64, total_tokens: u64) -> Option<BudgetExceeded>

Records the measured cost/tokens of one finished call.

Returns the first dimension this record pushed over its cap (the breaker latches — later prechecks keep skipping paid candidates).

Source

pub fn reset(&self)

Zeroes accumulated spend/tokens/trip count (start a new run reusing the same budget configuration).

Trait Implementations§

Source§

impl Debug for RouterBudget

Source§

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

Formats the value using the given formatter. Read more

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: Sized + 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: Sized + 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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