Skip to main content
Looking for compaction inside a running tool loop? See In-Loop Compaction.
Context Compaction Policy proactively routes long agent runs through token-budget checks before each LLM call, picking the right compaction strategy before context overflow happens.
The user runs long sessions; policy checks token budgets before each LLM call and compacts when needed.

Quick Start

1

Enable with defaults (one line)

2

Pick a preset

3

Custom policy


How It Works


Choose Your Policy


Configuration Options


Presets

CONSERVATIVE_POLICY

When to use: Short conversations, cheap models where early compaction doesn’t impact cost significantly.

BALANCED_POLICY (Default)

When to use: Most agents and standard use cases.

AGGRESSIVE_POLICY

When to use: Long tool-heavy loops or token-limited models where maximum context utilization is critical.

Routes (CompactionRoute enum)


Strategies (CompactionStrategy enum)


Tool Output Truncation

When aggressive_tool_truncation=True and any tool message content exceeds 1000 characters:
  • Threshold: 1000 chars
  • Keep: Head 300 chars + tail 200 chars
  • Marker: ...[truncated N chars for context budget]...
Set aggressive_tool_truncation=False to disable this behavior.

Model Overrides

Use model_overrides to apply different settings per model:
The override values take precedence over the base configuration for the specified models.

YAML / dict configuration

Policies serialize via to_dict() / from_dict() for CLI/YAML support:
Strategy accepts a plain string in dict/YAML form.

Deprecation Notice

ExecutionConfig.context_compaction will default to True in the next release for proactive context overflow protection. To disable, explicitly set context_compaction=False. To use the new default early, set context_compaction=True.Today the default is False — set it to True (or pass a policy) to opt in early.

Common Patterns

Use BALANCED for most agents

Use AGGRESSIVE for token-tight models

Per-model overrides for multi-model agents


Best Practices

The default 0.90 is fine for 128k models. For smaller context windows, consider lowering to 0.80-0.85 to ensure sufficient headroom.
This ensures the agent doesn’t lose the active sub-task or recent conversation context that’s critical for coherent responses.
If your agent swaps between cheap and large models, set different thresholds to optimize token usage for each model type.
For tool-heavy agents (code execution, web search, RAG), large tool outputs often contain redundant information. Truncation preserves the essential parts.

When the agent is bound to a session_id, the summaries produced by the policy-driven compactor are persisted automatically — so resume replays the compacted working history (summary + tail). See Compacted Session Resume.

Context Compaction

The reactive CompactionConfig system

Compacted Session Resume

Cheap resume via persisted compaction checkpoints

Execution Config

Agent execution configuration options

LLM Context Compression

LLM-driven message-history compression

Intelligent Conversation Compaction

Smart conversation summarization