跨AI供应商追踪与分析Token用量及成本,支持预算预警、模型成本对比、优化建议及本地数据存储。
Token Watch : 跟踪、 分析并优化 AI 供应商的象征性使用和成本是一项面向实际任务的技能,主要用于设置预算, 获得提醒、 比较模型并减少您的开支;自由与开源( MIT 许可证)。
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;
若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。
跨 AI 服务商追踪、分析与优化 token 使用量及成本。设置预算、接收告警、对比模型,从而降低支出。
免费且开源(MIT 许可证)• 零依赖 • 本地运行 • 无需 API 密钥
在 OpenAI 收购 OpenClaw 后,token 成本已成为高级用户的首要关切。该 Skill 提供完整的支出可见性——你花在哪里、花了多少、以及如何精准降本。
from tokenwatch import TokenWatch
monitor = TokenWatch()
monitor.record_usage(
model="claude-haiku-4-5-20251001",
input_tokens=1200,
output_tokens=400,
task_label="summarize article"
)
# ✅ 已记录:$0.00192
from tokenwatch import record_from_anthropic_response, record_from_openai_response # Anthropic response = client.messages.create(model="claude-haiku-4-5-20251001", ...) record_from_anthropic_response(monitor, response, task_label="my task") # OpenAI response = client.chat.completions.create(model="gpt-4o-mini", ...) record_from_openai_response(monitor, response, task_label="my task")
monitor.set_budget(
daily_usd=1.00,
weekly_usd=5.00,
monthly_usd=15.00,
per_call_usd=0.10,
alert_at_percent=80.0 # 预算使用率达 80% 时触发告警
)
# ✅ 预算已设置:日预算=$1.0,周预算=$5.0,月预算=$15.0
# 🚨 BUDGET ALERT 将在阈值被突破时自动触发
print(monitor.format_dashboard())
💰 支出概览 今日: $0.0042 (4 次调用,13,600 tokens) 本周: $0.0231 (18 次调用,67,200 tokens) 本月: $0.1847 (92 次调用,438,000 tokens) 📋 预算状态 日预算: [████░░░░░░░░░░░░░░░░] 42% $0.0042 / $1.00 ✅ 月预算: [███████░░░░░░░░░░░░░] 37% $0.1847 / $0.50 ⚠️ 💡 优化建议 🔴 对非推理类任务,将 Opus 替换为 Sonnet(每月节省约 $8.20) 🟡 gpt-4o 平均单次调用成本偏高 —— 缩短 prompt 长度
# 对于 2000 输入 tokens + 500 输出 tokens:
for m in monitor.compare_models(2000, 500)[:6]:
print(f"{m['model']:<42} ${m['cost_usd']:.6f}")
gemini-2.5-flash $0.000300 gpt-4o-mini $0.000600 mistral-small-2501 $0.000350 claude-haiku-4-5-20251001 $0.003600 mistral-large-2501 $0.007000 gemini-2.5-pro $0.007500
estimate = monitor.estimate_cost("claude-sonnet-4-5-20250929", input_tokens=5000, output_tokens=1000)
print(f"预估成本:${estimate['estimated_cost_usd']:.6f}")
suggestions = monitor.get_optimization_suggestions()
for s in suggestions:
savings = s.get("estimated_monthly_savings_usd", 0)
print(f"[{s['priority'].upper()}] {s['message']}")
if savings:
print(f" → 每月节省约 ${savings:.2f}")
monitor.export_report("monthly_report.json", period="month")
覆盖 10 家服务商共 41 款模型 —— 更新于 2026 年 2 月 16 日。
| 服务商 | 模型 | 输入/100 万 tokens | 输出/100 万 tokens |
|---|---|---|---|
| Anthropic | claude-opus-4-6 | $5.00 | $25.00 |
| Anthropic | claude-opus-4-5 | $5.00 | $25.00 |
| Anthropic | claude-sonnet-4-5-20250929 | $3.00 | $15.00 |
| Anthropic | claude-haiku-4-5-20251001 | $1.00 | $5.00 |
| OpenAI | gpt-5.2-pro | $21.00 | $168.00 |
| OpenAI | gpt-5.2 | $1.75 | $14.00 |
| OpenAI | gpt-5 | $1.25 | $10.00 |
| OpenAI | gpt-4.1 | $2.00 | $8.00 |
| OpenAI | gpt-4.1-mini | $0.40 | $1.60 |
| OpenAI | gpt-4.1-nano | $0.10 | $0.40 |
| OpenAI | o3 | $10.00 | $40.00 |
| OpenAI | o4-mini | $1.10 | $4.40 |
| gemini-3-pro | $2.00 | $12.00 | |
| gemini-3-flash | $0.50 | $3.00 | |
| gemini-2.5-pro | $1.25 | $10.00 | |
| gemini-2.5-flash | $0.30 | $2.50 | |
| gemini-2.5-flash-lite | $0.10 | $0.40 | |
| gemini-2.0-flash | $0.10 | $0.40 | |
| Mistral | mistral-large-2411 | $2.00 | $6.00 |
| Mistral | mistral-medium-3 | $0.40 | $2.00 |
| Mistral | mistral-small | $0.10 | $0.30 |
| Mistral | mistral-nemo | $0.02 | $0.10 |
| Mistral | devstral-2 | $0.40 | $2.00 |
| xAI | grok-4 | $3.00 | $15.00 |
| xAI | grok-3 | $3.00 | $15.00 |
| xAI | grok-4.1-fast | $0.20 | $0.50 |
| Kimi | kimi-k2.5 | $0.60 | $3.00 |
| Kimi | kimi-k2 | $0.60 | $2.50 |
| Kimi | kimi-k2-turbo | $1.15 | $8.00 |
| Qwen | qwen3.5-plus | $0.11 | $0.44 |
| Qwen | qwen3-max | $0.40 | $1.60 |
| Qwen | qwen3-vl-32b | $0.91 | $3.64 |
| DeepSeek | deepseek-v3.2 | $0.14 | $0.28 |
| DeepSeek | deepseek-r1 | $0.55 | $2.19 |
| DeepSeek | deepseek-v3 | $0.27 | $1.10 |
| Meta | llama-4-maverick | $0.27 | $0.85 |
| Meta | llama-4-scout | $0.18 | $0.59 |
| Meta | llama-3.3-70b | $0.23 | $0.40 |
| MiniMax | minimax-m2.5 | $0.30 | $1.20 |
| MiniMax | minimax-m1 | $0.43 | $1.93 |
| MiniMax | minimax-text-01 | $0.20 | $1.10 |
如需添加自定义模型:请在
tokenwatch.py文件顶部的PROVIDER_PRICING字典中新增条目。
TokenWatch(storage_path)初始化监控器。默认数据存储于 .tokenwatch/ 目录下。
record_usage(model, input_tokens, output_tokens, task_label, session_id)记录一次 API 调用。返回包含计算成本的 TokenUsageRecord 对象。
set_budget(daily_usd, weekly_usd, monthly_usd, per_call_usd, alert_at_percent)配置支出限额。当用量达到设定阈值时,系统自动触发告警。
get_spend(period)获取聚合支出数据。支持周期参数:"today"、"week"、"month"、"all" 或指定日期 "YYYY-MM-DD"。
get_spend_by_model(period)按模型维度统计支出,结果按成本由高到低排序。
get_spend_by_provider(period)按服务商维度统计支出。
compare_models(input_tokens, output_tokens)对比所有已知模型的成本。返回按价格升序排列的模型列表。
estimate_cost(model, input_tokens, output_tokens)调用前预估成本。
get_optimization_suggestions()分析历史使用数据,返回带预估月度节省金额的优化建议列表(按优先级排序)。
format_dashboard()生成面向人类阅读的支出仪表盘,含预算进度条与实用提示。
export_report(output_file, period)导出完整报告至 JSON 文件。
record_from_anthropic_response(monitor, response, task_label)辅助函数:从 Anthropic SDK 的响应对象中自动提取并记录 token 使用数据。
record_from_openai_response(monitor, response, task_label)辅助函数:从 OpenAI SDK 的响应对象中自动提取并记录 token 使用数据。
.tokenwatch/ 目录中最后更新时间:2026 年 2 月 16 日
当前版本:1.2.3
状态:活跃维护中 & 社区共同维护
© 2026 UnisAI 社区