pub struct ContextTracker { /* private fields */ }Expand description
Tracks context size using real token counts from provider responses combined with estimates for messages added after the last response.
This gives more accurate context size tracking than pure estimation, since providers report actual token counts in their usage data.
§Example
use phi_core::context::ContextTracker;
use phi_core::types::Usage;
let mut tracker = ContextTracker::new();
// After receiving an assistant response with usage data:
tracker.record_usage(&Usage { input: 1500, output: 200, ..Default::default() }, 3);Implementations§
Source§impl ContextTracker
impl ContextTracker
pub fn new() -> Self
Sourcepub fn record_usage(&mut self, usage: &Usage, message_index: usize)
pub fn record_usage(&mut self, usage: &Usage, message_index: usize)
Record usage from an assistant response.
Call this after each assistant message to update the tracker with real token counts from the provider.
Sourcepub fn estimate_context_tokens(&self, messages: &[AgentMessage]) -> usize
pub fn estimate_context_tokens(&self, messages: &[AgentMessage]) -> usize
Estimate current context size.
Uses real usage from the last assistant response as a baseline, then adds estimates (chars/4) for any messages added since. Falls back to pure estimation if no usage data is available.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ContextTracker
impl RefUnwindSafe for ContextTracker
impl Send for ContextTracker
impl Sync for ContextTracker
impl Unpin for ContextTracker
impl UnsafeUnpin for ContextTracker
impl UnwindSafe for ContextTracker
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