Skip to main content

ContextWindow

Struct ContextWindow 

Source
pub struct ContextWindow { /* private fields */ }
Expand description

A token-budgeted message buffer for managing conversation context.

Tracks messages with their token counts and provides compaction signals when the context approaches capacity.

Implementations§

Source§

impl ContextWindow

Source

pub fn new(max_tokens: u32, reserved_for_output: u32) -> Self

Creates a new context window.

§Arguments
  • max_tokens - Maximum tokens the model can handle (e.g., 128000 for GPT-4)
  • reserved_for_output - Tokens to reserve for model response (e.g., 4096)
§Panics

Panics if reserved_for_output >= max_tokens.

Source

pub fn push(&mut self, message: ChatMessage, tokens: u32)

Adds a message with its token count.

New messages are compactable by default. Use protect_recent to mark recent messages as non-compactable.

§Arguments
  • message - The chat message to add
  • tokens - Token count for this message (from provider usage or estimation)
Source

pub fn available(&self) -> u32

Returns the number of tokens available for new content.

This is max_tokens - reserved_for_output - total_tokens().

Source

pub fn iter(&self) -> impl Iterator<Item = &ChatMessage>

Returns an iterator over the current messages.

Prefer this over messages to avoid allocation.

Source

pub fn messages(&self) -> Vec<&ChatMessage>

Returns the current messages as a vector of references.

For iteration without allocation, use iter instead.

Source

pub fn messages_owned(&self) -> Vec<ChatMessage>

Returns owned copies of the current messages.

Use this when you need to pass messages to a provider that takes ownership.

Source

pub fn total_tokens(&self) -> u32

Returns the total tokens currently in the window.

Source

pub fn len(&self) -> usize

Returns the number of messages in the window.

Source

pub fn is_empty(&self) -> bool

Returns true if the window contains no messages.

Source

pub fn needs_compaction(&self, threshold: f32) -> bool

Checks if compaction is needed based on a threshold.

Returns true if the window is more than threshold percent full.

§Arguments
  • threshold - A value between 0.0 and 1.0 (e.g., 0.8 for 80%)
§Example
use llm_stack_core::context::ContextWindow;
use llm_stack_core::ChatMessage;

let mut window = ContextWindow::new(1000, 200);
window.push(ChatMessage::user("Hello"), 700);

// 700 / (1000 - 200) = 87.5% full
assert!(window.needs_compaction(0.8));
assert!(!window.needs_compaction(0.9));
Source

pub fn compact(&mut self) -> Vec<ChatMessage>

Removes and returns compactable messages.

Messages marked as non-compactable (via protect_recent or system messages) are retained. Returns the removed messages so the caller can summarize them.

§Returns

A vector of removed messages, in their original order.

Source

pub fn protect_recent(&mut self, n: usize)

Marks the most recent n messages as non-compactable.

This protects recent context from being removed during compaction. Call this after adding messages that should be preserved.

§Arguments
  • n - Number of recent messages to protect (from the end). If n exceeds the window length, all messages are protected.
Source

pub fn protect(&mut self, index: usize)

Marks a message at the given index as non-compactable.

Useful for protecting specific messages like system prompts.

§Panics

Panics if index >= len().

Source

pub fn unprotect(&mut self, index: usize)

Marks a message at the given index as compactable.

Reverses the effect of protect.

§Panics

Panics if index >= len().

Source

pub fn is_protected(&self, index: usize) -> bool

Returns whether the message at index is protected from compaction.

§Panics

Panics if index >= len().

Source

pub fn input_budget(&self) -> u32

Returns the input budget (max_tokens - reserved_for_output).

Source

pub fn max_tokens(&self) -> u32

Returns the maximum tokens this window was configured with.

Source

pub fn reserved_for_output(&self) -> u32

Returns the tokens reserved for output.

Source

pub fn clear(&mut self)

Clears all messages from the window.

Source

pub fn token_count(&self, index: usize) -> u32

Returns the token count for the message at the given index.

§Panics

Panics if index >= len().

Source

pub fn update_token_count(&mut self, index: usize, tokens: u32)

Updates the token count for the message at the given index.

Useful when you get accurate token counts from the provider after initially using estimates.

§Panics

Panics if index >= len().

Trait Implementations§

Source§

impl Debug for ContextWindow

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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: 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: 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 = Infallible

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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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