Expand description
§harpe
Deterministic context synthesis and token budgeting for autonomous AI agents.
Harpe provides deterministic, microsecond token budgeting and priority-based context trimming for agentic loops. When language models run multi-turn agent workflows, context windows can quickly exceed hardware or inference limits. Uncontrolled growth results in context truncation, lost system prompts, or excessive inference latency.
Harpe solves this by calculating available context headroom, prioritizing pinned instructions, and pruning lower-priority context items before model invocation.
§Architecture Overview
- Token Accounting:
TokenBudgetdefines the maximum context envelope, deducting reserved tokens for model output, tool specifications, and safety margin. - Context Entities:
ContextItemrepresents individual context components (system prompts, active tool outputs, conversation turns) tagged with numeric priorities. - Synthesis Engine:
ContextBudgetEngineresolves token counts and applies pruning strategies such asPrunePolicy::LowestPriorityorPrunePolicy::Fifoto produce an exactSynthesisReportfitting within the budget.
§Quick Start
use harpe::{ContextBudgetEngine, ContextItem, ContextSynthesizer, PrunePolicy, TokenBudget};
// Define a total context envelope of 4096 tokens, reserving 1024 for output and tools
let mut budget = TokenBudget::new(4096);
budget.reserved_completion = 512;
budget.reserved_tools = 256;
budget.safety_margin = 256;
assert_eq!(budget.available_context(), 3072);
// Prepare context items with explicit priorities (0 is pinned/highest priority)
let system_prompt = ContextItem::new("sys", "You are an autonomous systems assistant.", 0);
let conversation = ContextItem::new("hist", "Previous user query and reasoning traces.", 50);
let scratchpad = ContextItem::new("scratch", "Temporary debug buffer with logs.", 90);
let engine = ContextBudgetEngine::default();
let report = engine.synthesize(
&[system_prompt, conversation, scratchpad],
&budget,
PrunePolicy::LowestPriority,
).expect("Synthesis should succeed");
println!("Consumed tokens: {}", report.consumed_tokens);
println!("Remaining tokens: {}", report.budget_remaining);Structs§
- Context
Budget Engine - Primary context synthesis and budgeting engine.
- Context
Item - A discreet unit of context presented to the synthesis engine.
- Heuristic
Token Counter - Fast heuristic token counter estimating tokens via word and character ratios.
- Synthesis
Report - Detailed outcome of a context synthesis execution.
- Token
Budget - Token budget configuration for an agent invocation turn.
Enums§
- Harpe
Error - Errors returned during context synthesis and budgeting operations.
- Prune
Policy - Pruning policies supported by the context synthesis engine.
Constants§
Traits§
- Context
Synthesizer - Trait implemented by context synthesis engines.
- Pruning
Hook - Hook for lifecycle interception before and after pruning runs.
- Token
Counter - Trait for measuring token counts of text slices.