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>
<triggers> - "cost check", "budget status", "how much spent" - "optimize costs", "cost tracking", "budget alert" - "model routing", "cheaper model", "cost report" </triggers>
<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>
<triggers> - "成本核查"、"预算状态"、"支出金额" - "成本优化"、"成本跟踪"、"预算警报" - "模型路由"、"更经济的模型"、"成本报告" </triggers>

Model Rates (Current)

当前模型费率

ModelInput/1M tokensOutput/1M tokensTypical Use
Claude Opus 4$15.00$75.00Architecture, complex reasoning
Claude Sonnet 4.5$3.00$15.00Code generation, standard tasks
Claude Haiku 4.5$0.25$1.25Search, classification, simple
DeepSeek V3$0.27$1.10Bulk processing
GROQ Llama 3.3 70B$0.59$0.79Fast inference
Voyage Embeddings$0.10Embeddings

模型每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% BudgetAction
Info50%Display: "50% of monthly budget used"
Warn80%Yellow alert: "⚠️ 80% budget — consider model downgrade"
Block95%Red alert: "🛑 95% budget — require explicit override to continue"

阈值预算占比操作
提示50%显示:"已使用50%月度预算"
警告80%黄色警报:"⚠️ 已使用80%预算 — 考虑降级模型"
拦截95%红色警报:"🛑 已使用95%预算 — 需要明确覆盖才能继续"

Cost Optimization Strategies

成本优化策略

1. Model Routing (biggest impact)

1. 模型路由(影响最大)

TaskExpensiveOptimizedSavings
File searchSonnet ($3/1M)Haiku ($0.25/1M)92%
Code reviewSonnet ($3/1M)Haiku ($0.25/1M)92%
ClassificationSonnet ($3/1M)Haiku ($0.25/1M)92%
Bulk processingSonnet ($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
    Explore
    agent with
    haiku
    model for codebase search
  • Avoid reading entire files — use Grep to find specific lines
  • 保持SKILL.md文件在200行以内(渐进式披露)
  • 仅在需要时加载参考文件
  • 使用
    Explore
    agent搭配
    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
undefined
bash
undefined

Aggregate daily logs

汇总每日日志

cat ~/.claude/cost-log.jsonl | jq -s 'group_by(.phase) | map({phase: .[0].phase, total: (map(.est_cost) | add), count: length})'
undefined
cat ~/.claude/cost-log.jsonl | jq -s 'group_by(.phase) | map({phase: .[0].phase, total: (map(.est_cost) | add), count: length})'
undefined

Monthly 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

集成点

SystemHow
workflow-orchestratorCost gate checks budget before workflows
subagent-teamsModel selection uses cost tiers
agent-capability-matrixIncludes model recommendations
portfolio-artifactReports cost-per-feature metrics
End Day protocolLogs daily costs, updates MTD
TaskCreate/TaskUpdateZero API cost — local UI tool for progress tracking
TeamCreate/SendMessageZero 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-feature
Deep dive: See
reference/cost-tracking-guide.md
,
reference/budget-templates.md
~/.claude/
├── daily-cost.json          # 当日预算与支出数据
├── cost-log.jsonl           # 追加式操作日志
└── portfolio/
    └── daily-metrics.jsonl  # 包含按功能划分的成本数据
深入了解: 请查看
reference/cost-tracking-guide.md
reference/budget-templates.md