pub struct ContextWindowManager { /* private fields */ }Expand description
Context Window Manager for tracking and managing token usage.
Tracks cumulative token usage across API calls and provides utilities for calculating available context space.
Implementations§
Source§impl ContextWindowManager
impl ContextWindowManager
Sourcepub fn new(model_id: &str) -> Self
pub fn new(model_id: &str) -> Self
Create a new ContextWindowManager for the specified model.
§Arguments
model_id- The model identifier (e.g., “claude-3-5-sonnet-20241022”)
§Example
use aster::context::window_manager::ContextWindowManager;
let manager = ContextWindowManager::new("claude-3-5-sonnet-20241022");
assert_eq!(manager.get_context_window_size(), 200_000);Sourcepub fn get_model_context_window(model_id: &str) -> usize
pub fn get_model_context_window(model_id: &str) -> usize
Sourcepub fn calculate_available_context(model_id: &str) -> usize
pub fn calculate_available_context(model_id: &str) -> usize
Sourcepub fn calculate_output_space(model_id: &str) -> usize
pub fn calculate_output_space(model_id: &str) -> usize
Sourcepub fn update_model(&mut self, model_id: &str)
pub fn update_model(&mut self, model_id: &str)
Update the model and recalculate context window size.
§Arguments
model_id- The new model identifier
Sourcepub fn record_usage(&mut self, usage: TokenUsage)
pub fn record_usage(&mut self, usage: TokenUsage)
Record token usage from an API call.
Updates cumulative totals and stores the current usage.
§Arguments
usage- Token usage from the API call
Sourcepub fn get_usage_percentage(&self) -> f64
pub fn get_usage_percentage(&self) -> f64
Get the current context usage percentage.
Calculates usage based on total input tokens relative to context window.
§Returns
Usage percentage (0.0 - 100.0)
Sourcepub fn is_near_limit(&self, threshold: f64) -> bool
pub fn is_near_limit(&self, threshold: f64) -> bool
Sourcepub fn get_context_window_size(&self) -> usize
pub fn get_context_window_size(&self) -> usize
Get the context window size.
Sourcepub fn get_total_input_tokens(&self) -> usize
pub fn get_total_input_tokens(&self) -> usize
Get total input tokens consumed.
Sourcepub fn get_total_output_tokens(&self) -> usize
pub fn get_total_output_tokens(&self) -> usize
Get total output tokens generated.
Sourcepub fn get_available_context(&self) -> usize
pub fn get_available_context(&self) -> usize
Get available context space for the current model.
Sourcepub fn get_output_space(&self) -> usize
pub fn get_output_space(&self) -> usize
Get output space reservation for the current model.
Sourcepub fn get_model_id(&self) -> &str
pub fn get_model_id(&self) -> &str
Get the current model ID.
Sourcepub fn get_current_usage(&self) -> Option<&TokenUsage>
pub fn get_current_usage(&self) -> Option<&TokenUsage>
Get the most recent API call usage.
Sourcepub fn get_stats(&self) -> ContextWindowStats
pub fn get_stats(&self) -> ContextWindowStats
Sourcepub fn get_cache_stats(&self) -> CacheStats
pub fn get_cache_stats(&self) -> CacheStats
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Reset all statistics.
Clears cumulative token counts and current usage.
Sourcepub fn get_remaining_tokens(&self) -> usize
pub fn get_remaining_tokens(&self) -> usize
Get remaining available tokens.
Calculates how many more input tokens can be used before reaching the available context limit.
§Returns
Remaining available tokens (0 if limit exceeded)
Trait Implementations§
Source§impl Clone for ContextWindowManager
impl Clone for ContextWindowManager
Source§fn clone(&self) -> ContextWindowManager
fn clone(&self) -> ContextWindowManager
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for ContextWindowManager
impl Debug for ContextWindowManager
Auto Trait Implementations§
impl Freeze for ContextWindowManager
impl RefUnwindSafe for ContextWindowManager
impl Send for ContextWindowManager
impl Sync for ContextWindowManager
impl Unpin for ContextWindowManager
impl UnsafeUnpin for ContextWindowManager
impl UnwindSafe for ContextWindowManager
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
Source§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
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> 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 moreSource§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::RequestSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self, then passes self.as_ref() into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self, then passes self.as_mut() into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow() only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref() only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.