pub struct TokenBudgetGuard { /* private fields */ }Expand description
Guards LLM requests against token over-spend.
TokenBudgetGuard is Clone + Send + Sync. All clones share the same
budget counters via an Arc.
Implementations§
Source§impl TokenBudgetGuard
impl TokenBudgetGuard
Sourcepub fn new(config: TokenBudgetConfig) -> Self
pub fn new(config: TokenBudgetConfig) -> Self
Create a new guard with the given configuration.
Sourcepub fn check(&self, text: &str) -> Result<u64, TokenBudgetError>
pub fn check(&self, text: &str) -> Result<u64, TokenBudgetError>
Check whether text fits within the budget limits.
If the check passes, the estimated token count is reserved against
the period budget. Call release with the actual
token count from the provider response to correct the reservation.
§Returns
Ok(estimated_tokens)— request is within budget; proceed to LLM.Err(TokenBudgetError::*)— budget exceeded; reject the request.
Sourcepub fn release(&self, estimated: u64, actual: u64)
pub fn release(&self, estimated: u64, actual: u64)
Adjust the period budget by the difference between the estimated and actual token counts returned by the provider.
Call this after receiving the LLM response with the real token count.
If actual < estimated, the surplus is credited back. If
actual > estimated (rare for input tokens), the overage is debited.
Sourcepub fn total_tokens_estimated(&self) -> u64
pub fn total_tokens_estimated(&self) -> u64
Total tokens estimated across all requests since creation (lifetime).
Sourcepub fn period_tokens_used(&self) -> u64
pub fn period_tokens_used(&self) -> u64
Tokens consumed in the current rolling period window.
Sourcepub fn period_tokens_remaining(&self) -> u64
pub fn period_tokens_remaining(&self) -> u64
Remaining token budget in the current period.
Sourcepub fn period_utilization(&self) -> f64
pub fn period_utilization(&self) -> f64
Fraction of the period budget consumed (0.0 = empty, 1.0 = exhausted).
Sourcepub fn requests_allowed(&self) -> u64
pub fn requests_allowed(&self) -> u64
Number of requests allowed through the budget gate.
Sourcepub fn requests_rejected(&self) -> u64
pub fn requests_rejected(&self) -> u64
Number of requests rejected by the budget gate.
Sourcepub fn rejection_rate(&self) -> f64
pub fn rejection_rate(&self) -> f64
Budget rejection rate: rejected / (allowed + rejected).
Trait Implementations§
Source§impl Clone for TokenBudgetGuard
impl Clone for TokenBudgetGuard
Source§fn clone(&self) -> TokenBudgetGuard
fn clone(&self) -> TokenBudgetGuard
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for TokenBudgetGuard
impl RefUnwindSafe for TokenBudgetGuard
impl Send for TokenBudgetGuard
impl Sync for TokenBudgetGuard
impl Unpin for TokenBudgetGuard
impl UnsafeUnpin for TokenBudgetGuard
impl UnwindSafe for TokenBudgetGuard
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>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request