概述

Headroom (headroomlabs-ai/headroom) 是一个 AI agent 上下文压缩层(Context Compression Layer),能自动压缩 Agent 读取的 tool output、日志、文件等内容,在不影响准确性的前提下节省 60–95% 的输入 tokens。

本项目已成功集成到 OpenClaw 中,作为 ContextEngine 插件运行。

相关说明

[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/livez

systemd 服务管理常用命令

# 查看状态
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,7651,40892%
SRE incident debugging65,6945,11892%
GitHub issue triage54,17414,76173%
Codebase exploration78,50241,25447%

按内容类型细分

内容类型原始 tokens压缩后节省率延迟
JSON array (100 items)3,16329790.6%1ms
JSON array (500 items)9,5261,61483.1%2ms
Shell output (200 lines)3,23846985.5%1ms
Build log (200 lines)2,41214893.9%1ms
Python source (~480 lines)2,9582,9580% (pass-through)<1ms
grep results (150 hits)2,6242,6240% (pass-through)<1ms

准确性基准

BenchmarkBaselineHeadroom差异
GSM8K (数学)0.8700.870±0.000
TruthfulQA0.5300.560+0.030
SQuAD (QA)97%97%±0%
BFCL (Function Call)32%19%-13%(需关注)

本地实测数据

在本地环境使用 Headroom proxy 对真实 Api 负载的测试结果:

压缩策略压缩次数节省 tokens说明
SmartCrusher (JSON)96,080🏆 主力,JSON 智能压缩
Kompress v2 (文本)2974通用文本压缩
Log (日志)1134日志行压缩
text / code_aware / search13小数据 pass-through

合计:25 次压缩,节省 7,188 tokens(手动测试数据)

压缩策略详解

策略用途说明
SmartCrusherJSON 数组将长 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 阈值设置会跳过压缩。

参考资料