cost-tracker

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cost Tracker

成本跟踪器

Monitor and optimize Claude Code session costs.
监控并优化 Claude Code 会话成本。

Trigger

触发条件

Use when:
  • Checking session costs
  • Setting budget alerts
  • Optimizing token spend
  • Planning multi-session work
在以下情况下使用:
  • 检查会话成本
  • 设置预算提醒
  • 优化 Token 支出
  • 规划多会话工作

Cost Awareness

成本意识

Check Current Costs

检查当前成本

bash
undefined
bash
undefined

Session cost is shown at the end of each session

Session cost is shown at the end of each session

Mid-session: check the status bar or run /cost

Mid-session: check the status bar or run /cost

undefined
undefined

Cost Drivers

成本驱动因素

OperationRelative CostOptimization
Large file readsHighUse offset/limit params
Broad grep searchesMediumScope to specific dirs
Subagent spawningHigh (new context)Reuse agents via SendMessage
Repeated tool callsCumulativeBatch operations
MCP tool callsVariableMinimize round-trips
Model selection3-10x differenceUse haiku for simple tasks
操作相对成本优化方式
大型文件读取高使用 offset/limit 参数
广泛 grep 搜索中限定到特定目录
启动 Subagent高(新上下文)通过 SendMessage 复用 Agent
重复工具调用累积批处理操作
MCP 工具调用可变尽量减少往返调用
模型选择3–10 倍差异简单任务使用 Haiku

Token Budget by Task Type

按任务类型划分的 Token 预算

TaskTypical CostBudget Alert
Bug fix$0.10-0.50$1.00
Feature (small)$0.50-2.00$3.00
Feature (large)$2.00-8.00$10.00
Refactor$1.00-5.00$7.00
Code review$0.20-1.00$2.00
任务典型成本预算提醒
Bug 修复$0.10–0.50$1.00
小型功能$0.50–2.00$3.00
大型功能$2.00–8.00$10.00
重构$1.00–5.00$7.00
代码审查$0.20–1.00$2.00

Tool-Call Budgets

工具调用预算

Set explicit budgets by task complexity:
Task TypeTool-Call BudgetWrap-Up At
Quick fix / lookup20 calls15
Bug fix30 calls25
Feature (small)50 calls40
Feature (large)80 calls65
Refactor50 calls40
At the wrap-up threshold: commit progress, assess remaining work, decide whether to continue or start fresh.
根据任务复杂度设置明确的预算:
任务类型工具调用预算收尾阈值
快速修复/查询20 次调用15
Bug 修复30 次调用25
小型功能50 次调用40
大型功能80 次调用65
重构50 次调用40
达到收尾阈值时:提交当前进度,评估剩余工作,并决定是继续当前会话还是开启新会话。

Optimization Strategies

优化策略

  1. Scope prompts tightly — "Fix the auth bug in src/auth/login.ts" vs "Fix the auth bug"
  2. Use the right model — Haiku for simple lookups, Sonnet for features, Opus for architecture
  3. Delegate to subagents — Search/explore operations in subagents keep main context lean
  4. Compact proactively — Don't wait for auto-compact; compact at task boundaries
  5. Read selectively — Use
    offset
    and
    limit
    params for large files
  6. Batch operations — Multiple independent tool calls in one message
  7. One-pass discipline — Write complete solution, test once, stop if green
  8. No re-reads — Don't re-read unchanged files; trust cached knowledge
  9. Read before write — Never write a file without reading it first
  10. Kill output bloat — No sycophantic openers, no closing fluff, no prompt restatement
  1. 精确限定提示范围 — 使用“修复 src/auth/login.ts 中的认证 Bug”,而不是“修复认证 Bug”
  2. 选择合适的模型 — 简单查询使用 Haiku,功能开发使用 Sonnet,架构设计使用 Opus
  3. 委派给 Subagent — 将搜索和探索任务交给 Subagent,保持主上下文精简
  4. 主动 compact — 不要等待自动 compact,在任务边界处执行 compact
  5. 选择性读取 — 对大型文件使用
    offset
    和
    limit
    参数
  6. 批处理操作 — 在一条消息中执行多个相互独立的工具调用
  7. 坚持一次完成 — 写出完整解决方案,测试一次,通过后立即停止
  8. 避免重复读取 — 不要重新读取未发生变化的文件;信任已缓存的信息
  9. 先读后写 — 未读取文件前绝不写入
  10. 减少输出冗余 — 不要使用奉承式开场、无意义结尾或复述提示词

Budget Alerts

预算提醒

Set mental checkpoints:
  • 50% budget — Are you on track? Should you compact?
  • 80% budget — Wrap up current task, avoid new exploration
  • 100% budget — Commit what you have, start fresh session
设置心理检查点:
  • 预算的 50% — 当前进度是否符合预期?是否应该 compact?
  • 预算的 80% — 收尾当前任务,避免开展新的探索
  • 预算的 100% — 提交已有成果,开启新会话

Output

输出

text
COST TRACKER
  Session cost: $X.XX
  Token usage: [input]K in / [output]K out
  Cache hit rate: ~XX%

  Top cost drivers:
    1. [operation] — $X.XX
    2. [operation] — $X.XX

  Optimization tips:
    - [specific suggestion]
text
COST TRACKER
  Session cost: $X.XX
  Token usage: [input]K in / [output]K out
  Cache hit rate: ~XX%

  Top cost drivers:
    1. [operation] — $X.XX
    2. [operation] — $X.XX

  Optimization tips:
    - [specific suggestion]

Rules

规则

  • Track costs as awareness, not hard limits
  • Never sacrifice code quality to save tokens
  • Compact at task boundaries, not mid-task
  • Use subagents for exploration-heavy work
  • 将成本跟踪作为意识提醒,而非硬性限制
  • 绝不牺牲代码质量来节省 Token
  • 在任务边界处 compact,而不是在任务执行中途 compact
  • 对探索密集型工作使用 Subagent