cost-tracking

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Cost Tracking

成本追踪

Use this skill to analyze Claude Code cost and usage history from a local SQLite database. It is intended for users who already have a cost-tracking hook or plugin writing usage rows to
~/.claude-cost-tracker/usage.db
.
Source: salvaged from stale community PR #1304 by
MayurBhavsar
.
使用此技能从本地SQLite数据库分析Claude Code的成本和使用历史。它适用于已配置成本追踪钩子或插件,且该钩子/插件会将使用记录写入
~/.claude-cost-tracker/usage.db
的用户。
来源:由
MayurBhavsar
提交的陈旧社区PR #1304中修复而来。

When to Use

使用场景

  • The user asks "how much have I spent?", "what did this session cost?", or "what is my token usage?"
  • The user mentions budgets, spending limits, overruns, or cost controls.
  • The user wants a cost breakdown by project, tool, session, model, or date.
  • The user wants to compare today against yesterday or inspect a recent trend.
  • The user asks for a CSV export of recent usage records.
  • 用户询问“我已经花了多少钱?”、“本次会话花费了多少?”或“我的token使用量是多少?”
  • 用户提及预算、支出限额、超支或成本控制相关内容。
  • 用户需要按项目、工具、会话、模型或日期划分的成本明细。
  • 用户想要对比今日与昨日的支出,或查看近期趋势。
  • 用户要求导出近期使用记录的CSV文件。

How It Works

工作原理

First verify prerequisites:
bash
command -v sqlite3 >/dev/null && echo "sqlite3 available" || echo "sqlite3 missing"
test -f ~/.claude-cost-tracker/usage.db && echo "Database found" || echo "Database not found"
If the database is missing, do not fabricate usage data. Tell the user that cost tracking is not configured and suggest installing or enabling a trusted local cost-tracking hook/plugin.
The expected
usage
table usually contains one row per tool call or model interaction. Column names vary by tracker, but the examples below assume:
ColumnMeaning
timestamp
ISO timestamp for the usage event
project
Project or repository name
tool_name
Tool or event name
input_tokens
Input token count, when recorded
output_tokens
Output token count, when recorded
cost_usd
Precomputed cost in USD
session_id
Claude Code session identifier
model
Model used for the event
Prefer
cost_usd
over hand-calculating pricing. Model prices and cache pricing change over time, and the tracker should be the source of truth for how each row was priced.
首先验证前置条件:
bash
command -v sqlite3 >/dev/null && echo "sqlite3 available" || echo "sqlite3 missing"
test -f ~/.claude-cost-tracker/usage.db && echo "Database found" || echo "Database not found"
如果数据库不存在,请勿编造使用数据。告知用户成本追踪未配置,并建议安装或启用可信的本地成本追踪钩子/插件。
预期的
usage
表通常每行对应一次工具调用或模型交互。列名因追踪工具而异,但以下示例假设包含以下列:
列名含义
timestamp
使用事件的ISO时间戳
project
项目或仓库名称
tool_name
工具或事件名称
input_tokens
输入token数量(如有记录)
output_tokens
输出token数量(如有记录)
cost_usd
预计算的美元成本
session_id
Claude Code会话标识符
model
事件使用的模型
优先使用
cost_usd
字段,而非手动计算价格。模型价格和缓存价格会随时间变化,追踪工具应是每一行定价的事实来源。

Examples

示例

Quick Summary

快速摘要

bash
sqlite3 ~/.claude-cost-tracker/usage.db "
  SELECT
    'Today: $' || ROUND(COALESCE(SUM(CASE WHEN date(timestamp) = date('now') THEN cost_usd END), 0), 4) ||
    ' | Total: $' || ROUND(COALESCE(SUM(cost_usd), 0), 4) ||
    ' | Calls: ' || COUNT(*) ||
    ' | Sessions: ' || COUNT(DISTINCT session_id)
  FROM usage;
