Reduce input tokens for AI agents with a local compression tool
Imagine a coding agent reading tool outputs or logs with fewer unnecessary tokens, allowing you to get the same answer at a lower cost. Headroom is a local tool that compresses data before it reaches the LLM, ensuring prompts and file contents are not sent externally. It can be integrated into Python and TypeScript apps, as well as various coding agents.
Measured token savings
In four scenarios using actual MCP server output formats, input tokens were reduced by 21 to 57 percent. Repetitive JSON arrays or log lines are compressed by more than 90 percent, while prose sees little reduction. Accuracy evaluations on GSM8K and SQuAD v2 showed that existing performance was maintained or slightly improved.
Compression strategies by data type
ContentRouter detects content types and selects the appropriate compressor. SmartCrusher handles JSON arrays and objects, CodeCompressor processes AST-based source code, and Kompress-v2-base handles text. CacheAligner flags volatile content that could break a provider's KV-cache but does not rewrite the prompt. CCR caches the original locally so the model can retrieve the original text via headroom_retrieve when needed.
Integration methods for agents and apps
Use the library if you are constructing prompts in your own code, or use a proxy or wrapper if you are configuring agents without code changes.
- Library In Python or TypeScript apps, call the
compress(messages)function directly to compress inline. It runs within the process without requiring an API key or model download. - Proxy Running
headroom proxyallows clients in any language to use compressed prompts without code changes. All OpenAI-compatible clients are supported. - Agent wrapping Commands like
headroom wrap claudeset up coding agents such as Claude Code, Codex, and Cursor in one step. The proxy terminates when the session ends, and you can revert usingheadroom unwrap. - MCP server Provides
headroom_compress,headroom_retrieve, andheadroom_statstools for MCP clients. It also includes features for sharing memory and removing duplicates between multiple agents.
Output token reduction feature
You can also reduce the tokens in responses written by the model. It adds an instruction to answer concisely at the end of the system prompt, reducing unnecessary introductions or code re-inference. This feature is supported on Anthropic and OpenAI-compatible endpoints and is designed to maintain prompt cache hits.
Considerations before applying
Compression effects are minimal for short conversational prompts or prose-heavy tasks, so it is not recommended for those use cases. Python 3.11 or higher is required, and on x86 environments, some features are disabled if the AVX2 instruction set is missing. macOS Apple Silicon and Linux support native wheels, but Docker installation is recommended for Intel macOS. If you use SSL inspection or MITM proxies on enterprise networks, CA certificate configuration may be necessary.