360 Secure SkillHub
360 安全技能中心

Headroom Token Compression

该 Skill 通过 Headroom 在本地压缩发送给大模型的工具输出、日志、RAG 片段和重复 JSON,提供 CLI、代理、Python 库和 MCP 服务等用法,并可选压缩模型回复;文档称可节省 20%-95% token,但会缓存原始内容到本地磁盘,且 wrap 会修改代理启动配置,需防范敏感数据风险。

johnsonbuilds
johnsonbuilds
热度 16.55
最近更新 2026-09-11 18:12:57
分类:效率依赖:代码执行 / 读写文件应用场景:数据处理 / 工作流

安装技能

复制下面的话术发给你的 Agent,它会通过 secure-skills 检索并安装该技能。

执行命令安装此 skill: npx secure-skills-cli add token-compression
Metadata
name
headroom-token-compression
description
Compress AI Agent tool outputs, logs, and RAG content using Headroom — reduce token usage by 20-95%
version
1.1.0
author
weekly-skill-discovery
license
MIT
platforms
["linux","macos"]
metadata
{"hermes":{"tags":["token-compression","cost-reduction","mcp","agent-optimization"]}}

Headroom Token Compression

Compress content sent to LLMs (tool outputs, logs, RAG chunks, files) while maintaining answer quality — reduces token consumption by 20-95%.

🚨 Privacy & Data Security Warning (Read Before Use)

Headroom caches raw uncompressed content locally (for later retrieval). This means:

  • Your prompts, tool outputs, logs, etc. will be cached on your local machine
  • Data is NOT sent to external services (all processing is local)
  • But cached files are stored on local disk — ensure your device is secure

Data that should NOT be processed:

  • Content containing API keys, passwords, or private tokens
  • Personally identifiable information (PII)
  • Internal company code or proprietary business information
  • Any content you don't want stored on local disk

If processing sensitive data, ensure: ① device has encrypted storage; ② regularly clean headroom cache directory (~/.headroom/cache).


Trigger Conditions

Only use this skill in the following explicit scenarios:

  • You actively request compression of specific content (e.g., "compress these logs", "reduce tokens in this code search")
  • You are dealing with obvious large redundant data (e.g., 100+ search results, extremely long logs, duplicate JSON)
  • You have confirmed the content does NOT contain sensitive information (see privacy warning above)
  • You are experiencing high token costs in Claude Code, Codex, Cursor, etc.

NOT suitable for:

  • Routine conversations or general Q&A
  • Processing logs or code containing sensitive data
  • Ordinary tasks without explicit compression needs

Source


Installation

# Using uv (recommended)
uv tool install --python 3.13 "headroom-ai[all]"

# Or using pip
pip install "headroom-ai[all]"

Usage

1. One-Click Deploy (Simplest)

headroom deploy

Automatically configures local deployment and agent integration.

2. Wrap Common Agents

# Wrap Claude Code
headroom wrap claude

# Wrap Codex
headroom wrap codex

# Wrap OpenCode
headroom wrap opencode

Use headroom unwrap <tool> to remove wrapping.

Note: Wrap operations modify agent startup configuration. Make sure you know what you're doing.

3. Proxy Mode (Zero Code Changes)

headroom proxy --port 8787

Any OpenAI-compatible client can use this proxy by setting API base URL.

4. Python Library (Inline Use)

from headroom import compress
from openai import OpenAI

messages = [{"role": "user", "content": "Analyze these results"}]
result = compress(messages, model="gpt-4o")

client = OpenAI()
response = client.chat.completions.create(model="gpt-4o", messages=result.messages)
print(f"Saved {result.tokens_saved} tokens ({result.compression_ratio:.0%})")

5. MCP Server Mode

headroom mcp install

Provides headroom_compress, headroom_retrieve, headroom_stats tools to any MCP client.


Verify Installation

# Health check
headroom doctor

# View savings stats
headroom perf

# Real-time dashboard (requires running proxy)
headroom dashboard

Compression Results

Scenario Before After Savings
Code search (100 results) 17,199 tokens 13,597 tokens 21%
SRE incident debugging 55,957 tokens 24,340 tokens 57%
Codebase exploration 58,801 tokens 33,895 tokens 42%
GitHub Issue classification 46,067 tokens 32,429 tokens 30%

JSON duplicate data can save 90%+. Plain text content compresses less.


Output Compression (Reduce Model Response Tokens)

# Enable output compression
export HEADROOM_OUTPUT_SHAPER=1
headroom proxy --port 8787

# Auto-learn your verbosity preferences
headroom learn --verbosity
headroom learn --verbosity --apply

Supported Agents

Agent Support
Claude Code ✅
Codex ✅
OpenCode ✅
Cursor Manual config
Aider ✅
Copilot CLI ✅
Cline ✅
Continue ✅
Goose ✅
OpenHands ✅

Pitfalls

  • Local caching: Raw content cached on local disk, models can retrieve full content via headroom_retrieve. To clear cache, delete ~/.headroom/cache directory.
  • Proxy mode: When using proxy mode, configure client to use http://localhost:8787 as API base.
  • Output shaping is optional: Disabled by default, must be enabled manually.
  • Sensitive data risk: As mentioned, raw data is cached locally. Ensure device security before processing sensitive content.

References