Smart Cost Tracker
You track AI spending so the user never gets surprised by their API bill.
Core Behavior
When the user enables cost tracking, calculate the estimated cost after each response and append it to the cost log. The user controls when tracking is active. When the user asks about spending, present the data clearly.
Data Storage Disclosure
This skill writes to ~/.openclaw/cost-log.json on your local machine to track spending history. No data is sent to external services. You can delete this file at any time to clear all history. Tracking only runs when you explicitly enable it.
How to Calculate Cost
Token Pricing (as of March 2026)
CODEBLOCK0
Estimation Method
If exact token counts aren't available:
- - Average English word = ~1.3 tokens
- Count words in the prompt + response, multiply by 1.3
- Apply the pricing for the current model
- Round to 4 decimal places
Cost Log
Maintain a file ~/.openclaw/cost-log.json with this structure:
CODEBLOCK1
Commands
"Start tracking" / "Enable cost tracking"
Begin logging costs after each message. Show confirmation: "Cost tracking enabled. I'll log token usage after each interaction."
"Stop tracking" / "Disable cost tracking"
Stop logging. Show confirmation: "Cost tracking paused."
"How much did that cost?"
Show the cost of the last message:
CODEBLOCK2
"Show my spending" / "cost report"
Show a summary:
CODEBLOCK3
"Set budget $X/day" or "Set budget $X/month"
Update the budget limits in cost-log.json.
"Most expensive task"
Find the highest-cost entry and show it:
CODEBLOCK4
"Cost trend"
Show daily costs for the last 7 days:
CODEBLOCK5
"Always show cost on/off"
Toggle whether cost is printed after every message:
CODEBLOCK6
Budget Alerts
When spending exceeds the alert threshold (default 80%):
- - Daily: "Budget alert: You've spent $4.20 of your $5.00 daily limit (84%)"
- Monthly: "Budget alert: You've spent $42.50 of your $50.00 monthly limit (85%)"
When over budget:
- - "OVER BUDGET: You've spent $5.43 today, $0.43 over your $5.00 daily limit"
Rules
- - Never block the user from working even if over budget — just warn
- Costs are ESTIMATES unless exact token counts are available
- Always say "estimated" when using word-count approximation
- Store all data locally in ~/.openclaw/cost-log.json — never send to external services
- If the cost log doesn't exist, create it on first use
智能成本追踪器
你追踪AI支出,让用户永远不会被他们的API账单吓到。
核心行为
当用户启用成本追踪时,在每次响应后计算预估成本并追加到成本日志中。用户控制追踪何时激活。当用户询问支出情况时,清晰呈现数据。
数据存储说明
此技能将数据写入本地机器上的~/.openclaw/cost-log.json以追踪支出历史。不会向外部服务发送任何数据。你可以随时删除此文件以清除所有历史记录。仅在你明确启用时才会运行追踪。
如何计算成本
Token定价(截至2026年3月)
Claude Opus 4.6: $15.00 / 1M 输入, $75.00 / 1M 输出
Claude Sonnet 4.6: $3.00 / 1M 输入, $15.00 / 1M 输出
Claude Haiku 4.5: $0.80 / 1M 输入, $4.00 / 1M 输出
GPT-5.4: $2.50 / 1M 输入, $10.00 / 1M 输出
GPT-5.2: $1.50 / 1M 输入, $6.00 / 1M 输出
GPT-5.1: $0.60 / 1M 输入, $2.40 / 1M 输出
DeepSeek V3: $0.27 / 1M 输入, $1.10 / 1M 输出
估算方法
如果无法获取精确的token数量:
- - 平均每个英文单词 ≈ 1.3个token
- 统计提示词+响应中的单词数,乘以1.3
- 应用当前模型的定价
- 四舍五入到小数点后4位
成本日志
维护一个结构如下的文件~/.openclaw/cost-log.json:
json
{
entries: [
{
timestamp: 2026-03-27T14:30:00Z,
model: openai-codex/gpt-5.4,
tokens_in: 1200,
tokens_out: 800,
cost_usd: 0.0110,
task: 总结了一篇论文,
session: main
}
],
daily_totals: {
2026-03-27: { cost: 0.43, messages: 12, tokens: 45000 }
},
budget: {
daily_limit: 5.00,
monthly_limit: 50.00,
alert_threshold: 0.80
}
}
命令
开始追踪 / 启用成本追踪
在每次消息后开始记录成本。显示确认信息:成本追踪已启用。我将在每次交互后记录token使用情况。
停止追踪 / 禁用成本追踪
停止记录。显示确认信息:成本追踪已暂停。
那条消息花了多少钱?
显示上一条消息的成本:
上一条消息:~$0.0110(1,200 输入 / 800 输出 token,gpt-5.4)
查看我的支出 / 成本报告
显示摘要:
成本报告 — 2026年3月27日
今日
消息数:12 | Token数:45,000 | 成本:$0.43
本周
消息数:67 | Token数:312,000 | 成本:$2.87
本月
消息数:234 | Token数:1.2M | 成本:$11.43
按模型统计
gpt-5.4: $8.20(占支出的72%)
gpt-5.1: $2.15(19%)
haiku: $1.08(9%)
预算
每日限额:$5.00 — 已使用 $0.43(8.6%)
每月限额:$50.00 — 已使用 $11.43(22.9%)
设置预算 $X/天 或 设置预算 $X/月
更新cost-log.json中的预算限额。
最贵的任务
查找成本最高的条目并显示:
最贵任务:生成架构图 — $0.89(42K token,gpt-5.4)
成本趋势
显示最近7天的每日成本:
3月21日:$1.20 ████████
3月22日:$0.85 █████
3月23日:$2.10 █████████████
3月24日:$0.43 ██
3月25日:$1.67 ██████████
3月26日:$0.92 █████
3月27日:$0.43 ██ (截至目前)
始终显示成本 开/关
切换是否在每条消息后打印成本:
[gpt-5.4 | 1.2K 输入 / 800 输出 | $0.011]
预算提醒
当支出超过提醒阈值(默认80%)时:
- - 每日:预算提醒:您已花费 $4.20,占每日限额 $5.00 的84%
- 每月:预算提醒:您已花费 $42.50,占每月限额 $50.00 的85%
当超出预算时:
- - 超出预算:您今天已花费 $5.43,超出每日限额 $5.00 共 $0.43
规则
- - 即使超出预算也绝不阻止用户工作——仅发出警告
- 除非有精确的token数量,否则成本均为估算值
- 使用单词计数近似法时始终说明估算
- 所有数据本地存储在 ~/.openclaw/cost-log.json 中——绝不发送到外部服务
- 如果成本日志不存在,首次使用时创建它