Which Strategy Should I Pick?
How It Works
Quick Start
1
Simple Usage
2
With Configuration
Optimisation Strategies
Smart Strategy Flow (Default)
Low-Level API
Strategy Reference
Truncate
Removes oldest messages first, preserving system prompt and recent context.Sliding Window
Keeps the N most recent messages within a token window.Prune Tools
Truncates old tool outputs while preserving recent ones.Summarize
Uses LLM to create a summary of older conversation.Conversation
Conversation-aware compaction that preserves topic, goals, decisions, and action items across long sessions.ConversationOptimizer automatically falls back to
SmartOptimizer when the compaction ratio is not meaningful (target_tokens / original_tokens > (1 - min_compaction_ratio)) or when internal errors occur during compaction. This ensures safe operation while preserving advanced conversation analysis when beneficial.LLM Context Compressor
Advanced LLM-driven compression with session lineage and head/tail protection.The
LLMContextCompressorOptimizer is exposed as LLM_CONTEXT_COMPRESSOR_OPTIMIZER but is not in OPTIMIZER_REGISTRY — users must instantiate it directly with an llm_client.Non-Destructive
Tags messages for exclusion without deleting them (enables undo).Smart (Recommended)
Combines all strategies intelligently based on content analysis.- Summarize tool outputs - Uses LLM to intelligently summarize large tool outputs (preserves key info)
- Truncate tool outputs - Fallback truncation for remaining large outputs
- Sliding window - Remove oldest messages
- Summarize conversation - LLM summary of older conversation if still over limit
Tool output summarization uses LLM to preserve key information instead of blindly truncating. This is enabled by default when
llm_summarize=True.Factory Function
Optimization Result
Tool Call Preservation
The optimizer preserves tool_call/tool_result pairs to maintain API validity:CLI Usage
Configuration
Best Practices
Start with smart
Start with smart
smart combines summarisation, tool pruning, sliding window, and conversation summarisation — use it unless you have a specific reason not to.Set compact_threshold below 1.0
Set compact_threshold below 1.0
Trigger compaction at 0.7–0.8 so optimisation runs before the model hard-fails on context overflow.
Use conversation for long sessions
Use conversation for long sessions
Multi-hour planning or support threads benefit from
conversation strategy — it preserves topics, goals, and decisions.Preserve tool_call pairs
Preserve tool_call pairs
All strategies keep
tool_calls and matching tool results together so API message history stays valid.Related
Context Monitor
Watch optimisation in action
Context Budgeter
Set token budgets per session
Optimizer CLI
CLI flags and interactive commands
LLM Context Compression
Advanced LLM-driven compression

