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>
impl<M: BaseChatModel> ContextWindow<M>
Sourcepub fn new(max_tokens: usize) -> Result<Self, MemoryError>
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.
Sourcepub fn with_strategy(
max_tokens: usize,
strategy: Strategy<M>,
) -> Result<Self, MemoryError>
pub fn with_strategy( max_tokens: usize, strategy: Strategy<M>, ) -> Result<Self, MemoryError>
Creates a new ContextWindow with a specific strategy and default TiktokenCounter.
Sourcepub fn with_max_tokens(max_tokens: usize) -> Result<Self, MemoryError>
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.
Sourcepub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
pub fn with_counter(self, counter: Arc<dyn TokenCounter>) -> Self
Sets a custom token counter.
Sourcepub fn max_tokens(&self) -> usize
pub fn max_tokens(&self) -> usize
Returns the max token limit.
Sourcepub async fn fit(
&self,
messages: Vec<Message>,
) -> Result<Vec<Message>, MemoryError>
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.