Tamp for OpenClaw
Save 3-50% on input tokens by routing API requests through Tamp — a local HTTP proxy that compresses tool_result blocks before they reach Anthropic.
Prerequisites
- - Node.js 18+
- Anthropic API key — Tamp proxies requests to Anthropic. Your
ANTHROPIC_API_KEY must be set in your OpenClaw config. Tamp itself does not store or read this key — it forwards the x-api-key header from incoming requests unchanged.
1. Install & Run
CODEBLOCK0
Start with default stages:
CODEBLOCK1
Verify:
CODEBLOCK2
Tamp is open source (MIT). Audit the source, build from git, or run from a local clone: INLINECODE3
2. Run as systemd service
Create ~/.config/systemd/user/tamp.service:
CODEBLOCK3
CODEBLOCK4
3. Configure OpenClaw
Add a provider in your OpenClaw config:
CODEBLOCK5
Set as primary model:
CODEBLOCK6
Restart the gateway. All requests now flow through Tamp.
How it works
CODEBLOCK7
Tamp intercepts the request body, finds tool_result blocks in messages[], and compresses their content. Headers (including x-api-key) are forwarded unchanged. The response streams back untouched.
7 Compression Stages
| Stage | What it does | Lossy? |
|---|
| minify | Strip JSON whitespace | No |
| toon |
Columnar encoding for arrays (file listings, deps, routes) | No |
| strip-lines | Remove line-number prefixes from Read tool output | No |
| whitespace | Collapse blank lines, trim trailing spaces | No |
| dedup | Deduplicate identical tool_results across turns | No |
| diff | Delta-encode similar re-reads as unified diffs | No |
| prune | Strip lockfile hashes, registry URLs, npm metadata | Metadata only* |
\* Prune removes fields like integrity, resolved, shasum, _id, _from, _nodeVersion from JSON — npm registry metadata not needed by the LLM. To keep full provenance, remove prune from TAMP_STAGES.
What to expect
| Scenario | Savings |
|---|
| Chat sessions (short turns) | 3-5% |
| Coding sessions (file reads, JSON) |
30-50% |
| Lockfiles | up to 81% |
| Subagent tasks | 20-40% |
Security Notes
- - API key handling: Tamp forwards the
x-api-key / Authorization header from the incoming request to upstream. It does not store, log, or read API keys. - Local only: Tamp binds to localhost by default. It does not accept external connections unless you change the bind address.
- No telemetry: Tamp does not phone home, collect analytics, or make any outbound connections except to the configured upstream API.
- Fallback: Add Anthropic direct as a fallback model in OpenClaw. If Tamp is down, requests bypass it automatically.
Resources
OpenClaw 的 Tamp
通过 Tamp 路由 API 请求,在 toolresult 块到达 Anthropic 之前对其进行压缩,可节省 3-50% 的输入令牌。
前置条件
- - Node.js 18+
- Anthropic API 密钥 — Tamp 代理请求到 Anthropic。您的 ANTHROPICAPIKEY 必须在 OpenClaw 配置中设置。Tamp 本身不存储或读取此密钥 — 它会原封不动地转发传入请求中的 x-api-key 标头。
1. 安装与运行
bash
安装固定版本(推荐)
npm i -g @sliday/tamp@0.3.8
或无需安装直接运行
npx @sliday/tamp@0.3.8 -y
使用默认阶段启动:
bash
TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune tamp -y
验证:
bash
curl http://localhost:7778/health
{status:ok,version:0.3.8,stages:[minify,toon,...]}
Tamp 是开源 (MIT) 的。审计源代码、从 git 构建,或从本地克隆运行:git clone https://github.com/sliday/tamp && cd tamp && npm install && node bin/tamp.js -y
2. 作为 systemd 服务运行
创建 ~/.config/systemd/user/tamp.service:
ini
[Unit]
Description=Tamp 令牌压缩代理
After=network.target
[Service]
调整路径:node -e console.log(require(child_process).execFileSync(which, [tamp]).toString().trim())
ExecStart=/usr/local/bin/tamp
Restart=always
RestartSec=5
Environment=TAMP_PORT=7778
Environment=TAMP_STAGES=minify,toon,strip-lines,whitespace,dedup,diff,prune
Environment=TAMP_LOG=true
[Install]
WantedBy=default.target
bash
systemctl --user daemon-reload
systemctl --user enable --now tamp.service
journalctl --user -u tamp -f # 实时压缩日志
3. 配置 OpenClaw
在 OpenClaw 配置中添加一个提供商:
json5
{
models: {
providers: {
anthropic-tamp: {
baseUrl: http://localhost:7778,
apiKey: ${ANTHROPICAPIKEY}, // 转发到上游,Tamp 不存储
api: anthropic-messages,
models: [
{ id: claude-opus-4-6, name: Claude Opus 4.6 (压缩) },
{ id: claude-sonnet-4-6, name: Claude Sonnet 4.6 (压缩) }
]
}
}
}
}
设置为主要模型:
json5
{
agents: {
defaults: {
model: { primary: anthropic-tamp/claude-opus-4-6 }
}
}
}
重启网关。所有请求现在都通过 Tamp 流转。
工作原理
OpenClaw → POST /v1/messages → Tamp (localhost:7778) → 压缩 JSON 主体 → Anthropic API
← 原样流式返回响应
Tamp 拦截请求主体,在 messages[] 中找到 tool_result 块,并压缩其内容。标头(包括 x-api-key)原封不动地转发。响应原样流式返回。
7 个压缩阶段
| 阶段 | 功能 | 有损? |
|---|
| minify | 去除 JSON 空白 | 否 |
| toon |
数组的列式编码(文件列表、依赖项、路由) | 否 |
| strip-lines | 移除 Read 工具输出的行号前缀 | 否 |
| whitespace | 压缩空行,修剪尾部空格 | 否 |
| dedup | 跨轮次去重相同的 tool_results | 否 |
| diff | 将相似的重读内容编码为统一差异格式 | 否 |
| prune | 移除锁定文件哈希、注册表 URL、npm 元数据 | 仅元数据* |
\* Prune 从 JSON 中移除 integrity、resolved、shasum、id、from、nodeVersion 等字段 — LLM 不需要的 npm 注册表元数据。如需保留完整来源信息,请从 TAMPSTAGES 中移除 prune。
预期效果
| 场景 | 节省量 |
|---|
| 聊天会话(短轮次) | 3-5% |
| 编码会话(文件读取、JSON) |
30-50% |
| 锁定文件 | 最高 81% |
| 子代理任务 | 20-40% |
安全说明
- - API 密钥处理: Tamp 将传入请求中的 x-api-key / Authorization 标头转发到上游。它不存储、记录或读取 API 密钥。
- 仅本地: Tamp 默认绑定到 localhost。除非您更改绑定地址,否则不接受外部连接。
- 无遥测: Tamp 不会回传数据、收集分析信息,也不会进行任何出站连接,除非连接到配置的上游 API。
- 回退: 在 OpenClaw 中将 Anthropic 直接添加为回退模型。如果 Tamp 宕机,请求会自动绕过它。
资源占用