Skip to main content
Claude Code hooks make cost tracking fully automatic — every session starts, logs AI usage after each response, and closes cleanly when you’re done, all without changing how you use Claude Code.
1

Install CostHQ

2

Add hooks to your Claude Code settings

Add the following to .claude/settings.json for project-level tracking, or to ~/.claude/settings.json to track every Claude Code session globally:
The || true on every command means tracking failures never interrupt your work.
3

You're done

Every Claude Code session now automatically tracks costs. Open the dashboard at any time with cs dashboard — it runs locally at http://localhost:3737.

What Gets Tracked

  • AI tokens & cost — estimated from the conversation transcript, auto-priced per model
  • Git commits — every commit Claude makes during the session
  • Files changed — all file modifications between session start and end
  • Duration — how long the session lasted
  • Agent name — shows “Claude Code” in the dashboard for filtering

How cs auto-log Works

The Stop hook fires after each Claude response and calls cs auto-log, which:
  1. Reads hook input JSON from stdin (which includes transcript_path)
  2. Parses the Claude Code conversation transcript (JSONL format)
  3. Tracks its position so it only counts new messages since the last call
  4. Estimates token counts from conversation content (~1 token per 4 characters)
  5. Auto-calculates cost using the built-in pricing table for the specified model
  6. Logs everything to the active CostHQ session

cs auto-log Options

Update --model in the Stop hook to match what you’re actually using:

Alternative: CLAUDE.md Instructions

If you prefer Claude to handle tracking as part of its own workflow instead of using hooks, add the following to your CLAUDE.md:

Alternative: Manual CLI

You can also run the session lifecycle yourself alongside Claude Code:

Multi-Agent Tracking

If you run Claude Code alongside OpenClaw or other agents, the --agent flag keeps costs separated in the dashboard. Each agent gets its own cost breakdown so you always know which tool spent what.

Troubleshooting

The --close-stale flag in the SessionStart hook handles this automatically. If you see this error in a manual workflow, run:
Run claude --debug to view hook execution logs. Verify that cs is on your PATH by running which cs (macOS/Linux) or where cs (Windows) in a fresh terminal.
cs auto-log estimates approximately 1 token per 4 characters from the transcript. The estimate is consistent across calls, but it is approximate. For exact token counts, replace the Stop hook with explicit cs log-ai calls using real token values from your API responses.
Replace 2>/dev/null with 2>NUL in your hook commands, and use the full path to the cs binary: