Skip to main content

Module strategy

Module strategy 

Source

Enums§

ContextStrategy
Context management strategy for controlling how conversation history is trimmed before being sent to the LLM.

Functions§

apply_strategy
Apply a ContextStrategy to a message list and return the filtered list.
keep_first_last
Positional-bias mitigation (lost-in-the-middle): reorder so the most important items sit at the beginning and end of the context, where models attend most reliably (Anthropic’s ~18% middle drop vs 30-50% elsewhere, fable §13). Keeps the first head and last tail items in place and moves the middle to the end (least-attended region) preserving order.
resolve_effective_strategy
Resolve ContextStrategy::Auto into a concrete strategy using the caller’s model-specific effective context window (CH-05). Any other strategy passes through unchanged — this is the seam a caller with real model information (e.g. RunManager) should use before reaching trim_count/apply_strategy/trim_with_summarization, which only see DEFAULT_AUTO_CONTEXT_TOKENS if Auto reaches them unresolved.
split_pinned_system
Split a message list into its leading system message, if any, and the conversation turns that follow.
strategy_for_model
Choose a context strategy tuned to a model’s effective context window (CH-05, fable §13). Larger-context models get a proportionally larger sliding window before summarisation kicks in; small-context models summarise sooner. effective_context_tokens should be the usable window (typically 50-80% of advertised), not the advertised maximum.
trim_count
Apply a ContextStrategy to any cloneable list, trimming by count/position.
trim_history
Trim history under strategy, keeping system pinned at index 0.
trim_history_with_marked_prose
Apply a declared strategy while allowing the trusted host to identify the only prose spans that summarization may replace. Span indices address history, excluding the separately pinned system message.
trim_history_with_summarization
Async counterpart of trim_history: runs LLM-backed summarization for the strategies that need it, with the system message pinned out of reach. Without explicit host marks, summarization leaves history unchanged.
trim_with_marked_prose
Summarize only trusted-host-marked prose that is old enough for the configured strategy. Unmarked messages stay in their original position. Any missing driver or failed/cancelled/invalid/over-budget summary returns the original message vector without a truncation fallback.
trim_with_summarization
Compatibility entry point for callers without host-owned eligibility metadata. Unknown content is protected by default, so summarizing strategies leave it unchanged.