pub struct AtomicTokenBudget { /* private fields */ }Expand description
Atomic counterpart of AtomicBudget that tracks LLM token spend.
try_reserve_tokens deducts an estimate up front so concurrent
prompts can’t all over-commit. record_usage reconciles one
TokenReservation against the provider’s reported totals — if the
actual usage was smaller than that estimate, the difference is
returned to the pool; if larger, the overage is debited from future
reservations.
Reservations also refund their estimate on drop, so an error path
that abandons the handle without calling record_usage does not
permanently leak tokens.
Implementations§
Trait Implementations§
Source§impl Debug for AtomicTokenBudget
impl Debug for AtomicTokenBudget
Source§impl TokenBudget for AtomicTokenBudget
impl TokenBudget for AtomicTokenBudget
Source§fn try_reserve_tokens<'life0, 'async_trait>(
&'life0 self,
est: u64,
) -> Pin<Box<dyn Future<Output = Result<Option<TokenReservation>, BudgetError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn try_reserve_tokens<'life0, 'async_trait>(
&'life0 self,
est: u64,
) -> Pin<Box<dyn Future<Output = Result<Option<TokenReservation>, BudgetError>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Reserve
est prompt+completion tokens optimistically. Read moreSource§fn record_usage<'life0, 'async_trait>(
&'life0 self,
reservation: TokenReservation,
prompt: u64,
completion: u64,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn record_usage<'life0, 'async_trait>(
&'life0 self,
reservation: TokenReservation,
prompt: u64,
completion: u64,
) -> Pin<Box<dyn Future<Output = ()> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Record the actual prompt + completion token usage from a
finished call. The implementation reconciles the supplied
reservation against the observed usage.
Auto Trait Implementations§
impl Freeze for AtomicTokenBudget
impl RefUnwindSafe for AtomicTokenBudget
impl Send for AtomicTokenBudget
impl Sync for AtomicTokenBudget
impl Unpin for AtomicTokenBudget
impl UnsafeUnpin for AtomicTokenBudget
impl UnwindSafe for AtomicTokenBudget
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
Mutably borrows from an owned value. Read more