Context compression for AI agents: how Headroom cuts token costs
AI agents burn tokens on bloated context — tool outputs, logs, RAG chunks, chat history. Headroom, an open-source compression layer, trims that by 60–95% before it reaches the model. Here's how it works — and why context engineering is now core to shipping AI products.

Context compression is the practice of shrinking what an AI agent sends to a language model — tool outputs, logs, retrieved documents, conversation history — before it reaches the model, without losing the information the model needs to act. Done well, it cuts token usage by 60–95%. At agent scale, that is often the difference between an AI feature that pays for itself and one that quietly erodes its own margin.
Headroom, an open-source project (github.com/chopratejas/headroom), is one of the clearest implementations of the idea — and a useful lens on a discipline every team shipping AI products now has to take seriously. This is a technical walkthrough: what context compression is, why agent context bloats, how Headroom approaches it, and where a tool like it fits in a production stack. The savings figures below are the project's own reported numbers — a reason to benchmark on your workload, not a guarantee.
What is context compression for AI agents?
Every request an agent makes carries a payload: the system prompt, the running conversation, and — increasingly — the raw output of whatever tools the agent just ran. A single web fetch, database query or file read can dump thousands of tokens of mostly-boilerplate into the next model call. Context compression sits between the agent and the model and rewrites that payload to be smaller while preserving its meaning.
The things that bloat fastest, and that a compression layer targets first, are:
- Tool outputs and JSON API responses, which are verbose and highly repetitive.
- Source code, where structure matters more than every brace and blank line.
- Server logs and stack traces, where a handful of lines carry the signal.
- Retrieved documents — the RAG chunks that pad a prompt to improve recall.
- Long conversation histories that accrete over a multi-step task.
- Images, which can be reduced substantially before a vision model loses the plot.
Why context bloat is the hidden tax on AI agents
Three costs compound. First, money: providers bill per token, so a payload that is twice as large is roughly twice as expensive on every single call. Second, latency: more tokens take longer to transmit and process. Third, and least obvious, accuracy — models reason worse when the signal is buried, the well-documented "lost in the middle" effect where important details in a long context get overlooked.
An agent that calls tools in a loop accumulates context fast: each step appends the previous step's raw output to the next request. Agentic development is built on exactly this loop, and the loop is what makes context balloon — a ten-step task can carry the unedited residue of all ten steps by the end.
A bigger context window does not solve this; in some ways it makes it worse. A larger window costs more per call and gives the model more room to lose the thread. The fix is not more space — it is less noise.
How does Headroom work?
Headroom runs locally — on your own machine or in your own infrastructure — sitting between your agent and the LLM provider. Nothing has to change in your application code: the same requests flow through, just lighter.
Content-aware routing
The core idea is that no single algorithm compresses everything well, so Headroom detects what each piece of context is and routes it to a specialist. A component the project calls the ContentRouter does the detection; the compressors do the work:
- SmartCrusher — universal compression for JSON and other structured data.
- CodeCompressor — AST-aware compression for Python, JavaScript, Go, Rust, Java and C++, so it trims code without breaking its structure.
- Kompress-base — a small dedicated model, run via ONNX Runtime, for prose, logs and RAG text.
Reversible compression and cache alignment
Two details make this safe to run in a real loop. Reversible compression — the project's CCR — means the agent can ask for the original of anything that was compressed, so nothing is lost for good, only deferred. And a CacheAligner keeps the compressed output friendly to the provider's KV-cache, so you keep the cache-hit discounts that naïve rewriting would throw away.
How to run Headroom
There are three deployment shapes: a library you import, a proxy you point your traffic at, or an MCP server other tools can call. Installation is a single package:
# Python (3.10+)
pip install "headroom-ai[all]"
# or Node / TypeScript
npm install headroom-ai
# Wrap a coding agent — compression happens transparently
headroom wrap claude
# or run it as a drop-in proxy in front of any LLM provider
headroom proxy --port 8787As a library, you call compress() directly and hand the result to your provider SDK unchanged:
from headroom import compress
# Shrink the heavy parts of the payload — tool outputs, logs,
# RAG chunks, long histories — before they reach the model.
compressed = compress(messages, model="claude-sonnet-4-6")
# `compressed` is a drop-in replacement for `messages`.import { compress } from "headroom-ai";
// Same idea in TypeScript: fewer tokens, same meaning.
const compressed = await compress(messages, { model: "claude-sonnet-4-6" });It can also learn from your own history — mining past sessions for failures, generating compression rules, and estimating savings before you commit:
# Mine past sessions for failures and auto-generate compression rules
headroom learn --verbosity --apply
# Estimate the output-token savings on your own traffic
headroom output-savings
# Benchmark end-to-end before committing
headroom perfWhat context engineering means for teams shipping AI products
This is why context engineering is becoming its own discipline, sitting next to prompt engineering. When you build AI features and integrations or engineer SaaS and startup products on top of language models, token cost and context quality stop being infrastructure footnotes and become product constraints: they set your gross margin, your latency budget and, often, your accuracy ceiling.
It bites hardest on the road from prototype to production. Taking an AI build from a working demo to something you can actually ship usually means discovering how expensive real traffic is — and a compression layer in front of a wrapped agent like Claude Code is one of the levers that turns an exciting prototype into a viable unit-economics story.
Should you use Headroom?
It earns its place when context is genuinely your bottleneck. Reach for a compression layer like this when you are:
- Running multi-agent or heavily tool-using workflows that accumulate context fast.
- Feeding large logs, datasets or RAG corpora into every call.
- Operating under a tight token budget where margin matters.
- Working across multiple providers and want one consistent compression layer.
It is less compelling when:
- You run in a locked-down or sandboxed environment where adding a local proxy is impractical.
- You are on a single provider whose native prompt caching already covers most of your savings.
- You are on a latency-critical path where the compression step's own overhead is hard to justify.
As with any optimisation, measure before you adopt. Run Headroom's benchmark on a representative workload rather than trusting headline numbers — including the ones in this article.
The bottom line
Context used to feel free; at agent scale it is one of the largest line items you have. Whether or not you adopt Headroom specifically, the discipline it represents — measuring, routing and compressing what you send to the model — is quickly becoming table stakes for anyone running AI in production. Treat context as a first-class engineering surface, not an afterthought, and the economics of your AI features start to work in your favour.
What is context compression for AI agents?
Context compression shrinks the text an AI agent sends to a language model — tool outputs, logs, retrieved documents and conversation history — before it reaches the model, while preserving the information the model needs to act. It reduces token usage, and therefore cost and latency, without changing your application logic.
How much can context compression save?
Headroom reports reductions of 60–95% in token usage depending on the content. Structured data like JSON and logs compresses most; concise prose least. Savings are workload-specific, so benchmark on your own traffic — Headroom ships a perf command for exactly this — rather than relying on headline figures.
What is Headroom?
Headroom is an open-source context-compression layer for AI agents. It runs locally between your agent and the LLM provider, detects the type of each piece of context, and routes it to a specialised compressor for JSON, code or text. You can run it as a library, a proxy, or an MCP server, and it supports reversible compression so originals can be retrieved on demand.
Is context compression just a bigger context window?
No — in many ways it is the opposite. A larger context window costs more per call and can hurt accuracy, because models reason worse when key details are buried in a long payload (the "lost in the middle" problem). Compression reduces both the token count and the noise, so the model sees less context but more relevant context.
When should I not use a context compression layer?
Skip it when context is not your bottleneck: sandboxed environments where running a local proxy is impractical, single-provider setups whose native prompt caching already captures most savings, or latency-critical paths where the compression step's own overhead outweighs the gain. Measure first, then decide.