This release is a pre-release and may not be stable for production use.
harpe
Deterministic context synthesis and token budgeting for AI agents.
Part of the Telarch Suite (TCI / Telarch Cognitive Infrastructure) by Perseus Computing LLC.
Why Harpe exists
In multi-turn agent runs, prompts grow until they hit model limits. When that happens, naive truncation usually lops off system rules, tool schemas, or early task context, causing the agent to hallucinate or stall.
Harpe solves this by enforcing hard token budgets before calling an LLM. It sorts context items by priority and applies deterministic eviction rules so your agents never exceed token limits or drop critical instructions.
Key design goals
- Microsecond speed: Median pruning latency of 1.72 µs across 128k context allocations adds zero noticeable overhead to agent turns.
- Predictable eviction: Policies like
LowestPriority,Fifo,TruncateTail, andLeastRelevantensure prompt assembly is fully reproducible. - Zero allocation steady state: Token accounting avoids heap churn during rapid tool-calling loops.
- No network calls: Runs fully offline on your own machine with zero cloud dependencies.
Installation
Add harpe to your Cargo.toml:
[dependencies]
harpe = "0.1.0-alpha.2"
Or via Cargo:
cargo add harpe
Example
use harpe::{ContextBudgetEngine, ContextItem, ContextSynthesizer, PrunePolicy, TokenBudget};
fn main() -> Result<(), Box<dyn std::error::Error>> {
// 1. Set budget boundaries
let budget = TokenBudget {
total_budget: 4096,
reserved_completion: 1024,
reserved_tools: 512,
safety_margin: 128,
};
assert_eq!(budget.available_context(), 2432);
// 2. Set up the engine with a token counter (or use default)
let engine = ContextBudgetEngine::default();
// 3. Assemble candidate items with priorities (priority 0 is pinned)
let items = vec![
ContextItem::new(
"system_directive",
"You are a verification agent. Run all tests.",
0,
),
ContextItem::new(
"tool_output_1",
"Execution trace: node 4 state reconciled.",
50,
),
ContextItem::new(
"historical_logs",
"Verbose background data stream...",
80,
),
];
// 4. Synthesize prompt within budget
let report = engine.synthesize(&items, &budget, PrunePolicy::LowestPriority)?;
println!("Consumed Tokens: {}", report.consumed_tokens);
println!("Included Items: {}", report.included_items.len());
println!("Pruned Items: {}", report.pruned_items.len());
assert!(report.consumed_tokens <= budget.available_context());
Ok(())
}
Python: keeping confidential items off the wire
Mark an item sensitivity="confidential" and synthesize(...) leaves it out whenever the destination is "remote" (the default), even if the item is pinned. Withheld items cost no budget and are listed in report.withheld_items, so you can see what was held back. Pass destination="local" for a model that runs on your own hardware.
from harpe import ContextBudgetEngine, ContextItem, TokenBudget
engine = ContextBudgetEngine()
items = [
ContextItem(id="rule", content="Answer in plain language.", pinned=True),
ContextItem(id="note", content="Internal-only detail.", sensitivity="confidential"),
]
report = engine.synthesize(items, TokenBudget(total_budget=1000)) # note withheld
local = engine.synthesize(items, TokenBudget(total_budget=1000), destination="local") # note kept
Benchmark results
Measured on bare-metal Linux x86_64 (rustc 1.85.0, opt-level = 3, lto = "fat"):
| Metric | Measurement | Test condition | Operational Guarantee |
|---|---|---|---|
| Small Context Assembly (median) | 2.3 µs | 10 items / 2k context ceiling (range 1.7–2.3 µs) | Deterministic assembly |
| Large Context Assembly (p50) | 35.74 µs | 200 items / 40k context ceiling | Hard ceiling priority knapsack pruning |
| 100,000-Turn Production Curve | 47 µs / turn | 100k sequential turns (knapsack_cost_curve) |
~21,000 turns/s sustained throughput |
| Context Token Reduction | 78.3% | 100,000 simulated turns (78.28% verified) | Zero overrun fail-closed ceiling guarantee |
| Budget Overruns | 0 (0.00%) | 100,000 evaluated turns | Zero overrun fail-closed ceiling guarantee |
| Tier 0 Tactical Payload | 21.25 µs | 800 tokens -> 51 tokens (93.6% reduction) | 79 bytes binary / 132 bytes wire JSON |
Run benchmarks yourself:
cargo run --release -p telarch-benchmarks
cargo run --release -p telarch-benchmarks --bin knapsack_cost_curve
cargo run --release -p telarch-benchmarks --bin restricted_payload_test
License
Copyright © 2026 Perseus Computing LLC. Released under the MIT License.
Metadata
Release files for harpe 0.1.0a2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| harpe-0.1.0a2.tar.gz | 15.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| harpe-0.1.0a2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 23.7 kB
Release files / harpe-0.1.0a2.tar.gz
| Download URL | harpe-0.1.0a2.tar.gz |
|---|---|
| Size | 15.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
39dd4bf8215f739a5712c1d9be1d7e1bb6233910f93add5fafb7421bd1513a95
|
|
BLAKE2b-256 checksum How to use checksums |
22cba134d4618f55c7dc21c0d723372fd5ea418c8e1b80628f69d97ea8350a08
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|
Release files / harpe-0.1.0a2-py3-none-any.whl
| Download URL | harpe-0.1.0a2-py3-none-any.whl |
|---|---|
| Size | 8.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc2d53ca70c8fcd94ee0c2290c54b702614fa3b24030e091a31f4e5e4eab96c6
|
|
BLAKE2b-256 checksum How to use checksums |
c0b86c5121830580543d5b9e058944261a492c86680b31c1af0e5007e8ceb1cc
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.14.7
|