compression with a quality contract

Core Concepts

Three ideas explain why Distil's numbers are real and why the approach is different: decision-equivalence as the correct quality target, a statistical certificate that enforces it, and cache-aware cost modeling that makes aggressive compression financially correct.

How Distil works — a cache-aware compression pipeline with a quality contract AGENTDISTIL PROXY · localhostPROVIDER Claude Code · Codex Gemini · any SDK re-sends full context every turn compress → cache-pin → forward Tier-0 — lossless verbatim transforms (always on)Tier-0lossless Tier-1 — reversible decision-aware digestTier-1 digestreversible Causal / counterfactual pruning — ablation-provencausal pruneablation-proven Salience protection — model-free needle-keepingsaliencemodel-free Cache-aware placement — pin the stable prefix at the 0.1× cache-read rate cache-aware placement — stable prefix billed at the 0.1× cache-read rate RestoreStore — originals kept locally, never sent to the modelRestoreStoreoriginals kept locally · 0 tokens on wirenever sent to the model distil_expand — the model recovers any digested detail on demanddistil_expandmodel recovers any digested detailtransparent re-query · PAYG Anthropic · OpenAI Google Gemini prompt cache + LLM response → transparent expand loop → agent (it just gets the right answer) THE QUALITY CONTRACT — every operating point is certified decision-equivalent Decision-Equivalence Certificate — the conformal guarantee Decision-Equivalence Certificate conformal risk control (LTT/CRC) · distribution-free P(decision-change ≤ α) ≥ 1 − δ Shadow mode — live decision-equivalence on real traffic Shadow mode — live samples real traffic, runs it both ways rolling decision-change rate · streaming-aware Learned keep-model — the self-improving, never-regressing flywheel Learned keep-model — flywheel every expand is a label; only ever gets more conservative — never-regressing

Interactive — click any block to open the concept or technique behind it.


1. Decision-equivalence, not byte-equivalence

Most context compressors frame the problem as a string transformation: shrink the context, compare the output byte-by-byte, report overlap as accuracy. That framing has a fatal flaw — byte-equivalence and high compression are information-theoretically in tension. A claim of "87% compression, 100% accuracy" under byte-equivalence is self-contradicting; the two numbers cannot describe the same operation.

Decision-equivalence is the correct target. An agent only needs to take the same actions and produce the same outputs on the compressed context as it would on the original. "Same decision" means:

This is measurable, certifiable, and compatible with aggressive compression. Context blocks that are causally inert — they never change a decision when removed — are provably free to drop, regardless of how many bytes they contain. Distil's salience protection identifies the load-bearing frontier (model-free, using a causal ablation pass rather than a scorer model) and ensures those blocks are never touched, regardless of compression depth.

The reframe in one sentence: "100% accuracy" becomes a statistical non-inferiority guarantee on the agent's next action, not a diff of strings. On a live claude-opus-4-8 grading run, Distil hit 83.2% token savings at a measured 0% decision-change rate — with the ≤5%-at-95% bound certified offline by the conformal risk control (DERC).
Honest scope (we measured this). Next-action equivalence is a proxy. On a real multi-turn task — SWE-bench Verified, end-to-end (E7) — the proxy certificate does not transfer to task success once compression is aggressive (pass@1 52%→16% at trunc@500). The contract is right; the lesson is that a certificate earned on a single-turn proxy must be re-earned on the real task. Distil's defensible value: cache-aware savings inside a proxy-certified gate, plus a reversible recovery tier. E8 (500-instance long-horizon ReAct agent, 6 conditions, full SWE-bench Verified) is that re-earned test. The relevance-gated tier (E) achieves 36.8% pass@1 — the highest of any compressor tested — and is the only condition statistically non-inferior to full context (difference −2.4 pp, 95% CI [−5.7, +0.9]; McNemar p=0.19). It also beats Headroom (+4.2 pp, p=0.035). LLMLingua-2 removes a nearly identical share of context (52% vs 53%) yet reaches only 2.4% pass@1 — 34 points lower, isolating what is kept as the deciding factor. Lossy truncation collapses to 5.6%. Distil does not claim cheapest; Headroom is cheaper. The claim is certified accuracy with a guarantee. E10 extends this to a trajectory-level certificate (distribution-free, same LTT/HB engine as E2): with 95% confidence, the gated compressor costs a solvable task on ≤11.4% of exchangeable runs, proven out-of-sample across 1000 calibration/test splits.

