Skip to main content

Crate enact_context

Crate enact_context 

Source
Expand description

§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?;
}

Re-exports§

pub use budget::ContextBudget;
pub use budget::SegmentBudget;
pub use calibrator::CalibratedPrompt;
pub use calibrator::CalibrationConfig;
pub use calibrator::PromptCalibrator;
pub use compactor::CompactionResult;
pub use compactor::CompactionStrategy;
pub use compactor::CompactionStrategyType;
pub use compactor::Compactor;
pub use condenser::CondensedResult;
pub use condenser::CondenserConfig;
pub use condenser::ExecutionTrace;
pub use condenser::ResultCondenser;
pub use segment::ContextPriority;
pub use segment::ContextSegment;
pub use segment::ContextSegmentType;
pub use step_context::StepContextBuilder;
pub use step_context::StepContextConfig;
pub use step_context::StepContextResult;
pub use step_context::StepLearning;
pub use step_context::ToolCallInfo;
pub use token_counter::TokenCounter;
pub use window::ContextWindow;

Modules§

budget
Context Budget Management
calibrator
Prompt Calibrator
compactor
Context Compaction
condenser
Result Condenser
config
Context Configuration
prelude
Prelude for common imports
segment
Context Segment Types
step_context
Step Context Builder
token_counter
Token Counter
window
Context Window Management