概述
Headroom (headroomlabs-ai/headroom) 是一个 AI agent 上下文压缩层(Context Compression Layer),能自动压缩 Agent 读取的 tool output、日志、文件等内容,在不影响准确性的前提下节省 60–95% 的输入 tokens。
本项目已成功集成到 OpenClaw 中,作为 ContextEngine 插件运行。
相关说明
- 仓库: github.com/headroomlabs-ai/headroom
- 文档: headroom-docs.vercel.app
- 许可证: Apache 2.0
[toc]
概述
Headroom 是一款 AI 代理的上下文压缩层。它位于 AI 代理与 LLM 之间(或作为代理的 ContextEngine 插件),实时压缩传递给大模型的内容——尤其是 tool call 的输出、搜索结果、代码库浏览、日志等——然后才送到 LLM。
核心原理
- 压缩不失真:对于不需要完全保真的内容(搜索结果列表、文件列表、JSON 数据等)进行摘要式压缩
- 代码和 grep 不压缩:Python 源码、grep 匹配行等重要内容保持原样
- 代理无关设计:支持 OpenClaw、Claude Code、Codex、Cline 等多种 Agent
安装与集成
安装 Headroom
pipx install "headroom-ai"
pipx inject "headroom-ai" "headroom-ai[proxy]"与 OpenClaw 集成
Headroom 通过 OpenClaw 的 ContextEngine 插件系统集成:
# 方式一:自动集成(需 npm 包可访问)
headroom wrap openclaw
# 方式二:从源码构建(推荐,GitHub 仓库构建)
git clone --depth 1 https://github.com/headroomlabs-ai/headroom.git /tmp/headroom
cd /tmp/headroom/plugins/openclaw
npm install && npm run build
openclaw plugins install --link /tmp/headroom/plugins/openclaw/dist配置说明
集成后 openclaw.json 中会新增:
{
"plugins": {
"slots": { "contextEngine": "headroom" },
"entries": {
"headroom": {
"enabled": true,
"config": {
"proxyPort": 8787,
"autoStart": true,
"gatewayProviderIds": ["你的-provider-id"]
}
}
}
}
}启动 Proxy
方式一:手动启动(临时)
headroom proxy --port 8787方式二:systemd 用户服务(开机自启 + 崩溃自动恢复,推荐)
# 1. 创建 service 文件
mkdir -p ~/.config/systemd/user/
cat > ~/.config/systemd/user/headroom-proxy.service << 'SERVICEOF'
[Unit]
Description=Headroom AI Context Compression Proxy
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
ExecStart=%h/.local/bin/headroom proxy --port 8787
Restart=on-failure
RestartSec=5
[Install]
WantedBy=default.target
SERVICEOF
# 2. 启用并启动
systemctl --user daemon-reload
systemctl --user enable headroom-proxy
systemctl --user start headroom-proxy
# 3. 验证
systemctl --user status headroom-proxy
curl http://localhost:8787/livezsystemd 服务管理常用命令
# 查看状态
systemctl --user status headroom-proxy
# 查看实时日志
journalctl --user -u headroom-proxy -f -n 50
# 重启
systemctl --user restart headroom-proxy
# 停止
systemctl --user stop headroom-proxy
# 关闭开机自启
systemctl --user disable headroom-proxy
# 开机自启并自动登录(需启用 linger)
sudo loginctl enable-linger $USER注意:systemd user service 在用户未登录时不会自启。如果需要 headless 服务器场景(SSH 重启后无人登录),执行
sudo loginctl enable-linger $USER让服务在后台持续运行。
方式三:screen / tmux 保持后台运行
screen -dmS headroom headroom proxy --port 8787压缩性能数据
通用压缩场景
| 场景 | 压缩前 (tokens) | 压缩后 (tokens) | 节省率 |
|---|---|---|---|
| Code search (100 results) | 17,765 | 1,408 | 92% |
| SRE incident debugging | 65,694 | 5,118 | 92% |
| GitHub issue triage | 54,174 | 14,761 | 73% |
| Codebase exploration | 78,502 | 41,254 | 47% |
按内容类型细分
| 内容类型 | 原始 tokens | 压缩后 | 节省率 | 延迟 |
|---|---|---|---|---|
| JSON array (100 items) | 3,163 | 297 | 90.6% | 1ms |
| JSON array (500 items) | 9,526 | 1,614 | 83.1% | 2ms |
| Shell output (200 lines) | 3,238 | 469 | 85.5% | 1ms |
| Build log (200 lines) | 2,412 | 148 | 93.9% | 1ms |
| Python source (~480 lines) | 2,958 | 2,958 | 0% (pass-through) | <1ms |
| grep results (150 hits) | 2,624 | 2,624 | 0% (pass-through) | <1ms |
准确性基准
| Benchmark | Baseline | Headroom | 差异 |
|---|---|---|---|
| GSM8K (数学) | 0.870 | 0.870 | ±0.000 |
| TruthfulQA | 0.530 | 0.560 | +0.030 |
| SQuAD (QA) | 97% | 97% | ±0% |
| BFCL (Function Call) | 32% | 19% | -13%(需关注) |
本地实测数据
在本地环境使用 Headroom proxy 对真实 Api 负载的测试结果:
| 压缩策略 | 压缩次数 | 节省 tokens | 说明 |
|---|---|---|---|
| SmartCrusher (JSON) | 9 | 6,080 | 🏆 主力,JSON 智能压缩 |
| Kompress v2 (文本) | 2 | 974 | 通用文本压缩 |
| Log (日志) | 1 | 134 | 日志行压缩 |
| text / code_aware / search | 13 | — | 小数据 pass-through |
合计:25 次压缩,节省 7,188 tokens(手动测试数据)
压缩策略详解
| 策略 | 用途 | 说明 |
|---|---|---|
| SmartCrusher | JSON 数组 | 将长 JSON 列表压缩为摘要(保留关键字段),节省 80–90% 空间 |
| Kompress v2 | 通用文本 | 文本智能摘要,适合长文本段落 |
| text | 常规文本 | 基础文本压缩(短文本 pass-through) |
| code_aware | 代码 | 代码感知压缩(不做压缩,仅结构化处理) |
| search | 搜索结果 | 搜索结果摘要 |
| log | 日志 | 日志行去重与摘要 |
| tabular | 表格数据 | 结构化表格压缩 |
常用命令
# 查看 proxy 状态
headroom doctor
# 查看压缩统计数据
curl http://localhost:8787/stats | python3 -m json.tool
# 查看实时统计数据
curl http://localhost:8787/stats | python3 -c "
import json,sys
d = json.load(sys.stdin)
c = d.get('compressions_by_strategy', {})
t = d.get('tokens_saved_by_strategy', {})
total = sum(t.values())
print(f'总压缩: {sum(c.values())} 次')
print(f'总节省: {total} tokens')
"
# 查看 proxy 实时日志
journalctl --user -u headroom-proxy -n 50
# 重启 proxy
systemctl --user restart headroom-proxy
# 卸载
headroom unwrap openclaw实际使用收益(估算)
经 proxy 转发后,以 Claude Sonnet ($3/Mtok) 计算:
- 每次 code search 节省 ~16,000 tokens = $0.048
- 每次 SRE 调试节省 ~60,000 tokens = $0.18
- 日常使用可减少 40–80% 的输入 token 费用
注意:实际节省取决于工作负载中 tool output 的比例。代码密集型场景效果最好,短对话(<500 tokens)因
min_tokens_to_crush阈值设置会跳过压缩。