New to Claude Skills? Learn how to install them →

affaan-m on GitHub

Cost Tracking

Free

Analyze and report your Claude Code expenses efficiently.

by affaan-m239.3k stars on affaan-m/ecc
1 views
Updated Aug 10, 2026
Get this skill

Free · Opens the source repo

What Cost Tracking does

Cost Tracking is a bash-based skill designed for users who want to monitor and analyze their expenses related to Claude Code usage. By leveraging a local SQLite database, specifically the ~/.claude-cost-tracker/usage.db, this skill allows users to query their token usage, expenditures, and budgets effectively. It is particularly useful for those who have already set up a cost tracking hook or plugin that writes usage data to this database. The skill provides a straightforward way to retrieve detailed cost breakdowns based on various parameters such as projects, tools, sessions, and dates.

Users can ask questions like "How much have I spent?" or "What was the cost for this session?" and receive immediate insights. The skill also supports budget management by allowing users to track spending limits and analyze trends over time. For instance, users can compare today’s expenses with yesterday's or review their spending over the past week. Additionally, it offers the capability to export recent usage records in CSV format, facilitating further analysis or record-keeping.

To utilize this skill, users must ensure that the SQLite3 command-line tool is available and that the usage database exists. If the database is missing, the skill will inform users that a cost tracking setup is required. The expected structure of the usage table includes fields for timestamps, project names, tool names, token counts, costs in USD, and session identifiers, all of which are critical for accurate reporting. By prioritizing the cost_usd column for calculations, this skill provides reliable cost information that reflects the current pricing of models and tools used in Claude Code.

Overall, Cost Tracking is an essential tool for developers and designers who need to keep a close eye on their expenses while using Claude Code, ensuring they stay within budget and understand their usage patterns.

When to use it

Use this skill when you need to analyze your spending on Claude Code, track token usage, or manage budgets effectively.

When not to use it

This skill is not suitable for users without a local SQLite database setup for tracking costs or those who do not require detailed expense analysis.

What you can build with it

Daily Expense Comparison

Quickly compare today's expenses against yesterday's to monitor spending patterns.

Project Cost Analysis

Analyze costs associated with different projects to identify which ones are most resource-intensive.

Budget Management

Track your spending against set budgets to ensure you stay within financial limits.

How to install Cost Tracking

View source

1. Install with the skills CLI

npx skills add affaan-m/ecc/cost-tracking --agent claude-code

2. Or install it manually

Download the skill folder and drop it into ~/.claude/skills/ for all projects, or .claude/skills/ to scope it to one repo. Restart Claude Code so it picks up the new skill.

Anthropic's agentic coding CLI, and the reference implementation of Agent Skills. Drop a skill folder into ~/.claude/skills and Claude Code loads it automatically whenever a task matches the skill's description. Claude Code docs

Inside SKILL.md

Written by affaan-m

コスト追跡

このスキルを使用して、ローカルSQLiteデータベースからClaude Codeのコストと使用履歴を分析します。これは、~/.claude-cost-tracker/usage.dbに使用行を書き込むコスト追跡フックまたはプラグインをすでに持っているユーザーを対象としています。

出典: MayurBhavsarによるコミュニティのPR #1304から救済されました。

使用時期

  • ユーザーが「いくら使いましたか?」「このセッションのコストは?」「トークン使用量は?」と尋ねる場合
  • ユーザーが予算、支出制限、超過、またはコスト管理について言及する場合
  • ユーザーがプロジェクト、ツール、セッション、モデル、または日付ごとのコスト内訳を求める場合
  • ユーザーが今日と昨日を比較したい、または最近のトレンドを確認したい場合
  • ユーザーが最近の使用記録のCSVエクスポートを求める場合

動作方法

まず前提条件を確認します:

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テーブルには通常、ツール呼び出しまたはモデルインタラクションごとに1行が含まれます。列名はトラッカーによって異なりますが、以下の例では次のように仮定します:

意味
timestamp使用イベントのISOタイムスタンプ
projectプロジェクトまたはリポジトリ名
tool_nameツールまたはイベント名
input_tokens記録された場合の入力トークン数
output_tokens記録された場合の出力トークン数
cost_usdUSDで事前計算されたコスト
session_idClaude Codeセッション識別子
modelイベントに使用されたモデル

cost_usdを使用して手動で価格計算するよりも優先します。モデルの価格とキャッシュ価格は時間とともに変化し、トラッカーが各行の価格設定の信頼できる情報源であるべきです。

クイックサマリー

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;
"

過去7日間

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;
"

レポートガイダンス

コストデータを表示する場合、以下を含めます:

  1. 今日の支出と昨日の比較。
  2. 追跡されたデータベース全体の合計支出。
  3. コスト順にランク付けされた上位プロジェクト。
  4. コスト順にランク付けされた上位ツール。
  5. 十分なデータがある場合のセッション数とセッションごとの平均コスト。

少額の場合、通貨を小数点4桁でフォーマットします。大きな金額には2桁で十分です。

アンチパターン

  • cost_usdが存在する場合に生のトークン数からコストを推定しないこと。
  • 確認せずにデータベースが存在すると仮定しないこと。
  • 大規模なデータベースで無制限のSELECT *エクスポートを実行しないこと。
  • ユーザー向けの回答で現在のモデル価格をハードコードしないこと。
  • 任意のコードを実行する未審査のフックやプラグインのインストールを推奨しないこと。

関連

  • /cost-report - 同じデータベースを使用するコマンド形式のレポート。
  • cost-aware-llm-pipeline - モデルルーティングと予算設計のパターン。
  • token-budget-advisor - コンテキストとトークン予算の計画。
  • strategic-compact - 繰り返しのトークン支出を削減するためのコンテキスト圧縮。

Frequently asked questions about Cost Tracking

Similar skills