cost-tracking
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCost 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.dbSource: salvaged from stale community PR #1304 by .
MayurBhavsar使用此技能从本地SQLite数据库分析Claude Code的成本和使用历史。它适用于已配置成本追踪钩子或插件,且该钩子/插件会将使用记录写入的用户。
~/.claude-cost-tracker/usage.db来源:由提交的陈旧社区PR #1304中修复而来。
MayurBhavsarWhen 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 table usually contains one row per tool call or model
interaction. Column names vary by tracker, but the examples below assume:
usage| Column | Meaning |
|---|---|
| ISO timestamp for the usage event |
| Project or repository name |
| Tool or event name |
| Input token count, when recorded |
| Output token count, when recorded |
| Precomputed cost in USD |
| Claude Code session identifier |
| Model used for the event |
Prefer 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.
cost_usd首先验证前置条件:
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| 列名 | 含义 |
|---|---|
| 使用事件的ISO时间戳 |
| 项目或仓库名称 |
| 工具或事件名称 |
| 输入token数量(如有记录) |
| 输出token数量(如有记录) |
| 预计算的美元成本 |
| Claude Code会话标识符 |
| 事件使用的模型 |
优先使用字段,而非手动计算价格。模型价格和缓存价格会随时间变化,追踪工具应是每一行定价的事实来源。
cost_usdExamples
示例
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:
- Today's spend and yesterday comparison.
- Total spend across the tracked database.
- Top projects ranked by cost.
- Top tools ranked by cost.
- 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.
展示成本数据时,应包含:
- 今日支出及与昨日的对比。
- 追踪数据库中的总支出。
- 按成本排序的顶级项目。
- 按成本排序的顶级工具。
- 当数据足够时,展示会话数量及平均每次会话成本。
对于小额支出,货币格式保留四位小数;大额支出保留两位小数即可。
Anti-Patterns
反模式
- Do not estimate costs from raw token counts when is present.
cost_usd - Do not assume the database exists without checking.
- Do not run unbounded exports on large databases.
SELECT * - Do not hard-code current model pricing in user-facing answers.
- Do not recommend installing unreviewed hooks or plugins that execute arbitrary code.
- 当存在字段时,请勿通过原始token数量估算成本。
cost_usd - 未检查的情况下,请勿假设数据库存在。
- 请勿在大型数据库上运行无限制的导出操作。
SELECT * - 请勿在面向用户的回答中硬编码当前模型定价。
- 请勿推荐安装未经验证的、可执行任意代码的钩子或插件。
Related
相关内容
- - Command-form report using the same database.
/cost-report - - Model-routing and budget-design patterns.
cost-aware-llm-pipeline - - Context and token-budget planning.
token-budget-advisor - - Context compaction to reduce repeated token spend.
strategic-compact
- - 使用同一数据库的命令式报告。
/cost-report - - 模型路由和预算设计模式。
cost-aware-llm-pipeline - - 上下文与token预算规划。
token-budget-advisor - - 上下文压缩以减少重复token支出。
strategic-compact