strategy
Context window management strategies.
Each strategy implements [ContextStrategy] and decides which messages
to keep when the conversation history exceeds the token budget.
Traits
Section titled “Traits”ContextStrategy
Section titled “ContextStrategy”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 strHuman-readable name for logging and diagnostics.
Structs
Section titled “Structs”SlidingWindow
Section titled “SlidingWindow”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.
PriorityBased
Section titled “PriorityBased”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.
ContextStrategyKind
Section titled “ContextStrategyKind”Selects which context strategy to use (serializable for config).
Variants
| Variant | Description |
|---|---|
SlidingWindow | Keep the most recent messages that fit within budget. |
SlidingWindowWithSummary | Sliding window, but prepend a summary of evicted messages. |
PriorityBased | Score messages by importance; keep highest-scoring within budget. |
Functions
Section titled “Functions”create_strategy
Section titled “create_strategy”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]).