cost-metering
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chinese<objective>
Track Claude API costs across sessions with budget alerts, model routing optimization, and spend reporting. Integrates with workflow-orchestrator's cost gate for automated budget enforcement.
</objective>
<quick_start>
Check current spend:
bash
cat ~/.claude/daily-cost.json 2>/dev/null || echo "No tracking yet"Initialize tracking:
bash
mkdir -p ~/.claude
echo '{"date":"'$(date +%Y-%m-%d)'","spent":0,"budget_monthly":100,"budget_daily":5}' > ~/.claude/daily-cost.json</quick_start>
<success_criteria>
- Daily cost tracking initialized at
~/.claude/daily-cost.json - Budget alerts fire at 50% (info), 80% (warn), and 95% (block) thresholds
- Model routing applied: Haiku for search/classify, Sonnet for code, DeepSeek for bulk
- Cost-per-feature metric available via portfolio-artifact integration
- Monthly spend stays within configured budget ($100/mo default) </success_criteria>
<objective>
跨会话跟踪Claude API成本,提供预算警报、模型路由优化以及支出报告。与workflow-orchestrator的成本网关集成,实现自动化预算管控。
</objective>
<quick_start>
查看当前支出:
bash
cat ~/.claude/daily-cost.json 2>/dev/null || echo "No tracking yet"初始化跟踪:
bash
mkdir -p ~/.claude
echo '{"date":"'$(date +%Y-%m-%d)'","spent":0,"budget_monthly":100,"budget_daily":5}' > ~/.claude/daily-cost.json</quick_start>
<success_criteria>
- 每日成本跟踪已初始化至
~/.claude/daily-cost.json - 预算警报在50%(提示)、80%(警告)和95%(拦截)阈值触发
- 已应用模型路由:Haiku用于搜索/分类,Sonnet用于代码生成,DeepSeek用于批量处理
- 通过portfolio-artifact集成可获取按功能划分的成本指标
- 月度支出保持在配置的预算范围内(默认100美元/月) </success_criteria>
Model Rates (Current)
当前模型费率
| Model | Input/1M tokens | Output/1M tokens | Typical Use |
|---|---|---|---|
| Claude Opus 4 | $15.00 | $75.00 | Architecture, complex reasoning |
| Claude Sonnet 4.5 | $3.00 | $15.00 | Code generation, standard tasks |
| Claude Haiku 4.5 | $0.25 | $1.25 | Search, classification, simple |
| DeepSeek V3 | $0.27 | $1.10 | Bulk processing |
| GROQ Llama 3.3 70B | $0.59 | $0.79 | Fast inference |
| Voyage Embeddings | $0.10 | — | Embeddings |
| 模型 | 每1M输入代币成本 | 每1M输出代币成本 | 典型用途 |
|---|---|---|---|
| Claude Opus 4 | $15.00 | $75.00 | 架构设计、复杂推理 |
| Claude Sonnet 4.5 | $3.00 | $15.00 | 代码生成、标准任务 |
| Claude Haiku 4.5 | $0.25 | $1.25 | 搜索、分类、简单任务 |
| DeepSeek V3 | $0.27 | $1.10 | 批量处理 |
| GROQ Llama 3.3 70B | $0.59 | $0.79 | 快速推理 |
| Voyage Embeddings | $0.10 | — | 向量嵌入 |
Budget Configuration
预算配置
~/.claude/daily-cost.json
~/.claude/daily-cost.json
json
{
"date": "2026-02-07",
"spent": 2.40,
"budget_monthly": 100,
"budget_daily": 5,
"alerts": {
"info": 0.5,
"warn": 0.8,
"block": 0.95
}
}json
{
"date": "2026-02-07",
"spent": 2.40,
"budget_monthly": 100,
"budget_daily": 5,
"alerts": {
"info": 0.5,
"warn": 0.8,
"block": 0.95
}
}Alert Thresholds
警报阈值
| Threshold | % Budget | Action |
|---|---|---|
| Info | 50% | Display: "50% of monthly budget used" |
| Warn | 80% | Yellow alert: "⚠️ 80% budget — consider model downgrade" |
| Block | 95% | Red alert: "🛑 95% budget — require explicit override to continue" |
| 阈值 | 预算占比 | 操作 |
|---|---|---|
| 提示 | 50% | 显示:"已使用50%月度预算" |
| 警告 | 80% | 黄色警报:"⚠️ 已使用80%预算 — 考虑降级模型" |
| 拦截 | 95% | 红色警报:"🛑 已使用95%预算 — 需要明确覆盖才能继续" |
Cost Optimization Strategies
成本优化策略
1. Model Routing (biggest impact)
1. 模型路由(影响最大)
| Task | Expensive | Optimized | Savings |
|---|---|---|---|
| File search | Sonnet ($3/1M) | Haiku ($0.25/1M) | 92% |
| Code review | Sonnet ($3/1M) | Haiku ($0.25/1M) | 92% |
| Classification | Sonnet ($3/1M) | Haiku ($0.25/1M) | 92% |
| Bulk processing | Sonnet ($3/1M) | DeepSeek ($0.27/1M) | 91% |
Rule: If the task doesn't generate code, use Haiku. If it doesn't need Claude, use DeepSeek.
| 任务 | 高成本方案 | 优化后方案 | 成本节省比例 |
|---|---|---|---|
| 文件搜索 | Sonnet($3/1M) | Haiku($0.25/1M) | 92% |
| 代码审查 | Sonnet($3/1M) | Haiku($0.25/1M) | 92% |
| 分类任务 | Sonnet($3/1M) | Haiku($0.25/1M) | 92% |
| 批量处理 | Sonnet($3/1M) | DeepSeek($0.27/1M) | 91% |
规则: 如果任务不需要生成代码,使用Haiku。如果不需要Claude,使用DeepSeek。
2. Context Management
2. 上下文管理
- Keep SKILL.md files under 200 lines (progressive disclosure)
- Load reference files only when needed
- Use agent with
Exploremodel for codebase searchhaiku - Avoid reading entire files — use Grep to find specific lines
- 保持SKILL.md文件在200行以内(渐进式披露)
- 仅在需要时加载参考文件
- 使用agent搭配
Explore模型进行代码库搜索haiku - 避免读取整个文件 — 使用Grep查找特定行
3. Task Batching
3. 任务批处理
- Group related searches into one Explore agent call
- Use parallel subagents (haiku) instead of serial sonnet calls
- Combine file reads when possible
- 将相关搜索合并为一次Explore agent调用
- 使用并行子agent(haiku)替代串行sonnet调用
- 尽可能合并文件读取操作
Tracking Commands
跟踪命令
Daily Spend Check
每日支出核查
bash
cat ~/.claude/daily-cost.json | jq '{date, spent, remaining: (.budget_daily - .spent), pct: ((.spent / .budget_monthly) * 100 | floor)}'bash
cat ~/.claude/daily-cost.json | jq '{date, spent, remaining: (.budget_daily - .spent), pct: ((.spent / .budget_monthly) * 100 | floor)}'Weekly Report
每周报告
bash
undefinedbash
undefinedAggregate daily logs
汇总每日日志
cat ~/.claude/cost-log.jsonl | jq -s 'group_by(.phase) | map({phase: .[0].phase, total: (map(.est_cost) | add), count: length})'
undefinedcat ~/.claude/cost-log.jsonl | jq -s 'group_by(.phase) | map({phase: .[0].phase, total: (map(.est_cost) | add), count: length})'
undefinedMonthly Summary
月度汇总
bash
cat ~/.claude/cost-log.jsonl | jq -s '{
total: (map(.est_cost) | add),
by_model: (group_by(.model) | map({model: .[0].model, cost: (map(.est_cost) | add)})),
by_phase: (group_by(.phase) | map({phase: .[0].phase, cost: (map(.est_cost) | add)}))
}'bash
cat ~/.claude/cost-log.jsonl | jq -s '{
total: (map(.est_cost) | add),
by_model: (group_by(.model) | map({model: .[0].model, cost: (map(.est_cost) | add)})),
by_phase: (group_by(.phase) | map({phase: .[0].phase, cost: (map(.est_cost) | add)}))
}'Integration Points
集成点
| System | How |
|---|---|
| workflow-orchestrator | Cost gate checks budget before workflows |
| subagent-teams | Model selection uses cost tiers |
| agent-capability-matrix | Includes model recommendations |
| portfolio-artifact | Reports cost-per-feature metrics |
| End Day protocol | Logs daily costs, updates MTD |
| TaskCreate/TaskUpdate | Zero API cost — local UI tool for progress tracking |
| TeamCreate/SendMessage | Zero API cost — local coordination (but spawned agents incur model costs) |
| 系统 | 集成方式 |
|---|---|
| workflow-orchestrator | 工作流执行前通过成本网关检查预算 |
| subagent-teams | 模型选择采用成本分层策略 |
| agent-capability-matrix | 包含模型推荐内容 |
| portfolio-artifact | 报告按功能划分的成本指标 |
| End Day协议 | 记录每日成本,更新月度累计数据 |
| TaskCreate/TaskUpdate | 零API成本 — 本地UI工具用于进度跟踪 |
| TeamCreate/SendMessage | 零API成本 — 本地协作工具(但生成的agent会产生模型成本) |
Storage
存储结构
~/.claude/
├── daily-cost.json # Current day's budget + spend
├── cost-log.jsonl # Append-only operation log
└── portfolio/
└── daily-metrics.jsonl # Includes cost-per-featureDeep dive: See ,
reference/cost-tracking-guide.mdreference/budget-templates.md~/.claude/
├── daily-cost.json # 当日预算与支出数据
├── cost-log.jsonl # 追加式操作日志
└── portfolio/
└── daily-metrics.jsonl # 包含按功能划分的成本数据深入了解: 请查看、
reference/cost-tracking-guide.mdreference/budget-templates.md