分析 Claude Code 本地用量、花费与预算
复制安装指令,让 AI 自动完成配置 · 推荐新手
请帮我安装 askskill 上的 "cost-tracking" 技能: 1. 下载 https://raw.githubusercontent.com/affaan-m/ECC/main/skills/cost-tracking/SKILL.md 2. 保存为 ~/.claude/skills/cost-tracking/SKILL.md 3. 装好后重载技能,告诉我可以用了
帮我汇总今天、总计、调用次数和会话数的 Claude Code 花费。
输出今日花费、总花费、调用次数和会话数的简明汇总。
请按 project 汇总 Claude Code 的成本和调用次数,并按花费从高到低排序。
输出每个项目的花费与调用次数列表。
请给我最近 7 天每天的 Claude Code 花费和调用次数,用于观察趋势。
输出最近 7 天按日期排列的花费趋势表。
当你想知道当前或最近一次 Claude Code 会话的成本、调用次数和 token 用量时,可以用它快速汇总。本地数据库里有记录就能直接查。
适合比较不同项目、工具或模型的花费差异,找出成本最高的来源,方便做预算管理和优化。
当你关心超预算、日常消耗或最近 7 天趋势时,可以用它查看时间序列成本,辅助做成本控制。
该技能通过本地 SQLite 的 usage.db 分析 Claude Code 的费用与用量历史,适合查看总花费、按项目或工具拆分、按日期看趋势,以及导出近期记录。文档说明了所需前置条件:本地需有 sqlite3,并且已有可信的 cost-tracking hook/plugin 写入数据;若数据库不存在,应明确提示未配置而不要伪造结果。
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.
First verify prerequisites:
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:
| Column | Meaning |
|---|---|
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.
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;
"
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;
"
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;
"
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;
"
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;
"
When presenting cost data, include:
For small amounts, format currency with four decimal places. For larger amounts, two decimals are enough.
cost_usd is present.SELECT * exports on large databases.…
需要本地可用的 sqlite3,以及由 cost-tracking hook/plugin 写入的 `~/.claude-cost-tracker/usage.db`。如果数据库不存在,就不能编造用量数据。
可按项目、工具、会话、模型和日期查看成本与调用记录,也能看近期趋势。素材还提到可以导出最近的使用记录为 CSV。
应提示用户尚未配置成本跟踪,并建议安装或启用可信的本地 cost-tracking hook/plugin。更具体的实现细节见源码仓库。
在不改变程序行为的前提下,协调完成可验证、可提交的代码重构流程
帮助开发者掌握 SwiftUI 架构模式、状态管理与性能优化实践
帮助你为自适应智能体设计任务编排、评测门控与可复用技能提炼方案
提供现代 Perl 5.36+ 惯用法与最佳实践,帮助编写稳健且易维护的应用。
帮助开发者遵循该 JavaScript 仓库的代码、测试与提交规范
帮助生成或审查设计系统,检查视觉一致性并评审样式相关改动。
按模型汇总 CodexBar 本地费用日志,快速查看 Codex 或 Claude 的成本明细。
用自然语言记录收支、管理预算并生成个人财务汇总报表
监控 Claude Code 工具成本、ROI与预算风险,支持异常预警和趋势预测
帮助独立创业者用自然语言记录支出、管理预算并跟踪多项目财务。
审计 Claude Code 上下文占用并给出可执行的节省令牌优化建议。
通过已登录浏览器读取 Claude 套餐使用率与剩余额度信息