ClawSaver
Reduce model API costs by 20–40% through intelligent message batching and buffering.
Most agent systems waste money on redundant API calls. When users send follow-up messages, you call the model separately for each one. ClawSaver fixes this by waiting ~800ms to collect related messages, then sending them together in a single optimized request. Same response quality. Lower cost. No user friction.
How It Works: Batching & Buffering
CODEBLOCK0
Why it matters: Context (system prompts, history, instructions) gets re-sent on every API call. With ClawSaver, you pay that context overhead once per batch instead of three times. This compounds the savings beyond just "fewer calls."
Example (4K token context, 200 output tokens):
- - Without ClawSaver: 3 calls × 4,200 tokens = 12,600 tokens
- With ClawSaver: 1 call × 4,600 tokens = 4,600 tokens
- Actual savings: 63% token reduction (even better than call reduction)
The Problem
CODEBLOCK1
Without optimization: 3 API calls = 3x cost
With ClawSaver: 1 batched call = 1/3 the price
Across thousands of conversations, this compounds fast.
How It Works
- 1. User sends message → ClawSaver buffers it
- Waits ~800ms for follow-ups from same user
- If more messages arrive → keep buffering
- Timer expires → send all messages together
- Model responds once → you get complete answer
Why users don't notice: They're already waiting for your model response. Buffering input doesn't feel slower because the response comes right after the batch sends.
Install
CODEBLOCK2
Quick Start (10 lines)
CODEBLOCK3
Impact
| Metric | Value |
|---|
| Cost reduction | 20–40% typical |
| Setup time |
10 minutes |
|
Code added | ~10 lines |
|
Dependencies | 0 |
|
File size | 4.2 KB |
|
Latency added | +800ms (user-imperceptible) |
|
Maintenance | None |
Three Profiles
Choose based on your use case:
Balanced (Default)
- - 25–35% savings
- 800ms buffer
- Chat, Q&A, general conversation
Aggressive
- - 35–45% savings
- 1.5s buffer
- Batch workflows, high-volume ingestion
Real-Time
- - 5–10% savings
- 200ms buffer
- Interactive, voice-first systems
When to Use
✅ Chat applications
✅ Customer support bots
✅ Multi-turn Q&A
✅ Any conversation with follow-ups
❌ Single-request workflows
❌ Sub-100ms response requirements
API
CODEBLOCK4
Docs
- - START_HERE.md — Navigation (pick your role/timeline)
- AUTO-INTEGRATION.md — ⭐ Drop-in middleware wrapper (2 min setup)
- QUICKSTART.md — 5-minute integration
- INTEGRATION.md — Patterns, edge cases, full config
- SUMMARY.md — Metrics and ROI (decision makers)
- SKILL.md — Full API reference
- example-integration.js — Copy-paste templates
Security
- - No telemetry — Doesn't phone home
- No network calls — Runs locally
- No dependencies — Pure JavaScript
- You control output — You decide what goes to your model
Data never leaves your machine.
License
MIT
Start here: Pick your path in
START_HERE.md, or jump to
QUICKSTART.md for 5-minute setup.
ClawSaver
通过智能消息批处理和缓冲,将模型API成本降低20-40%。
大多数智能体系统在冗余的API调用上浪费资金。当用户发送后续消息时,你会为每条消息单独调用模型。ClawSaver通过等待约800毫秒收集相关消息,然后将它们合并到一个优化请求中一并发送来解决这个问题。相同的响应质量。更低的成本。无用户感知延迟。
工作原理:批处理与缓冲
不使用 CLAWSAVER(上下文开销隐藏):
用户:什么是机器学习?
模型:→ API 调用 #1 [上下文:系统提示、聊天历史](成本:$X)
返回:定义
用户:举个例子
模型:→ API 调用 #2 [上下文:系统提示、聊天历史、问题1、回答1](成本:$X)
返回:示例
用户:应用到金融领域?
模型:→ API 调用 #3 [上下文:系统提示、聊天历史、问题1–回答2](成本:$X)
返回:金融应用
总计:3次调用 × 完整上下文 = 3X成本,每次调用重复上下文开销
───────────────────────────────────────
使用 CLAWSAVER(单次上下文加载):
用户:什么是机器学习? ← 缓冲(等待800毫秒)
用户:举个例子 ← 缓冲(等待800毫秒)
用户:应用到金融领域? ← 刷新:一次性发送全部3条
模型:→ API 调用 #1 [上下文加载一次:系统提示、聊天历史]
一次性处理所有3个问题
返回:涵盖所有三个问题的综合回答
总计:1次调用 × 完整上下文 = 1X成本,上下文开销只支付一次
实际节省(含上下文):减少67%
每Token成本:1/3(更少的上下文重新加载 + 合并)
为什么重要: 上下文(系统提示、历史记录、指令)在每次API调用时都会被重新发送。使用ClawSaver,你每批次只支付一次上下文开销,而不是三次。这使得节省效果远超单纯的减少调用次数。
示例(4K Token上下文,200个输出Token):
- - 不使用ClawSaver:3次调用 × 4,200 Token = 12,600 Token
- 使用ClawSaver:1次调用 × 4,600 Token = 4,600 Token
- 实际节省:减少63% Token(甚至优于调用次数减少的效果)
问题所在
用户:什么是机器学习?
(停顿)
用户:举个例子
(停顿)
用户:这如何应用于医疗保健?
未优化时:3次API调用 = 3倍成本
使用ClawSaver:1次批处理调用 = 1/3的价格
在成千上万的对话中,这种节省效果会迅速累积。
工作原理
- 1. 用户发送消息 → ClawSaver将其缓冲
- 等待约800毫秒,查看同一用户是否有后续消息
- 如果有更多消息到达 → 继续缓冲
- 计时器到期 → 一次性发送所有消息
- 模型响应一次 → 你获得完整回答
为什么用户察觉不到: 他们本来就在等待你的模型响应。缓冲输入不会感觉更慢,因为响应在批次发送后立即返回。
安装
bash
clawhub install clawsaver
快速入门(10行代码)
javascript
import SessionDebouncer from clawsaver;
const debouncers = new Map();
function handleMessage(userId, text) {
if (!debouncers.has(userId)) {
debouncers.set(userId, new SessionDebouncer(
userId,
(msgs) => callModel(userId, msgs)
));
}
debouncers.get(userId).enqueue({ text });
}
影响
10分钟 |
|
添加的代码 | 约10行 |
|
依赖项 | 0 |
|
文件大小 | 4.2 KB |
|
增加的延迟 | +800毫秒(用户无感知) |
|
维护成本 | 无 |
三种配置模式
根据你的使用场景选择:
平衡模式(默认)
- - 节省25-35%
- 800毫秒缓冲
- 聊天、问答、通用对话
激进模式
- - 节省35-45%
- 1.5秒缓冲
- 批量工作流、高吞吐量数据摄入
实时模式
- - 节省5-10%
- 200毫秒缓冲
- 交互式、语音优先系统
适用场景
✅ 聊天应用
✅ 客服机器人
✅ 多轮问答
✅ 任何包含后续问题的对话
❌ 单次请求工作流
❌ 要求响应时间低于100毫秒的场景
API
javascript
new SessionDebouncer(userId, handler, {
debounceMs: 800, // 等待时间
maxWaitMs: 3000, // 绝对最大等待时间
maxMessages: 5, // 批次大小上限
maxTokens: 2048 // 预留Token数
})
// 方法
debouncer.enqueue(message) // 添加到批次
debouncer.forceFlush(reason) // 立即发送
debouncer.getState() // 缓冲状态 + 指标
debouncer.getStatusString() // 人类可读状态
文档
- - START_HERE.md — 导航(选择你的角色/时间线)
- AUTO-INTEGRATION.md — ⭐ 即插即用中间件封装(2分钟设置)
- QUICKSTART.md — 5分钟集成
- INTEGRATION.md — 模式、边界情况、完整配置
- SUMMARY.md — 指标和投资回报率(决策者参考)
- SKILL.md — 完整API参考
- example-integration.js — 可直接复制粘贴的模板
安全性
- - 无遥测 — 不会回传数据
- 无网络调用 — 本地运行
- 无依赖项 — 纯JavaScript
- 你控制输出 — 你决定发送给模型的内容
数据永远不会离开你的机器。
许可证
MIT
从这里开始: 在
START_HERE.md 中选择你的路径,或直接跳转到
QUICKSTART.md 进行5分钟设置。