API Cost Optimizer
Diagnose, estimate, and optimize LLM API costs for OpenClaw agents running 24/7.
What This Skill Does
- - Estimates API spend from agent configuration (model, heartbeat interval, task complexity)
- Diagnoses heartbeat waste — the #1 cause of runaway API bills
- Counts and categorizes tools the agent has access to
- Produces a cost report with prioritized recommendations
- Estimates time-to-payback for any recommended changes
The Problem This Solves
Documented cases show agents burning $8–$250 per week in API costs due to misconfigured heartbeats and inefficient task patterns. A properly tuned heartbeat alone can reduce costs by 60–90%.
Environment Variables
| Variable | Required | Default | Description |
|---|
| INLINECODE0 | No | INLINECODE1 | Provider: openai, anthropic, minimax, INLINECODE5 |
| INLINECODE6 |
No |
weekly | Report interval:
daily,
weekly,
monthly |
|
HEARTBEAT_INTERVAL | No |
auto | Override heartbeat interval in seconds (auto-detect if not set) |
|
MODEL_PRICE_INPUT | No | auto | Price per 1M input tokens (auto-selected by provider) |
|
MODEL_PRICE_OUTPUT | No | auto | Price per 1M output tokens (auto-selected by provider) |
Provider Default Pricing (per 1M tokens)
| Provider | Model | Input | Output |
|---|
| OpenAI | GPT-4o | $2.50 | $10.00 |
| OpenAI |
GPT-4o-mini | $0.15 | $0.60 |
| OpenAI | GPT-4-turbo | $10.00 | $30.00 |
| Anthropic | Claude 3.5 Sonnet | $3.00 | $15.00 |
| Anthropic | Claude 3 Opus | $15.00 | $75.00 |
| MiniMax | MiniMax-M2 | $0.10 | $0.10 |
| MiniMax | MiniMax-M2.1 | $0.20 | $0.40 |
Scripts
analyze.sh — Full Cost Analysis
Runs a complete cost diagnostic on the current OpenClaw configuration.
CODEBLOCK0
Output: Detailed markdown report with:
- - Estimated API spend (daily / weekly / monthly)
- Heartbeat cost analysis
- Tool count risk factor
- Top 3 cost reduction recommendations
- Estimated savings after optimization
heartbeat_diagnosis.sh — Find Heartbeat Waste
Checks OpenClaw heartbeat configuration and calculates wasted API calls.
CODEBLOCK1
Output: Heartbeat efficiency score (0–100%), wasted calls per day, estimated annual waste.
estimate.sh — Quick Cost Estimate
One-shot estimate with optional custom parameters.
CODEBLOCK2
Output: Daily, weekly, and monthly cost estimates for the given parameters.
Cost Reduction Recommendations
Priority order:
- 1. Increase heartbeat interval — If heartbeat is under 5 minutes, increase to 10–30 min for idle agents
- Switch to cheaper model — MiniMax M2 at $0.10/1M is 25x cheaper than GPT-4o for most tasks
- Batch tasks — Run 5–10 tasks per session instead of 1 task per heartbeat cycle
- Cache responses — Use memory skills to avoid repeated context
- Disable verbose logging — Reduces token churn in long conversations
Usage Example
CODEBLOCK3
Notes
- - Estimates are based on configuration analysis, not live API metering
- For actual spend tracking, enable provider cost logs and compare
- MiniMax models offer best price/performance for volume workloads
- Heartbeat waste is typically the single largest cost lever
API 成本优化器
诊断、估算并优化全天候运行的 OpenClaw 智能体的 LLM API 成本。
该技能的功能
- - 估算 API 支出:基于智能体配置(模型、心跳间隔、任务复杂度)
- 诊断心跳浪费:导致 API 账单失控的首要原因
- 统计并分类工具:智能体可访问的工具
- 生成成本报告:附带优先推荐建议
- 估算投资回报周期:针对任何推荐变更
解决的问题
有记录案例显示,由于心跳配置不当和任务模式低效,智能体每周 API 成本高达 8 至 250 美元。仅优化心跳设置即可将成本降低 60% 至 90%。
环境变量
| 变量 | 必填 | 默认值 | 描述 |
|---|
| APICOSTMODEL | 否 | openai | 提供商:openai、anthropic、minimax、lmstudio |
| APICOSTINTERVAL |
否 | weekly | 报告间隔:daily、weekly、monthly |
| HEARTBEAT_INTERVAL | 否 | auto | 覆盖心跳间隔(秒),未设置则自动检测 |
| MODEL
PRICEINPUT | 否 | auto | 每百万输入令牌价格(按提供商自动选择) |
| MODEL
PRICEOUTPUT | 否 | auto | 每百万输出令牌价格(按提供商自动选择) |
提供商默认定价(每百万令牌)
| 提供商 | 模型 | 输入 | 输出 |
|---|
| OpenAI | GPT-4o | $2.50 | $10.00 |
| OpenAI |
GPT-4o-mini | $0.15 | $0.60 |
| OpenAI | GPT-4-turbo | $10.00 | $30.00 |
| Anthropic | Claude 3.5 Sonnet | $3.00 | $15.00 |
| Anthropic | Claude 3 Opus | $15.00 | $75.00 |
| MiniMax | MiniMax-M2 | $0.10 | $0.10 |
| MiniMax | MiniMax-M2.1 | $0.20 | $0.40 |
脚本
analyze.sh — 完整成本分析
对当前 OpenClaw 配置运行完整的成本诊断。
bash
./scripts/analyze.sh
输出: 详细的 Markdown 报告,包含:
- - 预估 API 支出(每日/每周/每月)
- 心跳成本分析
- 工具数量风险因子
- 前 3 项成本降低建议
- 优化后预估节省金额
heartbeat_diagnosis.sh — 发现心跳浪费
检查 OpenClaw 心跳配置并计算浪费的 API 调用。
bash
./scripts/heartbeat_diagnosis.sh
输出: 心跳效率评分(0–100%)、每日浪费调用次数、预估年度浪费金额。
estimate.sh — 快速成本估算
一次性估算,支持自定义参数。
bash
./scripts/estimate.sh <心跳秒数> <每日任务数> <平均输入令牌数> <平均输出令牌数>
输出: 给定参数的每日、每周和每月成本估算。
成本降低建议
优先级顺序:
- 1. 增加心跳间隔 — 若心跳低于 5 分钟,对空闲智能体增加至 10–30 分钟
- 切换至更便宜的模型 — MiniMax M2 每百万令牌 $0.10,对大多数任务比 GPT-4o 便宜 25 倍
- 批量处理任务 — 每次会话运行 5–10 个任务,而非每个心跳周期执行 1 个任务
- 缓存响应 — 使用记忆技能避免重复上下文
- 禁用详细日志 — 减少长对话中的令牌消耗
使用示例
bash
使用 MiniMax 定价进行完整分析
export API
COSTMODEL=minimax
./scripts/analyze.sh
快速估算
./scripts/estimate.sh 300 20 8000 2000
仅心跳诊断
./scripts/heartbeat_diagnosis.sh
备注
- - 估算基于配置分析,而非实时 API 计量
- 如需实际支出追踪,请启用提供商成本日志并进行对比
- MiniMax 模型为批量工作负载提供最佳性价比
- 心跳浪费通常是最大的单一成本杠杆