Skip to content

strategy

Context window management strategies.

Each strategy implements [ContextStrategy] and decides which messages to keep when the conversation history exceeds the token budget.

Trait for context window management strategies.

Implementations decide which messages to retain when the conversation history exceeds the available token budget. The contract:

  • Input: full message history (OpenAI JSON format) + budget
  • Output: trimmed message list that fits within budget.available_for_history
  • Ordering must be preserved (messages keep their chronological order)

Required / Provided Methods

fn apply(&self, messages: &[serde_json::Value], budget: &ContextBudget) -> Vec<serde_json::Value>

Apply this strategy to trim messages to fit within budget.

Returns a new Vec containing only the messages that should be sent to the LLM. The caller owns the returned vector.

fn name(&self) -> &''static str

Human-readable name for logging and diagnostics.

Keeps the most recent messages that fit within the token budget.

This is the simplest and most predictable strategy. It always retains the newest context, which is usually the most relevant. Messages are dropped from the beginning (oldest first).

If a system message is present at position 0, it is always retained.

Scores messages by importance and keeps the highest-scoring within budget.

Scoring heuristics:

  • System messages: always retained (infinite priority)
  • User messages: high priority (recency-weighted)
  • Assistant messages with tool_calls: medium-high priority
  • Tool result messages: medium priority (paired with their tool_call)
  • Assistant text messages: medium priority (recency-weighted)

Within each priority tier, more recent messages score higher.

Selects which context strategy to use (serializable for config).

Variants

VariantDescription
SlidingWindowKeep the most recent messages that fit within budget.
SlidingWindowWithSummarySliding window, but prepend a summary of evicted messages.
PriorityBasedScore messages by importance; keep highest-scoring within budget.
fn create_strategy(kind: &ContextStrategyKind) -> Box<dyn ContextStrategy>

Create a boxed strategy from a [ContextStrategyKind].

For SlidingWindowWithSummary, falls back to SlidingWindow since summarization requires an external Summarizer (see [HistoryManager]).