Deep-dive 3 — Context as a Contended Cache; Compaction as Lossy Encoding
Consequence from the field guide: treat compaction as an encoder-design problem. Decide explicitly what must survive a pass — decisions, constraints, identifiers, open questions — drop the rest, and measure the reliability delta on your evals when you change the policy. You are doing rate-distortion tradeoffs, so measure the distortion.
How it's done
Manage the window as a scarce resource, not a bucket to fill. The primary levers:
- Compaction — when the window nears its limit, summarize older turns and reinitialize with the summary. This is your eviction policy, and it is lossy.
- Structured note-taking — externalize a running scratchpad (decisions, state) to a file the agent can re-read, rather than relying on the window to hold it.
- Tool scoping — every exposed tool definition costs tokens and adds distraction; give the agent the smallest sufficient toolset.
- Just-in-time retrieval — carry lightweight identifiers (file paths, IDs, queries) and page in the underlying content only when needed, rather than pre-loading everything.
- Position-aware placement — put load-bearing tokens where attention is strongest (the start and end), not buried in the middle.
Tools available
- Provider compaction: Anthropic's compaction API (
compact-2026-01-12) and automatic context compaction — production-ready across major clouds with zero-data-retention support. - Framework state management: LangGraph state, and agent runtimes that expose compaction hooks; claude-mem-style persistent compression layers.
- Context-awareness tooling: real-time feedback on remaining context capacity after each tool call, so the agent (or harness) can compact proactively.
- Research prototypes: ACE (Agentic Context Engineering, ICLR 2026), Cartridges, parallel block compaction, structurally-lossless trimming — see research line.
Best practices
Compact before the cliff, the way you evict a cache before it saturates. Adopt a lossy-encoder mindset: name the fields that must survive (decisions made, hard constraints, IDs, open questions) and protect them; let the discursive middle go. Keep the working set small and high-signal — "the smallest set of high-signal tokens that maximizes the desired outcome." Scope tools aggressively; in some production agents the majority of input tokens are system prompts and tool definitions, which is pure distraction tax. Externalize anything that must persist losslessly to a file and page it in via JIT retrieval, because compaction will eventually paraphrase it away.
Failure points teams ignore — and what each costs
- Treating compaction as lossless. The 98%-reduction case (132k tokens → ~2.3k) is not free; it discards nuance built up over an entire session. Consequence: the one constraint or decision that later matters gets summarized out, and the agent contradicts itself or repeats work — with no error, just silent regression.
- Using prompts to control summary volume. Research shows summarization output is largely input- and prompt-invariant, so "tell it to be more detailed" is an unreliable knob. Consequence: you think you're tuning retention and you aren't; the lost information is lost regardless.
- "Bigger window will fix it." Consequence: you pay for a larger window and still hit context rot and lost-in-the-middle — capacity was never the whole problem; signal-to-noise was.
- Tool-list bloat. Consequence: attention is pulled off the task, error rates rise, and every single turn costs more.
- No measurement of compaction quality. Consequence: you discover what a compaction pass dropped only when a failure surfaces in production — the most expensive possible place to learn it.
How to evaluate and mitigate
Evaluate: run your task evals with and without a compaction-policy change and read the reliability delta. Test information survival directly: seed a known fact early, force a compaction, then probe for it. Measure failure rate as a function of context fill — reliability often degrades well before the hard limit. Combine synthetic needle-in-a-haystack with your real input distribution; the synthetic version flatters the system.
Mitigate: protect must-keep fields explicitly; prefer structured, itemized context (the ACE direction) over free-form summary blobs; use JIT retrieval so the source of truth lives outside the lossy window; scope tools; place critical tokens at the edges.
Practical vs. still research
Practical / mature: compaction, summarization, tool scoping, JIT retrieval, and position-aware placement are production-ready and should be default hygiene.
Emerging / active research: ACE-style incremental, itemized context representations; parallel block compaction (giving operators direct control over summary volume, which prompt engineering can't); Cartridges and other long-context-representation methods; DAG-based "structurally lossless" trimming. These are promising and worth tracking, not yet standard. Provably lossless compaction under a token budget is, by definition, mostly research — you are choosing what to lose, so choose deliberately and measure it.