Skip to main content
Reduce context size automatically before models hit token limits — preventing overflow errors and cutting costs.
The user keeps chatting; the optimizer compacts context automatically when usage nears the model limit.

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.
Best for: Simple cases where old context is not important.

Sliding Window

Keeps the N most recent messages within a token window.
Best for: Conversations where recent context matters most.

Prune Tools

Truncates old tool outputs while preserving recent ones.
Best for: Tool-heavy conversations with large outputs.

Summarize

Uses LLM to create a summary of older conversation.
Best for: Long conversations where context continuity matters.

Conversation

Conversation-aware compaction that preserves topic, goals, decisions, and action items across long sessions.
Low-level class usage:
Configuration Options:
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.
Best for: Multi-hour planning, iterative development, long support sessions where topic/goal continuity matters more than literal message history. See Intelligent Conversation Compaction for the full conceptual deep-dive.

LLM Context Compressor

Advanced LLM-driven compression with session lineage and head/tail protection.
Best for: Long conversations requiring intelligent summarization with audit trails.
The LLMContextCompressorOptimizer is exposed as LLM_CONTEXT_COMPRESSOR_OPTIMIZER but is not in OPTIMIZER_REGISTRY — users must instantiate it directly with an llm_client.
See LLM Context Compression for detailed usage.

Non-Destructive

Tags messages for exclusion without deleting them (enables undo).
Best for: When you need to preserve full history for audit/undo. Combines all strategies intelligently based on content analysis.
Order of operations (Smart Strategy):
  1. Summarize tool outputs - Uses LLM to intelligently summarize large tool outputs (preserves key info)
  2. Truncate tool outputs - Fallback truncation for remaining large outputs
  3. Sliding window - Remove oldest messages
  4. 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

smart combines summarisation, tool pruning, sliding window, and conversation summarisation — use it unless you have a specific reason not to.
Trigger compaction at 0.7–0.8 so optimisation runs before the model hard-fails on context overflow.
Multi-hour planning or support threads benefit from conversation strategy — it preserves topics, goals, and decisions.
All strategies keep tool_calls and matching tool results together so API message history stays valid.

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