闲社服务运行正常AI智能体自动化平台
c

Claude API成本优化

技能标识:claude-api-cost-optimizer

Minimize Anthropic Claude API costs through model selection, prompt caching, batching, and cost tracking. Trigger phrases: reduce API costs, optimize Claude spending, save on API calls, Claude cost optimization, cheaper Claude models.

作者:admin | 来源记录:ClawHub
登记来源
ClawHub
版本
V 1.0.3
检测标记
后台标记通过
免费
免费获取
0
收藏
来源与检测标记为平台登记信息,并不代表已展示可复核的检测报告。使用前请核对版本、依赖和所需权限,建议先在隔离环境中运行。
概述
安装方式
版本历史

Claude API成本优化

Claude API 成本优化器

通过智能模型选择、缓存和批处理,将Claude API成本降低70-90%。

快速入门

  1. 1. 审计当前API调用 — 识别哪些使用Opus或Sonnet的任务可以使用Haiku。仅模型选择一项即可在简单任务上节省10-18倍成本。
  2. 为每个任务选择最便宜的模型层级:Haiku(最便宜)→ Sonnet(中等)→ Opus(最昂贵,谨慎使用)。查看references/pricing.md了解当前费率。
  3. 通过向消息块添加cachecontrol: {type: ephemeral},为重复上下文(系统提示、代码库)启用提示缓存。
  4. 实施成本报告 — 跟踪API响应中的inputtokens、output_tokens和缓存指标。

关键概念

  • - 模型选择 — 简单任务(格式化、注释)使用Haiku — 最便宜层级。中等任务(重构、调试)使用Sonnet — 中等层级。仅复杂任务(架构、安全)使用Opus — 最昂贵,谨慎使用。查看references/pricing.md了解当前费率。
  • 提示缓存 — 缓存大型静态内容(系统提示、代码库上下文)。缓存读取成本降低90%;写入成本在1-2次复用后即可收回。
  • 批处理 — 将多个请求合并为一个API调用,消除单次请求开销。减少80%的调用 ≈ 降低80%的成本。
  • 本地缓存 — 在本地缓存相同响应,完全跳过冗余API调用。
  • 上下文提取 — 仅发送相关片段,而非整个文件。输入越小 = 成本越低。
  • max_tokens纪律 — 设置合理的限制;未使用的token预算就是浪费的钱。

常见用法

代码示例使用Python,但概念适用于任何语言或SDK。

模型选择模式:

python
def selectmodel(tasktype: str) -> str:
simple_tasks = [formatting, comments, explanation, rename]
complextasks = [architecture, algorithm, securityaudit]
return (claude-haiku-4-5-20251001 if tasktype in simpletasks else
claude-opus-4-6 if tasktype in complextasks else
claude-sonnet-4-6)

提示缓存:

python
response = client.messages.create(
model=claude-sonnet-4-6,
max_tokens=1024,
system=[{
type: text,
text: system_prompt,
cache_control: {type: ephemeral}
}],
messages=[{
role: user,
content: [
{type: text, text: fCode:\n{source_code},
cache_control: {type: ephemeral}},
{type: text, text: query}
]
}]
)

成本跟踪:

python
usage = response.usage
cost = (usage.inputtokens * INPUTRATE +
usage.cachecreationinputtokens * CACHEWRITE_RATE +
usage.cachereadinputtokens * CACHEREAD_RATE +
usage.outputtokens * OUTPUTRATE)

参考资料

  • - references/implementation.md — 完整实现模式、模型路由、缓存设置、批处理、重试逻辑和反模式
  • references/pricing.md — 当前定价、缓存成本计算、节省计算和批处理API详情

标签

skill ai

通过对话安装

以下为平台配置的接入选项,并非逐项实测通过。能否安装取决于客户端支持、技能来源和运行环境:

OpenClaw WorkBuddy QClaw Kimi Claude

方式一:安装 SkillHub 和技能

帮我安装 SkillHub 和 claude-api-cost-optimizer-1775940105 技能

方式二:设置 SkillHub 为优先技能安装源

设置 SkillHub 为我的优先技能安装源,然后帮我安装 claude-api-cost-optimizer-1775940105 技能

通过命令行安装

skillhub install claude-api-cost-optimizer-1775940105

下载

⬇ 下载 claude-api-cost-optimizer v1.0.3(免费)

文件大小: 9.56 KB | 发布时间: 2026-4-12 09:23

v1.0.3 最新 2026-4-12 09:23
Verified metadata: ANTHROPIC_API_KEY correctly declared in requires.env
返回顶部