enact-context 0.0.1

Context window management and compaction for Enact
Documentation

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 enact_context::{ContextWindow, ContextBudget, Compactor};

// Create a context window with GPT-4 128K budget
let budget = ContextBudget::preset_gpt4_128k();
let mut window = ContextWindow::new(budget);

// Add segments
window.add_segment(ContextSegment::system("You are a helpful assistant..."))?;
window.add_segment(ContextSegment::user_input("Hello!"))?;

// Check if compaction is needed
if window.needs_compaction() {
    let compactor = Compactor::summarize();
    window.compact(&compactor).await?;
}