agentleFS
agenticlukeagenticluke/token-cost-tracker-plus/skill/SKILL.md

cost-tracking

Skill0 starsChanged 3 months agoMITView on GitHub

Track and report Claude Code token use, cost, and budgets from a local SQLite database. Use for questions about cost, spend, usage, tokens, budgets, or breakdowns by project, tool, session, model, or date.

9 other projects use a version of this, 4 of them edited.

Adopted by

Most recently taken up by

The file

Show its rules and text

We split this skill into the rules it gives an agent

41 rules. 0 are common, 36 are distinctive, and 5 are very specific to this project.

  • 0

    Common

    Most of the 9 edited versions we read keep these.

  • 36

    Distinctive

    This author's own choices. Worth a look.

  • 5

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Use this skill to read Claude Code cost and usage data from a local SQLite database.

    line 9
  2. Distinctive:

    Use this skill when the user asks:

    line 21
  3. Distinctive:

    Read local data only.

    line 34
  4. Distinctive:

    Do not send usage data to any outside service.

    line 35
  5. Distinctive:

    Do not install or run an unknown hook or plugin.

    line 36
  6. Distinctive:

    Do not change or delete the database.

    line 37
  7. Distinctive:

    Do not make up missing data.

    line 38
  8. Distinctive:

    Do not show full session IDs unless the user asks.

    line 39
  9. Distinctive:

    Do not treat tracked cost as an invoice.

    line 40
  10. Distinctive:

    If SQLite is missing, say that it is needed to read the database.

    line 51
  11. Distinctive:

    If the database is missing, do not create it.

    line 53
  12. Distinctive:

    If the database cannot be read, report the file error.

    line 55
  13. Very specific:

    If the usage table does not exist, inspect the listed tables and find the one that holds usage rows.

    line 66
  14. Distinctive:

    Use only columns that exist.

    line 81
  15. Very specific:

    Prefer cost_usd when it exists.

    line 83
  16. Distinctive:

    Before a date report:

    line 91
  17. Distinctive:

    Check a few timestamps.

    line 93
  18. Distinctive:

    Use the user's local time if it can be done with clear data.

    line 95
  19. Distinctive:

    If the time zone is not known, say that date totals may differ near midnight.

    line 96
  20. Distinctive:

    Do not show a percent change when yesterday's cost is zero.

    line 132
  21. Distinctive:

    Run this only when both token columns exist:

    line 201
  22. Distinctive:

    If the user gives a budget, use the same date range for both spend and budget.

    line 216
  23. Distinctive:

    Do not claim that a budget is stored unless the database has a budget table or field.

    line 233
  24. Distinctive:

    Ask for the date range and output path if they are not clear.

    line 237
  25. Distinctive:

    Do not export session IDs unless the user needs them.

    line 248
  26. Distinctive:

    Check that SQLite and the database exist.

    line 260
  27. Very specific:

    Check the usage table and its columns.

    line 261
  28. Distinctive:

    Check the timestamp form and time zone.

    line 262
  29. Distinctive:

    When the data allows it, include:

    line 275
  30. Distinctive:

    Keep the report short. Put the main answer first.

    line 283
  31. Distinctive:

    Use four decimal places for amounts under $1.

    line 285
  32. Distinctive:

    Do not assume the database exists.

    line 291
  33. Distinctive:

    Do not assume the table or column names.

    line 292
  34. Very specific:

    Do not use SELECT * for a large report or export.

    line 293
  35. Very specific:

    Do not use current model prices when cost_usd exists.

    line 294
  36. Distinctive:

    Do not mix date ranges when checking a budget.

    line 295
  37. Distinctive:

    Do not count blank session IDs as real sessions.

    line 296
  38. Distinctive:

    Do not compare days without stating the time zone.

    line 297
  39. Distinctive:

    Do not add costs from two databases unless the user asks.

    line 298
  40. Distinctive:

    Do not hide null or missing cost rows.

    line 299
  41. Distinctive:

    Do not suggest an untrusted hook or plugin that can run code.

    line 300

“Common” is measured: kept by at least 6 in 10 of the 9 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Cost Tracking

