三层压缩,API费用减半
简介
通过三级压缩机制在调用付费API前预处理提示,实现40-60%的成本削减,保持输出质量不变。
原始描述
Pre-process prompts through 3 compression layers before sending to paid APIs. Uses a local Ollama model to intelligently compress messages and summarize hist...
通过你的 Agent 添加
点「快速安装」复制一段指令,粘给你正在用的 Agent(Claude Code、Codex、OpenClaw 等),它会自己完成安装。
或者手动安装
包里是 SKILL.md 加作者附带的文件,解压后目录名就是技能名。
技能申请的权限由作者决定,装之前请确认它能读什么、能执行什么。
中文使用说明 来自收录源
核心用法
3-Layer Token Compressor 是一款专为降低 AI API 调用成本设计的本地预处理工具。其工作原理是在用户提示词发送至付费 API 之前,通过三层递进式压缩处理显著减少 Token 数量。
第一层:格式清理 —— 自动剥离冗余空白字符、标准化换行符、移除模型不需要的 Markdown 格式标记;第二层:冗余消除 —— 智能检测并删除重复信息、冗长措辞及填充词汇;第三层:上下文裁剪 —— 针对长对话场景,自动摘要历史消息以在上下文窗口中容纳更多有效信息。
使用方式极为灵活,既可作为独立模块集成,也可作为中间件嵌入任意 AI 提供商的调用链路中。典型集成模式为:用户消息 → compressor.compress() → 压缩后消息 → 付费 API → 返回响应,全程不改变输出质量却显著降低账单金额。
显著优点
成本效益突出:官方测试数据显示,聊天对话场景可压缩 40-50%,长文档处理可达 50-60%,代码解释类内容也能实现 25-35% 的压缩率,直接转化为等比例的 API 费用节省。
安全架构优异:零第三方依赖,仅使用 Node.js 内置 http 模块;所有处理均在本地完成,通过 HTTP POST 与本地 Ollama 服务(127.0.0.1:11434)通信,绝无数据外泄风险,完全符合 GDPR/CCPA 隐私合规要求。
技术实现简洁:代码仅 448 行、4 个文件,结构清晰无危险函数(eval/exec/system)调用,便于安全审计和二次开发。MIT 开源协议保障自由使用和修改权限。
潜在缺点与局限性
本地基础设施依赖:必须预先安装并运行 Ollama 本地服务,对硬件资源有一定要求,无法直接作为纯云端 SaaS 使用。
短文本收益有限:对于少于 100 Token 的短提示,压缩率仅 5-10%,边际收益较低,更适合高频、长文本的 API 调用场景。
输入验证待增强:当前版本对特殊字符过滤、XSS 防护、prompt injection 的防御机制较为基础,需用户自行加固。
TypeScript 支持缺失:暂未提供类型定义文件,对大型 TypeScript 项目的开发体验有一定影响。
适合的目标群体
- 高频 API 调用者:日调用量超过万次的 AI 应用开发者、SaaS 运营商
- 长文本处理场景:法律文档分析、学术论文摘要、客服聊天记录处理等需大上下文窗口的业务
- 数据敏感型企业:金融、医疗、政务等领域对数据出境严格限制的组织
- 成本优化团队:希望在保证输出质量前提下系统性降低 AI 基础设施支出的技术团队
- 自托管倡导者:偏好本地部署、反对云厂商锁定的独立开发者和开源社区成员
使用风险与注意事项
功能风险:过度压缩可能导致极罕见场景下的语义细微损失,建议对关键业务场景进行 A/B 测试验证。
性能风险:本地 Ollama 推理引入额外延迟(通常 50-200ms),对延迟极度敏感的实时应用需评估吞吐影响。
运维风险:Ollama 服务的稳定性、版本兼容性、本地端口冲突等问题需要纳入运维监控体系。
安全建议:尽管代码本身无外部通信,仍需定期审查 Ollama 配置防止本地服务暴露;建议实施输入长度限制(如 10,000 字符)和请求超时机制。
技能正文
name: "3-Layer Token Compressor — Cut AI API Costs 40-60%" description: "Pre-process prompts through 3 compression layers before sending to paid APIs. Same quality, fewer tokens, lower bills." author: "@TheShadowRose" version: "1.0.0" tags: ["token-optimization", "cost-reduction", "compression", "api-costs", "prompt-optimization", "budget"] license: "MIT"
3-Layer Token Compressor — Cut AI API Costs 40-60%
Pre-process prompts through 3 compression layers before sending to paid APIs. Same quality, fewer tokens, lower bills.
Reduce API costs by compressing prompts before they hit paid models.
The 3 Layers
- Whitespace & Formatting — Strips unnecessary whitespace, normalizes line breaks, removes markdown formatting that models don't need
- Redundancy Removal — Detects and removes repeated information, verbose phrasing, filler words
- Context Pruning — For long conversations, summarizes older messages to fit more in the context window
Usage
const { TokenCompressor } = require('./src/token-compressor');
const compressor = new TokenCompressor();
const original = "Your very long prompt with lots of redundant information...";
const compressed = compressor.compress(original);
console.log(`Original: ${original.length} chars`);
console.log(`Compressed: ${compressed.length} chars`);
console.log(`Saved: ${Math.round((1 - compressed.length/original.length) * 100)}%`);
Typical Savings
| Content Type | Compression | Quality Impact |
|---|---|---|
| Chat conversations | 40-50% | None |
| Code + explanation | 25-35% | None |
| Long documents | 50-60% | Minimal |
| Short prompts (<100 tokens) | 5-10% | None |
Integration
Works as middleware — drop it between your app and any AI provider:
// Before sending to API
const compressed = compressor.compress(userMessage);
const response = await api.chat(compressed);
// Response quality is the same, but you paid for fewer tokens
⚠️ Disclaimer
This software is provided "AS IS", without warranty of any kind, express or implied.
USE AT YOUR OWN RISK.
- The author(s) are NOT liable for any damages, losses, or consequences arising from the use or misuse of this software — including but not limited to financial loss, data loss, security breaches, business interruption, or any indirect/consequential damages.
- This software does NOT constitute financial, legal, trading, or professional advice.
- Users are solely responsible for evaluating whether this software is suitable for their use case, environment, and risk tolerance.
- No guarantee is made regarding accuracy, reliability, completeness, or fitness for any particular purpose.
- The author(s) are not responsible for how third parties use, modify, or distribute this software after purchase.
By downloading, installing, or using this software, you acknowledge that you have read this disclaimer and agree to use the software entirely at your own risk.
DATA DISCLAIMER: This software processes and stores data locally on your system. The author(s) are not responsible for data loss, corruption, or unauthorized access resulting from software bugs, system failures, or user error. Always maintain independent backups of important data. This software does not transmit data externally unless explicitly configured by the user.
Support & Links
| 🐛 Bug Reports | TheShadowyRose@proton.me |
| ☕ Ko-fi | ko-fi.com/theshadowrose |
| 🛒 Gumroad | shadowyrose.gumroad.com |
| @TheShadowyRose | |
| 🐙 GitHub | github.com/TheShadowRose |
| 🧠 PromptBase | promptbase.com/profile/shadowrose |
Built with OpenClaw — thank you for making this possible.
🛠️ Need something custom? Custom OpenClaw agents & skills starting at $500. If you can describe it, I can build it. → Hire me on Fiverr
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。