Local compression of LLM input
Headroom addresses a specific cost issue: AI agents process large, verbose contexts containing command output, API responses, logs and retrieved documents. Every token costs money, and output tokens on pricier models cost roughly five times as much as input tokens. Headroom compresses this content on your machine before it reaches the LLM.
It is a Python project under Apache-2.0, published as headroom-ai, with three integration methods: libraries for Python and TypeScript, a proxy providing an OpenAI-compatible HTTP interface without code changes, and an MCP server exposing compression as agent tools. All three share the same compression engines; choose according to the cost of changing your project.
Specialized compressors divide the work: SmartCrusher compresses JSON; CodeCompressor provides AST-aware code compression; Kompress-base is a text compression model trained on agentic traces; CacheAligner stabilizes prefixes to improve KV-cache hits; CCR provides reversible compression with originals cached locally for retrieval on demand. The official description states that compressing tool output, logs, files and RAG excerpts reduces tokens by 60–95% without changing answers.
Tool output / Logs / RAG
Local compressors
compress()
LLM
Retrieve originals when needed
Compress tool output, logs, files and RAG excerpts before they reach the LLM. Use 60–95% fewer tokens with unchanged answers.
Library, proxy and MCP integration
Requires Python 3.10+. Choose features with installation extras; [all] is common. The most direct approach uses the library: import compress and compress messages before sending them to the model.
# Python library(install all extras)
pip install "headroom-ai[all]"
# Compress messages in code before sending them to the model
from headroom import compress
compressed = compress(messages, model="...")
# TypeScript / Node also has a library
npm install headroom-ai
await compress(messages, { model })Proxy without code changes
Start the proxy if you prefer not to change application code. It is an OpenAI-compatible drop-in HTTP proxy; route traffic through it. The MCP server installs with one command and exposes three tools: headroom_compress, headroom_retrieve and headroom_stats.
# HTTP proxy requiring no code changes(OpenAI compatible)
headroom proxy --port 8787
# Install MCP server,expose three compression tools
headroom mcp installCompressors and integration interfaces
Specialized compressors handle structured JSON, code, natural-language text and KV-cache prefixes. The library, proxy and MCP server connect the same engines to different layers of your system. Capabilities include five compression mechanisms and three integration points.
Compressor · 01
SmartCrusher
Compress JSON
Compress structured JSON, including large API responses and tool outputs.
Compressor · 02
CodeCompressor
AST-aware code compression
AST-based compression for six languages, Python, JavaScript, Go, Rust, Java and C++.
Compressor · 03
Kompress-base
Text compression model
A HuggingFace model trained on agentic traces to compress natural-language text.
Compressor · 04
CacheAligner
Stabilize KV-cache
Stabilize prefixes to improve KV-cache hit rates. More hits mean less recomputation, reducing both latency and cost.
Compressor · 05
CCR
Reversible compression
Reversible compression with originals cached locally, retrieval on demand and configurable TTL. Restore full content when needed.
Entry · 06
Library
Library integration
Python compress(messages) or TypeScript await compress(messages, { model }) gives explicit control of compression points in code.
Entry · 07
Proxy
No-code-change HTTP proxy
headroom proxy --port 8787 starts an OpenAI-compatible drop-in proxy. Route traffic through it without changing application code.
Entry · 08
MCP server
Agent tools
headroom mcp install exposes headroom_compress, headroom_retrieve and headroom_stats to agents.
Comparison of three integration methods
| Method | Command / Interface | Code changes | Suitable use |
|---|---|---|---|
Library | compress(messages, model=…) | Yes (explicit calls) | Precise control of compression points |
Proxy | headroom proxy --port 8787 | No | Keep existing code |
MCP server | headroom mcp install | No (tool calls) | Agent-controlled compression / retrieval |
Eight usage points
These points come from the official Headroom README and documentation site. Choose an integration method based on implementation cost, retrieve originals when needed and measure reductions on your own workloads; do not rely only on the headline 60–95% range.
Use the proxy to keep existing code
The library requires calling compress() in code. To keep existing code, start an OpenAI-compatible proxy with headroom proxy --port 8787 and route traffic through it.
Source · Official README
Retrieve originals with CCR
CCR provides reversible compression with local caching, retrieval on demand and configurable TTL. Retrieve full content for replay or audits when needed; otherwise retain the compressed form.
Source · Official README · CCR
Output shaper reduces model output
Set HEADROOM_OUTPUT_SHAPER=1 before starting the proxy to reduce model output through verbosity steering and effort routing as well as compressing input. Output tokens on pricier models cost roughly five times as much as input tokens.
Source · Official README · output shaper
headroom learn derives settings from failed sessions
Preview with headroom learn --verbosity; add --apply to save. It examines failed sessions and writes corrections into each agent's own markdown file.
Source · Official README · learn
Install only required extras
Instead of [all] every time, choose [proxy], [mcp], [code], [ml], [langchain] and other extras as needed for a smaller installation with fewer dependencies.
Source · Official README · extras
CodeCompressor covers six languages
AST-aware code compression supports Python, JavaScript, Go, Rust, Java and C++, reducing code context sent to the LLM.
Source · Official README · CodeCompressor
Add --memory --code-graph when wrapping claude
Headroom wraps multiple coding agents. Claude Code has dedicated options, headroom wrap claude --memory --code-graph, adding cross-session memory and a code graph.
Source · Official README · agent wrapping
Benchmarks require workload verification
Official benchmarks show accuracy is broadly retained, GSM8K math 0.870, TruthfulQA 0.560 and SQuAD v2 at 97% with 19% compression. Measure on your own workloads as well.
Source · Official README · benchmarks
Proxy and token reduction verification
Start the proxy, compress code-search results once through an MCP tool, inspect actual reductions in stats and then reduce model output with output shaper. The official benchmark reports 100 code-search results compressed from 17,765 to 1,408 tokens, saving 92%.
$ pip install "headroom-ai[all]"
$ headroom proxy --port 8787
ok: Headroom proxy listening on http://127.0.0.1:8787
$ headroom mcp install
hl: 17,765 → 1,408 tokens · saved 92%
ok: restored original from local cache
$ export HEADROOM_OUTPUT_SHAPER=1
$ headroom proxy --port 8787
$ headroom output-savings
ok: estimated output reduction reported with confidence intervals
Code search with 100 results: 17,765 → 1,408 tokens, saving 92%. Accuracy is broadly retained.
Type-aware compression and retrieval
Token reductions come from type-aware local compression, without directly discarding information: JSON uses SmartCrusher, code uses CodeCompressor, text uses Kompress-base and prefixes use CacheAligner. CCR retrieves full originals from local cache when needed. Both compression and retrieval happen on your machine, keeping data flow controlled.
Output shaper changes prompt and effort. It reduces output tokens through verbosity steering and effort routing, modifying the system prompt and amount of thinking. Before production use, measure quality and savings on your own workloads to decide whether to keep it enabled.
Compression limits
Headroom advanced integration
After basic compression works, extend integration with output reduction, settings learned from failures, framework interfaces and wrappers for your usual coding agents.
Advanced settings
1. Enable output shaper by starting the proxy after export HEADROOM_OUTPUT_SHAPER=1. Verbosity steering adds concise instructions to the system prompt; effort routing reduces thinking on routine steps such as processing tool results. Use headroom output-savings to measure estimated savings.
2. Adjust settings with the learn loop. Preview with headroom learn --verbosity and add --apply to save the best settings. It examines failed sessions and writes corrections into agent-specific markdown files.
3. Connect your framework. Interfaces include Anthropic SDK withHeadroom(new Anthropic()), OpenAI SDK withHeadroom(new OpenAI()), LangChain HeadroomChatModel(your_llm), Vercel AI SDK wrapLanguageModel() middleware and LiteLLM HeadroomCallback().
4. Wrap coding agents. Headroom can wrap Claude Code, Codex, Cursor, Aider, Copilot CLI, OpenClaw and Cortex Code. Claude Code has dedicated options, headroom wrap claude --memory --code-graph, for cross-session memory and a code graph.
5. Control installation with the extras matrix. Choose [proxy], [mcp], [ml], [code], [memory], [relevance], [langchain] and others as needed. On Apple Silicon, set HEADROOM_EMBEDDER_RUNTIME=pytorch_mps to run embeddings on the GPU.
Official resources
① Official documentation: Full installation, compressors, integration interfaces and benchmark documentation. ② github.com/headroomlabs-ai/headroom: Source, README, issues and change history.
Compress tool output, logs, files and RAG excerpts before they reach the LLM. Choose Library, proxy or MCP server.