你的AI Agent 每个月烧掉500美元token费用,其中90%是冗余上下文——Lowfat 用CLI过滤器+可插拔规则引擎,在代码不改一行的情况下把token消耗砍到原来的1/10。本文给出从安装到生产环境的完整配置。
问题:你的LLM费用有90%是浪费的
2026年6月,AI创业者的工具箱里至少跑着3-5个Agent:Claude Code写代码、Hermes Agent做自动化、OpenClaw做多Agent编排。每个Agent每天发出数百次LLM调用,每次调用都在消耗token——而这些token中,真正有用的可能不到10%。
典型浪费场景:
你给 Claude Code 一个任务:"修复 src/auth.py 里的登录bug"
Claude Code 读取了整个项目上下文 → 发送 15,000 tokens 给 API
其中真正相关的代码只有 200 行 → 约 1,500 tokens
浪费率 = 90%
每一个冗余的 context window 字节都在烧钱。如果你用的是 Claude Opus($15/M input tokens),一个月轻松烧掉$500-2000,而其中$450-1800是垃圾上下文。
Lowfat 的出现改变了这一切。
Lowfat 是什么
Lowfat(GitHub: zdk/lowfat,HN 154 points / 77 comments,6月5日发布)是一个可插拔的CLI过滤器,可以插入任何LLM调用链中,自动过滤掉不必要的token消耗。
核心架构:
你的应用 → Lowfat CLI → 过滤后的prompt → LLM API
↑
规则引擎(可配置)
- 去重冗余文本
- 压缩重复结构
- 移除无关上下文
- 智能摘要长文本
它不修改你的应用代码,不依赖特定的LLM provider,通过管道模式(pipe mode)工作在标准输入输出之间。
快速上手:5分钟配置
1. 安装
# 从源码安装
git clone https://github.com/zdk/lowfat.git
cd lowfat
pip install -e .
# 或者用 pip
pip install lowfat-cli
2. 基础用法
# 管道模式:过滤后再发送给 LLM
cat my_large_prompt.txt | lowfat filter | llm
# 直接过滤文件
lowfat filter --input prompt.txt --output filtered.txt
# 查看节省统计
cat prompt.txt | lowfat filter --stats
# 输出: Input: 15,234 tokens → Output: 1,247 tokens (91.8% saved)
3. 与 Claude Code 集成
Claude Code 调用 LLM 时,可以在 shell wrapper 中插入 Lowfat:
# ~/.claude/wrapper.sh
#!/bin/bash
# 在 Claude Code 调用 API 之前,先过滤上下文
INPUT=$(cat)
FILTERED=$(echo "$INPUT" | lowfat filter --preserve-imports --preserve-types)
echo "$FILTERED"
配置 Claude Code 使用此 wrapper:
// ~/.claude/config.json
{
"api": {
"preProcessHook": "~/lowfat/wrapper.sh"
}
}
4. 与 Hermes Agent 集成
Hermes Agent 的 conversation memory 特别容易膨胀。在 prompt 进入 LLM 之前插入 Lowfat:
# hermes_lowfat_middleware.py
import subprocess
def filter_prompt(prompt_text: str) -> str:
"""在 Hermes Agent 发送 prompt 前调用"""
result = subprocess.run(
["lowfat", "filter", "--mode", "agent"],
input=prompt_text,
capture_output=True,
text=True
)
return result.stdout
# 在 Hermes 配置中注册中间件
# ~/.hermes/config.yaml
middleware:
pre_llm:
- module: hermes_lowfat_middleware
function: filter_prompt
规则引擎详解
Lowfat 的节省效果取决于过滤器规则的配置。以下是针对 AI Agent 场景优化的规则模板:
规则1:去重历史对话(节省 40-60%)
Agent 的 conversation history 经常包含大量重复内容——同一个文件内容在不同轮次中反复出现。
# ~/.lowfat/rules/dedup.yaml
rules:
- name: dedup-history
type: dedup
target: conversation_history
strategy: semantic_hash # 语义哈希去重,不是简单的字符串匹配
threshold: 0.85 # 相似度 > 85% 视为重复
keep: first # 保留第一次出现,删除后续重复
规则2:压缩代码上下文(节省 20-30%)
Claude Code 在修改文件时,经常把整个文件内容塞进 context。大部分情况下只需要相关的函数/类。
# ~/.lowfat/rules/code-context.yaml
rules:
- name: compress-code
type: summarize
target: code_blocks
strategy: function_boundary # 按函数边界保留
max_tokens_per_file: 2000
preserve:
- imports
- type_definitions
- function_signatures
规则3:移除无关系统消息(节省 5-10%)
很多 Agent 的系统提示词(system prompt)长达数千字,其中大量是"最佳实践"、"注意事项"等通用内容。
# ~/.lowfat/rules/system-prompt.yaml
rules:
- name: trim-system-prompt
type: trim
target: system_messages
strategy: keep_core_directives
core_keywords:
- "you must"
- "never"
- "always"
- "format"
remove_patterns:
- "you are a helpful"
- "remember to be"
- "it is important to note"
实战:三种Agent场景的配置模板
场景1:Claude Code 日常编程
# lowfat-claude-code.yaml
# 适用于:日常代码编写、bug修复、代码审查
pipeline:
- rule: dedup-code-context # 去重重复的代码片段
- rule: compress-large-files # 超过2000行的文件只保留相关部分
- rule: trim-system-prompt # 精简Claude Code的系统提示
- rule: remove-stdout-noise # 去掉编译输出/日志中的无意义行
stats:
typical_savings: "75-85%"
best_for: "代码修改、重构、测试生成"
场景2:OpenClaw 多Agent编排
# lowfat-openclaw.yaml
# 适用于:多Agent任务分发、结果聚合
pipeline:
- rule: dedup-subagent-outputs # 去重多个子Agent返回的重复结果
- rule: summarize-agent-logs # 将冗长的Agent日志压缩为关键摘要
- rule: merge-parallel-results # 合并并行任务的相似输出
- rule: drop-empty-responses # 删除空响应和no-op结果
stats:
typical_savings: "80-92%"
best_for: "多Agent并行任务、数据聚合、网页抓取"
场景3:Hermes Agent 长对话
# lowfat-hermes.yaml
# 适用于:长时间运行的Agent会话,记忆管理
pipeline:
- rule: dedup-history # 去重重复的对话轮次
- rule: compress-old-messages # 将10轮前的对话压缩为摘要
- rule: remove-tool-noise # 过滤工具调用中的冗余输出
- rule: keep-recent-full # 保留最近3轮的完整上下文
stats:
typical_savings: "65-75%"
best_for: "长对话、复杂任务、记忆管理"
性能数据
基于 HN 讨论和社区反馈(154 points / 77 comments 的验证):
| Agent类型 | 优化前 token/天 | 优化后 token/天 | 节省率 | 月省成本 |
|---|---|---|---|---|
| Claude Code 重度使用 | 500K | 75K | 85% | ~$190 |
| OpenClaw 多Agent | 1.2M | 96K | 92% | ~$500 |
| Hermes Agent 长对话 | 300K | 75K | 75% | ~$100 |
| GPT-5 编码助手 | 800K | 160K | 80% | ~$280 |
数据来源:Lowfat GitHub README + HN社区实测反馈
成本按 Claude Opus $15/M input tokens 计算
进阶技巧
技巧1:按任务类型动态切换规则
不是所有任务都需要同样的过滤策略。代码生成任务需要更多上下文,简单的问答可以激进过滤。
#!/bin/bash
# lowfat-smart.sh — 根据任务类型选择规则
TASK_TYPE=$1
INPUT=$(cat)
case $TASK_TYPE in
code)
echo "$INPUT" | lowfat filter --rules claude-code
;;
chat)
echo "$INPUT" | lowfat filter --rules aggressive # 激进过滤
;;
analysis)
echo "$INPUT" | lowfat filter --rules preserve-data # 保留数据
;;
*)
echo "$INPUT" | lowfat filter --rules default
;;
esac
技巧2:白名单机制——保护关键信息
有些信息绝对不能过滤掉(API密钥、关键配置、错误堆栈)。Lowfat 支持白名单:
# ~/.lowfat/rules/whitelist.yaml
rules:
- name: preserve-critical
type: whitelist
patterns:
- "API_KEY|SECRET|TOKEN" # 保护凭证信息
- "Error:|Exception:|Traceback" # 保护错误信息
- "FIXME|TODO|HACK" # 保护代码标记
- "def test_|def main" # 保护测试和入口函数
技巧3:监控和调优
在生产环境中持续监控节省率,逐步优化规则:
# lowfat_monitor.py — 记录每次过滤的统计
import subprocess, json, time
def filter_with_stats(prompt):
start = time.time()
result = subprocess.run(
["lowfat", "filter", "--json-stats"],
input=prompt, capture_output=True, text=True
)
stats = json.loads(result.stdout)
stats["latency_ms"] = (time.time() - start) * 1000
# 记录到监控系统
print(f"[Lowfat] Saved {stats['saved_pct']:.1f}% "
f"({stats['input_tokens']}→{stats['output_tokens']} tokens, "
f"{stats['latency_ms']:.0f}ms)")
return stats["filtered_text"]
常见问题
Q: Lowfat 会影响回答质量吗?
A: 取决于规则配置的激进程度。默认规则保守,主要去重和压缩,不影响语义。激进规则会移除大量上下文,适合简单任务但不推荐用于复杂代码生成。建议从保守规则开始,逐步调优。
Q: 和 prompt caching 有什么区别?
A: Prompt caching(如 Anthropic 的 cache_control)是在服务端缓存重复的 prompt 前缀,不能减少首次发送的 token 量。Lowfat 是在客户端减少实际需要的 token,两者互补——先用 Lowfat 减少总量,再用 caching 加速重复部分。
Q: 开源还是商业?
A: Lowfat 是 MIT 开源协议,可以自由修改和商用。核心过滤引擎用 Rust 编写(性能高),规则引擎用 YAML 配置。社区已经贡献了多个针对不同 LLM 的规则包。
Q: 延迟会增加多少?
A: Lowfat 的过滤延迟通常在 50-200ms(取决于输入大小)。对于 LLM 调用通常 2-30 秒的响应时间来说,这个延迟可以忽略不计。而且因为发送的 token 更少,API 响应时间也会缩短,总体延迟可能反而下降。
行动建议
- 立即安装:
pip install lowfat-cli,先对历史对话做一次离线分析,看看你的浪费率是多少 - 渐进采纳:从去重规则开始(最安全),观察一周后确认不影响质量再加入压缩规则
- 按Agent配置:不同Agent用不同规则——写代码的 Agent 保守,做搜索的 Agent 激进
- 监控ROI:对比部署 Lowfat 前后的月度 LLM 账单,确认实际节省金额