Use this skill to read Claude Code cost and usage data from a local SQLite database.

This skill is for users who already have a trusted hook or plugin that writes usage data to:

~/.claude-cost-tracker/usage.db

Credit: This skill was recovered from community PR #1304 by MayurBhavsar.

When to Use

Use this skill when the user asks:

  • How much did I spend?
  • What did this session cost?
  • How many tokens did I use?
  • Am I over my budget?
  • Which project, tool, session, or model costs the most?
  • How does today compare with yesterday?
  • What is my recent cost trend?
  • Can I export recent usage as CSV?

Safety Rules

  • Read local data only.
  • Do not send usage data to any outside service.
  • Do not install or run an unknown hook or plugin.
  • Do not change or delete the database.
  • Do not make up missing data.
  • Do not show full session IDs unless the user asks.
  • Do not treat tracked cost as an invoice. It may not match the final bill.

Check the Setup

First, check for SQLite and the database:

command -v sqlite3 >/dev/null && echo "sqlite3 available" || echo "sqlite3 missing"
test -f "$HOME/.claude-cost-tracker/usage.db" && echo "Database found" || echo "Database not found"

If SQLite is missing, say that it is needed to read the database.

If the database is missing, do not create it. Tell the user that local cost tracking is not set up. Suggest that they enable a trusted local cost tracker.

If the database cannot be read, report the file error. Do not change its access rights without clear user approval.

Check the Schema

Trackers may use different table and column names. Check the real schema before running a report:

sqlite3 "$HOME/.claude-cost-tracker/usage.db" ".tables"
sqlite3 "$HOME/.claude-cost-tracker/usage.db" "PRAGMA table_info(usage);"

If the usage table does not exist, inspect the listed tables and find the one that holds usage rows. Do not guess.

A common usage table has one row for each tool call or model event:

ColumnMeaning
timestampDate and time of the event
projectProject or repo name
tool_nameTool or event name
input_tokensInput token count
output_tokensOutput token count
cost_usdStored cost in US dollars
session_idClaude Code session ID
modelModel used for the event

Use only columns that exist.

Prefer cost_usd when it exists. Do not work out cost from token counts unless the user asks and gives a price list. Model prices and cache rules can change.

Some rows may have blank or null values. Group these as Unknown when useful.

Time Rules

SQLite uses UTC for date('now'). Stored times may use UTC or local time.

Before a date report:

  1. Check a few timestamps.
  2. State which time zone the report uses.
  3. Use the user's local time if it can be done with clear data.
  4. If the time zone is not known, say that date totals may differ near midnight.

Also check whether timestamps are text, Unix seconds, or Unix milliseconds. Change the query if needed.

Queries

Quick Summary

sqlite3 "$HOME/.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;
"

Today and Yesterday

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    ROUND(COALESCE(SUM(CASE
      WHEN date(timestamp) = date('now') THEN cost_usd
    END), 0), 4) AS today_usd,
    ROUND(COALESCE(SUM(CASE
      WHEN date(timestamp) = date('now', '-1 day') THEN cost_usd
    END), 0), 4) AS yesterday_usd
  FROM usage;
"

Do not show a percent change when yesterday's cost is zero. Show the dollar change instead.

Cost by Project

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    COALESCE(NULLIF(project, ''), 'Unknown') AS project,
    ROUND(COALESCE(SUM(cost_usd), 0), 4) AS cost_usd,
    COUNT(*) AS calls
  FROM usage
  GROUP BY COALESCE(NULLIF(project, ''), 'Unknown')
  ORDER BY cost_usd DESC
  LIMIT 20;
"

Cost by Tool

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    COALESCE(NULLIF(tool_name, ''), 'Unknown') AS tool,
    ROUND(COALESCE(SUM(cost_usd), 0), 4) AS cost_usd,
    COUNT(*) AS calls
  FROM usage
  GROUP BY COALESCE(NULLIF(tool_name, ''), 'Unknown')
  ORDER BY cost_usd DESC
  LIMIT 20;
"

