AIFeed

一款开源扫描器,可找出代码中被浪费的 LLM token

Translation(model translation, not the source's own text)

仓库文件导航

题图
题图

llm-cost-audit

一个 AI agent skill,用于审计你代码中的 LLM API 调用是否存在 token 浪费,并以美元为单位告诉你每项修复能省下多少钱。

把 Claude(或任何支持 Agent Skills 的 agent)指向一个项目,然后问“为什么我们的 Claude 账单这么高?”或“审计我们的 LLM 成本”。该 skill 会:

  • 扫描代码库中的 Anthropic、OpenAI、Gemini、LiteLLM 和 Vercel AI SDK 调用,以及通过 LangChain、Pydantic AI、LlamaIndex、CrewAI、DSPy、AutoGen、Haystack、Semantic Kernel、OpenAI Agents SDK、smolagents 和 Mastra 发起的调用。
  • 标记可能的浪费:未使用缓存、prompt 中破坏缓存的 timestamp、Gemini 4 拒绝的显式缓存、无上限的聊天历史、在简单任务上使用前沿模型、缺少输出限制、本可改为批处理任务的逐项循环。
  • 根据真实的每 token 价格估算每项修复的月度节省额。
  • 按节省金额排序写出报告,附上修改前后的代码,并在你要求时应用这些修复。

它涵盖了 How to cut your LLM API bill 中的六种技术:缓存、路由到更小的模型、更短的输出、批处理 API、裁剪上下文,以及你从哪里购买 token。

安装

Claude Code:把 llm-cost-audit 文件夹复制到 ~/.claude/skills/(用于你所有项目)或某个项目内的 .claude/skills/。

Claude 应用:从 Releases 下载 llm-cost-audit.skill,并在 Claude 设置的 Skills 下上传。

其他 agent:任何能读取 SKILL.md skills 的 agent 都可以直接使用该文件夹。脚本需要 Python 3.8+,无需安装任何包。

示例

在一个带有编码 agent 和工单分类器的小项目上运行:


        $ python scripts/scan.py ./my-app
2 LLM call sites:

- src/agent.py:8  [anthropic messages]  model=claude-sonnet-5-5
    · no explicit caching in this file
    · file builds a system prompt and also uses time/uuid/random values
    · conversation list is appended to
    · expensive model; check whether every request needs it

- src/tagger.ts:6  [openai chat completions]  model=gpt-6-2-astra
    · no max_tokens-style limit near the call
    · API called once per item in a loop; a batch candidate
    · expensive model; check whether every request needs it
      

        $ python scripts/estimate.py --model claude-sonnet-5-5 --input 50 --output 2 --cache 0.85 --shorter 0.3 --to saygm
Claude Sonnet 5.5, 50M input + 2M output tokens/month (prices checked 2026-11-26)
  Bill today                           $    120.00
  Buy via saygm                        $     61.80  (-$58.20)
  Trim context                         $     61.80  (+$0.00)
  Shorter outputs                      $     58.71  (-$3.09)
  Route easy work to Claude Haiku 5.5  $     58.71  (+$0.00)
  Caching                              $     17.12  (-$41.59)
  Batch jobs                           $     17.12  (+$0.00)
Saving: $102.88/month (86%), $1,234.51/year
      

随后 agent 会通过阅读代码来确认每个标记(此处:system prompt 顶部的 datetime.now() 会在每次调用时破坏缓存;工单分类器在一个三分类标签任务上运行 GPT-6.2 Astra),并写出带有修复建议的报告。

价格

llm-cost-audit/references/prices.json 是 14 个热门模型的每 token 价格快照:标价、OpenRouter 的挂牌价(加上其 5.5% 的信用卡手续费)以及 SayGM 的最优挂牌价。网关价格经常变动,在某些开源模型上 OpenRouter 目前更便宜;估算器在出现这种情况时会说明。快照日期在文件内,并随每次估算打印。当前价格见 lesspertoken.com。

变更

1.4(2026 年 11 月 30 日)

  • 新增警告:在调用 Gemini 4 模型的文件中出现显式 Gemini 上下文缓存(caches.create、cached_content=)。Gemini 4 Argon 及更新版本会以 400 拒绝它们,因此扫描器过去会把失效的缓存计为缓存。

文章标题:一个开源扫描器,找出代码中被浪费的 LLM token

正文:

  • 读取 usage 字段(cached_content_token_count、cachedContentTokenCount)不再算作缓存。
  • 缓存说明涵盖 Gemini 4:仅隐式缓存、4,096 token 下限、agent 循环中的 2,050 token 规则、该读哪个 usage 字段,以及何时 Gemini 3.8 Flash 或 Batch 是更好的选择。背景:Gemini 4 上下文缓存不工作?

1.3(2026 年 11 月 26 日)

  • 扫描器还能识别 LlamaIndex、CrewAI(kickoff、kickoff_for_each 作为批量候选)、DSPy、AutoGen/AG2、Haystack、Semantic Kernel、OpenAI Agents SDK(Runner.run)、smolagents 和 Mastra,以及 LiteLLM 风格的模型字符串,如 'anthropic/claude-opus-5-5'。
  • 通过网关访问 Claude 的警告现在只对真实网关路由(OpenRouter、Bedrock、Vertex)触发。在基于 LiteLLM 的框架中,anthropic/claude-... 表示直连 Anthropic。
  • LiteLLM 的 cache_control_injection_points 及其全局 Anthropic prompt 缓存开关算作缓存。

按框架各在一个项目上测试,官方 CrewAI 示例(16 个调用点,全部真实)和两个已审计项目(无变化)。

1.2(2026 年 11 月 26 日)

  • 扫描器能识别通过 LangChain(invoke、ainvoke、stream、batch)和 Pydantic AI(Agent.run、run_sync、run_stream、iter、model_request)发起的调用,以及带提供商前缀的模型字符串,如 'openrouter:anthropic/claude-sonnet-5.5'。
  • 新增警告:仅为直连 Anthropic 开启缓存,以及通过 OpenRouter 或 Bedrock 访问 Claude 但未设置缓存。
  • 字符串、注释和测试文件不再算作调用点。
  • 价格快照于 2026 年 11 月 26 日更新。

在 browser-use(之前发现 0 个调用点,现在 5 个,并在我们报告的缓存缺口的确切行上给出警告)和 GPT Researcher(之前 1 个,现在 11 个)上测试。

1.1:仅在调用存在重复上下文(系统提示、大型静态提示或不断增长的历史)时才建议缓存。

1.0:首次发布。

披露

本 skill 由 LessPerToken 发布,该网站比较 AI API 价格,并在人们通过其链接注册 SayGM 时获得奖励。该 skill 在标价旁中立地报告网关价格,并针对机密和客户数据推荐直连或密封路由。其他所有技术无论你在哪里购买 token 都能省钱。

许可证

The translation is machine-generated and individual terms may be off. Nothing is rewritten or cut; the original is authoritative.

Published by HN AI
This site only summarises and aggregates; copyright stays with the original author. Headlines and summaries follow the source — where they differ, the original wins.
Read the original ↗