Skip to main content

ContextWindow

Struct ContextWindow 

Source
pub struct ContextWindow<M: BaseChatModel> { /* private fields */ }
Expand description

Context window manager for fitting messages within a token budget.

Counts tokens using a TokenCounter (defaults to TiktokenCounter), and applies a Strategy when messages exceed max_tokens.

Implementations§

Source§

impl<M: BaseChatModel> ContextWindow<M>

Source

pub fn new(max_tokens: usize) -> Result<Self, MemoryError>

Creates a new ContextWindow with the Truncate strategy and default TiktokenCounter.

P1-4: returns Result instead of panicking — a tiktoken model download/load failure (offline / missing model) returns MemoryError, so the library constructor no longer crashes on a bad local environment.

Source

pub fn with_strategy( max_tokens: usize, strategy: Strategy<M>, ) -> Result<Self, MemoryError>

Creates a new ContextWindow with a specific strategy and default TiktokenCounter.

Source

pub fn with_max_tokens(max_tokens: usize) -> Result<Self, MemoryError>

Creates a new ContextWindow with a custom max token limit and default counter/strategy.

Source

pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self

Sets a custom token counter.

Source

pub fn max_tokens(&self) -> usize

Returns the max token limit.

Source

pub async fn fit( &self, messages: Vec<Message>, ) -> Result<Vec<Message>, MemoryError>

Fits messages within the token limit by applying the configured strategy.

  • If total tokens are within max_tokens, returns messages as-is.
  • If over, applies the Strategy (truncate or summarize).
§Budget semantics (P1-4 contract)

Strategy::Truncate: System messages are always kept and do not consume budget (M7); if the System messages alone exceed max_tokens, they are returned as-is (the result may exceed the budget). Even when the budget is too small for a single conversation message, at least the newest non-System message is kept — history is never silently emptied (H7). Callers must not assume fit’s result is always within budget.

Strategy::Summarize: the summary placeholder counts toward the budget, but the LLM’s actual summary token count is unknown — the budget semantics differ from Truncate, and the intentionally inconsistent handling of System messages between the two is deliberate; each strategy defines its own.

§Arguments
  • messages - The conversation messages to fit.
§Returns

A vector of messages that fits within the token budget.

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for ContextWindow<M>

§

impl<M> !UnwindSafe for ContextWindow<M>

§

impl<M> Freeze for ContextWindow<M>
where Strategy<M>: Freeze,

§

impl<M> Send for ContextWindow<M>
where Strategy<M>: Send,

§

impl<M> Sync for ContextWindow<M>
where Strategy<M>: Sync,

§

impl<M> Unpin for ContextWindow<M>
where Strategy<M>: Unpin,

§

impl<M> UnsafeUnpin for ContextWindow<M>

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, <T as TryFrom<U>>::Error>

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