Two recently shipped techniques apply decision-equivalence directly to the coding-agent read → edit → re-read loop. Cache-delta context coding (distil/cachedelta.py) replaces an exact or near-duplicate re-read with a back-reference or reference + diff. It is decision-equivalent because the prior version of the file is already present earlier in the cached conversation — prior version + diff carries the same information. It also enforces cache-monotonicity: only the volatile suffix is mutated, so every prompt-cache hit that existed before the turn still exists after it. The deepest layer, AST-structural delta (distil/astdelta.py), fingerprints Python definitions with ast.dump (invariant to whitespace, comments, and import order), so a reformat-only re-read is a zero-change reference rather than a spurious diff. For a full treatment see Techniques → Cache-delta and AST-structural delta.

Three fidelity tiers, precisely. Lossless — the model sees content byte-identical in-context (Tier-0 / --verbatim). Reversible — content is digested but byte-recoverable on demand via the local store / distil_expand (the default; like a zip you unpack only when needed). Lossy — dropped irrecoverably (every other tool). All three Distil tiers are certified decision-equivalent; only Distil offers the reversible tier, and only Distil certifies it. (--lossless-only, alias --safe, restricts to no-lossy-shaping + no-tool-injection — the reversible digest still runs; --verbatim drops to the lossless tier.)

2. The quality contract

A compression strategy in Distil does not ship unless a pre-registered statistical gate certifies it. Distil uses two complementary certification methods, both grounded in conformal prediction:

The Student-t tail used in TOST is computed from a hand-rolled regularised incomplete beta function — zero dependencies on scipy or numpy. Both gates run across all 7 corpus domains.

How the TOST gate works

  1. Pre-register the indifference margin and significance level.
  2. For each turn, run the agent on the original context and on the compressed context; record whether the decision matched.
  3. Compute paired differences (compressed score − baseline score) across all turns.
  4. Run the lower one-sided test. Reject H₀ at α → the strategy is certified non-inferior.
$ distil certify --strategy distil
decision-equivalence match rate: 100.0%
TOST non-inferiority (margin=0.02, alpha=0.05): mean diff=+0.000, p=0.0000

VERDICT: PASS  (certified non-inferior)

$ distil certify --strategy aggressive
decision-equivalence match rate: 0.0%
TOST non-inferiority (margin=0.02, alpha=0.05): mean diff=−1.000, p=1.0000

VERDICT: FAIL  (NOT certified — would degrade quality)

A strategy that cannot pass the gate simply does not ship. You can watch the gate reject a quality-degrading strategy yourself — it is not a configuration option.


3. Cache misses — not size — dominate agent cost

This is the insight most context compressors miss entirely, and it is where Distil's biggest wins come from.

In a multi-turn agent loop, the growing context is re-sent to the model at every turn. With prompt caching enabled:

The dominant cost lever is therefore not how many tokens you send — it is how many of those tokens hit the cache. A naive compressor that rewrites the context every turn drops the longest common prefix to zero, losing the 10× discount on every previously-cached block.

Naive recompression sends fewer tokens yet costs more than no compression at all, because it rewrites the cached prefix every turn and pays full input price on tokens that would otherwise cost 0.1×.

Distil's cache-aware engine models this explicitly. It keeps the prefix byte-stable across turns via schema canonicalization and volatile-field extraction, compresses only the volatile tail, and proves via simulation that the naive approach is strictly dominated.

The numbers, measured

33%
Cost cut, cache-aware lossless · ops/sre trajectory
−11%
Naive compress + cache (costs more than baseline)
10×
Cache-read discount vs. fresh input (Anthropic pricing)
Cache-aware savings vs naive compression

The 33% figure is the conservative lossless, single-trajectory result. Across a varied 64-trajectory corpus, the certified lossless ceiling is ~58%. The distil-causal mode reaches ~81% on that offline corpus. And it holds up where it counts — under live model grading: in the live head-to-head against the real installed packages (a realistic, decision-determined corpus, graded by claude-opus-4-8), Distil certified 83.2% savings at a 0% decision-change rate, while LLMLingua-2 reached only 53.1% savings and flipped 1-in-5 decisions, and Headroom achieved 35.3% at 0%.

For the compression mechanics, see Techniques. To price any trajectory yourself, see CLI Reference → distil savings.