简介
基于本地 OpenClaw 会话日志估算 Claude API 实际开销,支持订阅与按量计费对比,帮助用户做成本决策。
原始描述
Estimate API token costs from OpenClaw session transcripts. Analyzes all agent sessions to calculate what you'd pay on per-token pricing vs subscription plan...
通过你的 Agent 添加
安装目录:token-cost-estimator/(目录名取自 SKILL.md 的 frontmatter name,不是 URL 里的 slug)。
点「快速安装」复制一段指令贴给你的 Agent:它会先下载到 /tmp、把文件清单和校验值给你看,你确认之后才解压安装。不会一步装完、也不改系统其它位置。
或者手动安装
包是「根级 SKILL.md + 作者附带的文件」,解压到上面的 <slug>/ 目录即可;
目录名不用换,也不要把包直接解压到 skills 根目录。
技能申请的权限由作者决定:装之前先看清单里有没有脚本、它会读写什么。
中文使用说明 来自收录源
核心用法
Token Cost Estimator 是一款针对 Claude/OpenClaw 用户的本地成本分析工具,通过解析 ~/.openclaw/agents 下的 JSONL 会话转录文件,估算真实 API 调用成本。用户只需运行提供的 Python 脚本,即可遍历所有 Agent 的历史会话,统计输入/输出 token 量,并按 Anthropic 官方定价(Opus/Sonnet 双档)计算费用。
关键计算逻辑:
- 上下文重发成本:脚本核心创新在于模拟了对话的累积特性——每一轮新消息都会将完整历史作为输入重新发送,这是 API 计费的最大隐性成本。
- 缓存命中率模拟:支持 60%-90% 缓存命中场景计算,缓存读取价格仅为标准输入的 12.5%。
- 字符到 token 转换:采用保守的 4:1 字符/token 估算比。
显著优点
- 数据主权:完全本地运行,敏感会话数据不离境。
- 真实成本透视:揭示「50 轮对话 = 1250 轮等效输入」的计费真相,打破「按轮计费」直觉误区。
- 订阅决策支持:内置 Claude Max ($100/$200) 与 API 按量计费的盈亏平衡分析框架。
潜在局限与风险
局限
说明
估算偏差
未计入系统提示、工具定义、内部重试,实际成本通常为估算值的 1.5-2x
路径硬编码
依赖 OpenClaw 特定目录结构,其他平台无法直接使用
定价时效性
PRICING 字典需手动维护,Anthropic 调价后失效
缓存不确定性
60%-90% 为经验区间,实际命中率因 prompt 结构而异
OAuth 政策风险
明确指出 Anthropic 已限制第三方工具的 OAuth token 使用,订阅方案可行性存疑
适合人群
- 重度 Claude 用户:月 API 支出超过 $100,需验证订阅是否划算
- 多 Agent 管理者:同时运行多个 OpenClaw Agent,需全局成本视图
- 成本敏感型开发者:在 Opus 与 Sonnet 之间做性价比权衡
常规风险提示
- 执行风险:脚本直接读取用户主目录文件,需确认来源可信
- 隐私残留:虽为本地处理,但转录文件可能包含敏感对话内容
- 策略合规:依赖 OpenClaw 的非官方实现,未来目录结构变更可能导致失效
| 局限 | 说明 |
|---|---|
| 估算偏差 | 未计入系统提示、工具定义、内部重试,实际成本通常为估算值的 1.5-2x |
| 路径硬编码 | 依赖 OpenClaw 特定目录结构,其他平台无法直接使用 |
| 定价时效性 | PRICING 字典需手动维护,Anthropic 调价后失效 |
| 缓存不确定性 | 60%-90% 为经验区间,实际命中率因 prompt 结构而异 |
| OAuth 政策风险 | 明确指出 Anthropic 已限制第三方工具的 OAuth token 使用,订阅方案可行性存疑 |
适合人群
- 重度 Claude 用户:月 API 支出超过 $100,需验证订阅是否划算
- 多 Agent 管理者:同时运行多个 OpenClaw Agent,需全局成本视图
- 成本敏感型开发者:在 Opus 与 Sonnet 之间做性价比权衡
常规风险提示
- 执行风险:脚本直接读取用户主目录文件,需确认来源可信
- 隐私残留:虽为本地处理,但转录文件可能包含敏感对话内容
- 策略合规:依赖 OpenClaw 的非官方实现,未来目录结构变更可能导致失效
- 执行风险:脚本直接读取用户主目录文件,需确认来源可信
- 隐私残留:虽为本地处理,但转录文件可能包含敏感对话内容
- 策略合规:依赖 OpenClaw 的非官方实现,未来目录结构变更可能导致失效
技能正文
name: token-cost-estimator
description: Estimate API token costs from OpenClaw session transcripts. Analyzes all agent sessions to calculate what you'd pay on per-token pricing vs subscription plans. Use when asked about API costs, token usage, billing estimates, or comparing Max/Pro plans to pay-per-use.
Token Cost Estimator
Estimate real API costs from OpenClaw session transcript data.
How It Works
OpenClaw stores session transcripts as JSONL files in ~/.openclaw/agents/<agent>/sessions/*.jsonl. Each line is a turn with role, content, and sometimes usage data.
Estimation Script
Run this Python script to analyze all agents:
import json, glob, os
AGENTS_DIR = os.path.expanduser('~/.openclaw/agents')
# Pricing per million tokens (update as needed)
PRICING = {
'opus': {'input': 15, 'output': 75, 'cache_read': 1.875},
'sonnet': {'input': 3, 'output': 15, 'cache_read': 0.375},
}
agent_dirs = [d for d in os.listdir(AGENTS_DIR) if os.path.isdir(os.path.join(AGENTS_DIR, d))]
grand_in, grand_out = 0, 0
for agent in sorted(agent_dirs):
sess_dir = os.path.join(AGENTS_DIR, agent, 'sessions')
if not os.path.isdir(sess_dir):
continue
total_in, total_out, sessions = 0, 0, 0
for f in glob.glob(os.path.join(sess_dir, '*.jsonl')):
sessions += 1
turns = []
for line in open(f):
try:
obj = json.loads(line)
msg = obj.get('message', {})
if not isinstance(msg, dict): continue
role = msg.get('role', '')
raw = json.dumps(msg)
turns.append((role, len(raw)))
except: pass
# Account for context re-sending
cumulative = 0
for role, chars in turns:
if role in ('user', 'system', 'tool'):
cumulative += chars
elif role == 'assistant':
total_in += cumulative // 4
total_out += chars // 4
print(f'{agent}: {sessions} sessions, ~{total_in:,} input, ~{total_out:,} output tokens')
grand_in += total_in
grand_out += total_out
print(f'\nTotal input: ~{grand_in:,}, output: ~{grand_out:,}')
for tier, rates in PRICING.items():
for cache_pct in [0.6, 0.9]:
cached = int(grand_in * cache_pct)
uncached = grand_in - cached
cost = (uncached/1e6)*rates['input'] + (cached/1e6)*rates['cache_read'] + (grand_out/1e6)*rates['output']
print(f'{tier} ({int(cache_pct*100)}% cache): ${cost:,.2f}')
Key Concepts
Context re-sending: Every API call sends the full conversation history as input. A 50-turn conversation re-sends all prior turns on each new message. This is the #1 cost driver.
Cache hits: OpenClaw caches prompt prefixes. Typical cache hit rates: 60-90%. Cache reads cost 87.5% less than fresh input.
What transcripts miss: System prompts, tool definitions, and internal retries aren't always logged. Real cost is typically 1.5-2x the transcript estimate.
Comparing Plans
Plan
Monthly Cost
Best For
API (Opus)
Variable
Heavy agentic use (>$200/mo equivalent)
API (Sonnet)
Variable
Most agent tasks, 5x cheaper than Opus
Claude Max ($100)
$100 flat
Light-medium use via OAuth (if allowed)
Claude Max ($200)
$200 flat
Heavy use via OAuth (if allowed)
| Plan | Monthly Cost | Best For |
|---|---|---|
| API (Opus) | Variable | Heavy agentic use (>$200/mo equivalent) |
| API (Sonnet) | Variable | Most agent tasks, 5x cheaper than Opus |
| Claude Max ($100) | $100 flat | Light-medium use via OAuth (if allowed) |
| Claude Max ($200) | $200 flat | Heavy use via OAuth (if allowed) |
Break-even: If your estimated API cost exceeds your subscription price, the subscription saves money. Note: Anthropic has restricted OAuth token use in third-party tools.
正文来自原始发布页, 版权归作者所有,许可未标注。 权利人认为有不妥,可申请下架。