"
bash
sqlite3 ~/.claude-cost-tracker/usage.db "
  SELECT
    'Today: $' || ROUND(COALESCE(SUM(CASE WHEN date(timestamp) = date('now') THEN cost_usd END), 0), 4) ||
    ' | Total: $' || ROUND(COALESCE(SUM(cost_usd), 0), 4) ||
    ' | Calls: ' || COUNT(*) ||
    ' | Sessions: ' || COUNT(DISTINCT session_id)
  FROM usage;
"

Cost By Project

按项目划分成本

bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT project, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY project
  ORDER BY cost DESC;
"
bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT project, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY project
  ORDER BY cost DESC;
"

Cost By Tool

按工具划分成本

bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT tool_name, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY tool_name
  ORDER BY cost DESC;
"
bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT tool_name, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY tool_name
  ORDER BY cost DESC;
"

Last Seven Days

过去七天数据

bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT date(timestamp) AS date, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY date(timestamp)
  ORDER BY date DESC
  LIMIT 7;
"
bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT date(timestamp) AS date, ROUND(SUM(cost_usd), 4) AS cost, COUNT(*) AS calls
  FROM usage
  GROUP BY date(timestamp)
  ORDER BY date DESC
  LIMIT 7;
"

Session Drilldown

会话详情

bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT session_id,
    MIN(timestamp) AS started,
    MAX(timestamp) AS ended,
    ROUND(SUM(cost_usd), 4) AS cost,
    COUNT(*) AS calls
  FROM usage
  GROUP BY session_id
  ORDER BY started DESC
  LIMIT 10;
"
bash
sqlite3 -header -column ~/.claude-cost-tracker/usage.db "
  SELECT session_id,
    MIN(timestamp) AS started,
    MAX(timestamp) AS ended,
    ROUND(SUM(cost_usd), 4) AS cost,
    COUNT(*) AS calls
  FROM usage
  GROUP BY session_id
  ORDER BY started DESC
  LIMIT 10;
"

Reporting Guidance

报告指南

When presenting cost data, include:
  1. Today's spend and yesterday comparison.
  2. Total spend across the tracked database.
  3. Top projects ranked by cost.
  4. Top tools ranked by cost.
  5. Session count and average cost per session when enough data exists.
For small amounts, format currency with four decimal places. For larger amounts, two decimals are enough.
展示成本数据时,应包含:
  1. 今日支出及与昨日的对比。
  2. 追踪数据库中的总支出。
  3. 按成本排序的顶级项目。
  4. 按成本排序的顶级工具。
  5. 当数据足够时,展示会话数量及平均每次会话成本。
对于小额支出,货币格式保留四位小数;大额支出保留两位小数即可。

Anti-Patterns

反模式

  • Do not estimate costs from raw token counts when
    cost_usd
    is present.
  • Do not assume the database exists without checking.
  • Do not run unbounded
    SELECT *
    exports on large databases.
  • Do not hard-code current model pricing in user-facing answers.
  • Do not recommend installing unreviewed hooks or plugins that execute arbitrary code.
  • 当存在
    cost_usd
    字段时,请勿通过原始token数量估算成本。
  • 未检查的情况下,请勿假设数据库存在。
  • 请勿在大型数据库上运行无限制的
    SELECT *
    导出操作。
  • 请勿在面向用户的回答中硬编码当前模型定价。
  • 请勿推荐安装未经验证的、可执行任意代码的钩子或插件。

Related

相关内容

  • /cost-report
    - Command-form report using the same database.
  • cost-aware-llm-pipeline
    - Model-routing and budget-design patterns.
  • token-budget-advisor
    - Context and token-budget planning.
  • strategic-compact
    - Context compaction to reduce repeated token spend.
  • /cost-report
    - 使用同一数据库的命令式报告。
  • cost-aware-llm-pipeline
    - 模型路由和预算设计模式。
  • token-budget-advisor
    - 上下文与token预算规划。
  • strategic-compact
    - 上下文压缩以减少重复token支出。