enact-context
Context window management and compaction for long-running agentic executions.
Key Components
- ContextWindow: Manages the context segments within token limits
- ContextBudget: Token allocation across different segment types
- Compactor: Strategies for reducing context size when approaching limits
- TokenCounter: Token counting using tiktoken
- PromptCalibrator: Constructs calibrated prompts for spawned callables
- StepContextBuilder: Extracts learnings when steps are discovered
- ResultCondenser: Condenses child traces to 1-2k token summaries
Architecture
┌─────────────────────────────────────────────────────────────┐
│ ContextWindow │
├─────────────────────────────────────────────────────────────┤
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ System │ │ History │ │ Tools │ ... │
│ │ (critical) │ │ (high) │ │ (medium) │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │
│ ContextBudget: total=128K, used=45K, available=83K │
├─────────────────────────────────────────────────────────────┤
│ Compactor: summarize | truncate | sliding_window │
└─────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────┐
│ Callable Spawning Flow │
├─────────────────────────────────────────────────────────────┤
│ Parent Context ──► PromptCalibrator ──► Child Prompt │
│ │ │ │
│ │ StepContextBuilder │ │
│ ▼ │ ▼ │
│ Step Discovered ──────►│◄───── Child Execution │
│ │ │ │ │
│ ▼ ▼ ▼ │
│ Learnings ◄──── ResultCondenser ◄── Child Trace │
└─────────────────────────────────────────────────────────────┘
Usage
use ;
// Create a context window with GPT-4 128K budget
let budget = preset_gpt4_128k;
let mut window = new;
// Add segments
window.add_segment?;
window.add_segment?;
// Check if compaction is needed
if window.needs_compaction