Last 7 Calendar Days

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    date(timestamp) AS day,
    ROUND(COALESCE(SUM(cost_usd), 0), 4) AS cost_usd,
    COUNT(*) AS calls
  FROM usage
  WHERE timestamp >= datetime('now', '-7 days')
  GROUP BY date(timestamp)
  ORDER BY day DESC;
"

This query skips days with no rows. Say so if the user asks for a daily chart.

Recent Sessions

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    substr(session_id, 1, 8) || '...' AS session,
    MIN(timestamp) AS started,
    MAX(timestamp) AS ended,
    ROUND(COALESCE(SUM(cost_usd), 0), 4) AS cost_usd,
    COUNT(*) AS calls
  FROM usage
  WHERE session_id IS NOT NULL AND session_id != ''
  GROUP BY session_id
  ORDER BY started DESC
  LIMIT 10;
"

Token Totals

Run this only when both token columns exist:

sqlite3 -header -column "$HOME/.claude-cost-tracker/usage.db" "
  SELECT
    COALESCE(SUM(input_tokens), 0) AS input_tokens,
    COALESCE(SUM(output_tokens), 0) AS output_tokens,
    COALESCE(SUM(input_tokens), 0) +
      COALESCE(SUM(output_tokens), 0) AS total_tokens
  FROM usage;
"

Budget Check

If the user gives a budget, use the same date range for both spend and budget.

sqlite3 "$HOME/.claude-cost-tracker/usage.db" "
  SELECT ROUND(COALESCE(SUM(cost_usd), 0), 4)
  FROM usage
  WHERE timestamp >= datetime('now', 'start of month');
"

Report:

  • Budget amount
  • Spend in the same time range
  • Money left, or amount over
  • Percent used when the budget is above zero

Do not claim that a budget is stored unless the database has a budget table or field.

Safe CSV Export

Ask for the date range and output path if they are not clear. Export only the needed columns and rows:

sqlite3 -header -csv "$HOME/.claude-cost-tracker/usage.db" "
  SELECT timestamp, project, tool_name, input_tokens, output_tokens, cost_usd, model
  FROM usage
  WHERE timestamp >= datetime('now', '-7 days')
  ORDER BY timestamp;
" > ./usage-last-7-days.csv

Do not export session IDs unless the user needs them. Do not overwrite a file without clear approval.

Concrete Example

User request:

How much did I spend this week, and which project cost the most?

Steps:

  1. Check that SQLite and the database exist.
  2. Check the usage table and its columns.
  3. Check the timestamp form and time zone.
  4. Sum cost_usd for the chosen week.
  5. Group the same rows by project.
  6. Report the total, top project, date range, and time zone.

Example answer:

You spent $3.4821 from Monday through today. Project `website` cost the most at $2.1044. The report uses UTC dates from the local tracker. These values are tracked estimates and may not match the final bill.

Report Format

When the data allows it, include:

  1. Today's spend and yesterday's spend.
  2. Total spend for the asked date range.
  3. Top projects by cost.
  4. Top tools by cost.
  5. Session count and average cost per session.

Keep the report short. Put the main answer first.

Use four decimal places for amounts under $1. Use two decimal places for larger amounts. State the currency as USD.

Say when data is missing, blank, or incomplete. If no rows match the date range, report zero matching rows. Do not claim that usage was zero unless tracking was active for that whole time.

Common Problems

  • Do not assume the database exists.
  • Do not assume the table or column names.
  • Do not use SELECT * for a large report or export.
  • Do not use current model prices when cost_usd exists.
  • Do not mix date ranges when checking a budget.
  • Do not count blank session IDs as real sessions.
  • Do not compare days without stating the time zone.
  • Do not add costs from two databases unless the user asks.
  • Do not hide null or missing cost rows. Note them in the report.
  • Do not suggest an untrusted hook or plugin that can run code.

Related Skills

  • /cost-report: Command-style reports from the same database.
  • cost-aware-llm-pipeline: Plans model use and budgets.
  • token-budget-advisor: Plans context and token limits.
  • strategic-compact: Cuts repeat token use by making context smaller.

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.