Headroom如何无损压缩AI代理提示,节省90%的Token消耗?
BLUF 摘要
Headroom是一款本地无损压缩工具,专为AI代理提示设计,可在保持准确性的前提下,将Token消耗降低高达92%,并支持主流大语言模型提供商及常用编码代理。
Headroom: 为 AI 智能体无损压缩上下文,节省 90% 的 Token
压缩 AI 智能体读取的一切内容。答案相同,Token 消耗仅为零头。
Compress everything the AI agent reads. Same answers, token consumption is just a fraction.

智能体(Agent)每次工具调用、日志输出、数据库读取、RAG 检索片段以及注入到提示词(Prompt)中的文件,大部分内容都是重复的样板信息。Headroom 能够剥离这些噪音,保留核心信号——无损、本地化运行,且不影响准确性。
Every tool call, log line, DB read, RAG chunk, and file your agent injects into a prompt is mostly boilerplate. Headroom strips the noise and keeps the signal — losslessly, locally, and without touching accuracy.
100 条日志,1 条 FATAL 错误埋在第 67 条。两种方式都找到了它。 基准测试 10,144 tokens → Headroom 1,260 tokens — 节省 87% 的 Token,答案完全相同。python examples/needle_in_haystack_test.py
100 logs. One FATAL error buried at position 67. Both runs found it. Baseline 10,144 tokens → Headroom 1,260 tokens — 87% fewer, identical answer.
python examples/needle_in_haystack_test.py
快速开始
支持 Anthropic、OpenAI、Google、Bedrock、Vertex、Azure、OpenRouter 以及通过 LiteLLM 接入的 100 多种模型。
Works with Anthropic, OpenAI, Google, Bedrock, Vertex, Azure, OpenRouter, and 100+ models via LiteLLM.
一键包装你的编程智能体:
Wrap your coding agent — one command:
pip install "headroom-ai[all]"
headroom wrap claude # Claude Code
headroom wrap codex # Codex
headroom wrap cursor # Cursor
headroom wrap aider # Aider
headroom wrap copilot # GitHub Copilot CLI
集成到你的代码中 —— Python 或 TypeScript:
Drop it into your own code — Python or TypeScript:
from headroom import compress
result = compress(messages, model="claude-sonnet-4-5")
response = client.messages.create(model="claude-sonnet-4-5", messages=result.messages)
print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.0%})")
import { compress } from 'headroom-ai';
const result = await compress(messages, { model: 'gpt-4o' });
或作为代理服务器运行 —— 无需修改代码,支持任何语言:
Or run it as a proxy — zero code changes, any language:
headroom proxy --port 8787
ANTHROPIC_BASE_URL=http://localhost:8787 your-app
OPENAI_BASE_URL=http://localhost:8787/v1 your-app
为什么选择 Headroom
Why Headroom
保持准确性。 GSM8K 0.870 → 0.870 (±0.000)。TruthfulQA +0.030。SQuAD v2 和 BFCL 压缩后准确率均为 97%。基于可自行复现的公开 OSS 基准测试验证。
Accuracy-preserving. GSM8K 0.870 → 0.870 (±0.000). TruthfulQA +0.030. SQuAD v2 and BFCL both 97% accuracy after compression. Validated on public OSS benchmarks you can rerun yourself.
在本地运行。 无需云端 API,无数据外泄。压缩延迟为毫秒级 —— 对于 Sonnet / Opus / GPT-4 级别的模型,端到端速度比托管服务往返更快。
Runs on your machine. No cloud API, no data egress. Compression latency is milliseconds — faster end-to-end for Sonnet / Opus / GPT-4 class models than a hosted service round-trip.
HuggingFace 上的 Kompress-base。 我们开源的文本压缩器,基于真实的智能体轨迹微调 —— 包括工具输出、日志、RAG 片段、代码。通过
pip install "headroom-ai[ml]"安装。Kompress-base on HuggingFace. Our open-source text compressor, fine-tuned on real agentic traces — tool outputs, logs, RAG chunks, code. Install with
pip install "headroom-ai[ml]".跨智能体记忆与学习。 Claude Code 保存一个事实,Codex 可以读取它。
headroom learn从失败的会话中挖掘信息,并将修正直接写入CLAUDE.md/AGENTS.md/GEMINI.md—— 可靠性随时间推移而增强。Cross-agent memory and learning. Claude Code saves a fact, Codex reads it back.
headroom learnmines failed sessions and writes corrections straight toCLAUDE.md/AGENTS.md/GEMINI.md— reliability compounds over time.可逆 (CCR)。 压缩并非删除。模型始终可以调用
headroom_retrieve来获取原始字节。没有任何内容被丢弃。Reversible (CCR). Compression is not deletion. The model can always call
headroom_retrieveto pull the original bytes. Nothing is thrown away.
捆绑了 RTK 二进制文件用于 shell 输出重写 —— 完整归因见下文。
Bundles the RTK binary for shell-output rewriting — full attribution below.
架构概览
How it fits
Your agent / app
(Claude Code, Cursor, Codex, LangChain, Agno, Strands, your own code…)
│ prompts · tool outputs · logs · RAG results · files
▼
┌────────────────────────────────────────────────────┐
│ Headroom (runs locally — your data stays here) │
│ ─────────────────────────────────────────────── │
│ CacheAligner → ContentRouter → CCR │
│ ├─ SmartCrusher (JSON) │
│ ├─ CodeCompressor (AST) │
│ └─ Kompress-base (text, HF) │
│ │
│ Cross-agent memory · headroom learn · MCP │
└────────────────────────────────────────────────────┘
│ compressed prompt + retrieval tool
▼
LLM provider (Anthropic · OpenAI · Bedrock · …)
→ 架构 · CCR 可逆压缩 · Kompress-base 模型卡片
→ Architecture · CCR reversible compression · Kompress-base model card
性能证明
Proof
真实智能体工作负载下的节省效果:
Savings on real agent workloads:
工作负载 | 压缩前 (Token) | 压缩后 (Token) | 节省率 |
|---|---|---|---|
代码搜索 (100 条结果) | 17,765 | 1,408 | 92% |
SRE 事件调试 | 65,694 | 5,118 | 92% |
GitHub Issue 分类 | 54,174 | 14,761 | 73% |
代码库探索 | 78,502 | 41,254 | 47% |
在标准基准测试上保持的准确性:
Accuracy preserved on standard benchmarks:
基准测试 | 类别 | 样本数 (N) | 基线准确率 | Headroom 准确率 | 变化 (Delta) |
|---|---|---|---|---|---|
GSM8K | 数学 | 100 | 0.870 | 0.870 | ±0.000 |
TruthfulQA | 事实性 | 100 | 0.530 | 0.560 | +0.030 |
SQuAD v2 | 问答 | 100 | — | 97% | 19% 压缩率 |
BFCL | 工具调用 | 100 | — | 97% | 32% 压缩率 |
复现命令:
Reproduce:
python -m headroom.evals suite --tier 1
社区实时数据:
Community, live:
专为编程智能体打造
Built for coding agents
智能体 | 一键包装命令 | 说明 |
|---|---|---|
Claude Code |
| 包装 Claude Code CLI,自动压缩所有上下文 |
Codex |
| 包装 Codex CLI,自动压缩所有上下文 |
Cursor |
| 包装 Cursor 编辑器,自动压缩所有上下文 |
Aider |
| 包装 Aider 编程助手,自动压缩所有上下文 |
Copilot CLI |
| 包装 GitHub Copilot CLI,自动压缩所有上下文 |
支持所有通过 LiteLLM 接入的模型:
Works with any model via LiteLLM:
from headroom import compress
result = compress(messages, model="ollama/llama3")
result = compress(messages, model="bedrock/claude-v3")
result = compress(messages, model="vertex/claude-v3")
与替代方案对比
Compared to alternatives
特性 | Headroom | RTK | LLMLingua | 选择性上下文 |
|---|---|---|---|---|
无损 | ✅ 是 (CCR) | ❌ 否 | ❌ 否 | ❌ 否 |
本地运行 | ✅ 是 | ✅ 是 | ✅ 是 | ✅ 是 |
跨智能体记忆 | ✅ 是 | ❌ 否 | ❌ 否 | ❌ 否 |
代码感知 | ✅ 是 (AST) | ❌ 否 | ❌ 否 | ❌ 否 |
JSON 感知 | ✅ 是 | ❌ 否 | ❌ 否 | ❌ 否 |
代理服务器 | ✅ 是 | ❌ 否 | ❌ 否 | ❌ 否 |
开源 | ✅ 是 (Apache 2.0) | ✅ 是 | ✅ 是 | ✅ 是 |
Headroom 在内部使用 RTK 进行 shell 输出重写(完整归因)。
Headroom uses RTK internally for shell-output rewriting (full attribution).
立即开始
Get started now
pip install headroom-ai
headroom wrap claude
或
Or
pip install "headroom-ai[all]"
headroom proxy --port 8787
版权与免责声明:本文仅用于信息分享与交流,不构成任何形式的法律、投资、医疗或其他专业建议,也不构成对任何结果的承诺或保证。
文中提及的商标、品牌、Logo、产品名称及相关图片/素材,其权利归各自合法权利人所有。本站内容仅供参考,请以官方信息为准。
若本文内容或素材涉嫌侵权、隐私不当或存在错误,请相关权利人/当事人联系本站,我们将及时核实并采取删除、修正或下架等处理措施。 也请勿在评论或联系信息中提交身份证号、手机号、住址等个人敏感信息。



