CClaude Code Catalog
All Hooks

Cost Alert Monitor

NotificationIntermediateHook Type: notification

Cost Alert Monitor tracks cumulative token usage throughout a Claude Code session using notification events. It maintains a running tally of input and output tokens, calculates estimated costs based on current pricing, and triggers alerts at configurable thresholds (50%, 75%, 90% of budget). When a threshold is crossed, it displays a prominent warning with usage details and remaining budget. The hook also logs per-session cost summaries for historical tracking and monthly roll-up reports.

costbudgettokensmonitoringalerts

Hook Code

#!/bin/bash # Cost Alert Monitor Hook # Tracks token usage and alerts on budget thresholds COST_FILE="${HOME}/.claude/cost-tracker.json" DAILY_BUDGET=5.00 # USD ALERT_THRESHOLDS="0.50 0.75 0.90" # Initialize cost file if missing if [ ! -f "$COST_FILE" ]; then echo '{"date":"","input_tokens":0,"output_tokens":0,"alerts_sent":[]}' > "$COST_FILE" fi TODAY=$(date +%Y-%m-%d) CURRENT_DATE=$(jq -r '.date' "$COST_FILE") # Reset daily counters if new day if [ "$TODAY" != "$CURRENT_DATE" ]; then echo "{"date":"$TODAY","input_tokens":0,"output_tokens":0,"alerts_sent":[]}" > "$COST_FILE" fi # Parse token counts from notification INPUT_TOKENS=$(echo "$NOTIFICATION_DATA" | jq -r '.input_tokens // 0') OUTPUT_TOKENS=$(echo "$NOTIFICATION_DATA" | jq -r '.output_tokens // 0') # Update cumulative counts jq --argjson inp "$INPUT_TOKENS" --argjson out "$OUTPUT_TOKENS" '.input_tokens += $inp | .output_tokens += $out' "$COST_FILE" > "$COST_FILE.tmp" && mv "$COST_FILE.tmp" "$COST_FILE" # Calculate estimated cost (Claude Sonnet pricing) TOTAL_INPUT=$(jq '.input_tokens' "$COST_FILE") TOTAL_OUTPUT=$(jq '.output_tokens' "$COST_FILE") COST=$(echo "scale=4; $TOTAL_INPUT * 0.000003 + $TOTAL_OUTPUT * 0.000015" | bc) RATIO=$(echo "scale=4; $COST / $DAILY_BUDGET" | bc) # Check thresholds for THRESHOLD in $ALERT_THRESHOLDS; do ALREADY=$(jq -r --arg t "$THRESHOLD" '.alerts_sent | index($t) // empty' "$COST_FILE") if [ -z "$ALREADY" ] && [ "$(echo "$RATIO >= $THRESHOLD" | bc)" -eq 1 ]; then PCT=$(echo "scale=0; $THRESHOLD * 100 / 1" | bc) echo "COST ALERT: ${PCT}% of daily budget reached ($$COST / $$DAILY_BUDGET)" echo " Input: $TOTAL_INPUT tokens | Output: $TOTAL_OUTPUT tokens" jq --arg t "$THRESHOLD" '.alerts_sent += [$t]' "$COST_FILE" > "$COST_FILE.tmp" && mv "$COST_FILE.tmp" "$COST_FILE" fi done exit 0

Add this hook to your Claude Code settings or .claude/settings.json to activate.

Terminal Preview

Cost Alert Monitor

About Cost Alert Monitor

Claude Code hooks let you run custom shell commands automatically in response to specific events during Claude's operation. Cost Alert Monitor is a Notification hook at the Intermediate level that automates tasks at key moments in your development workflow, reducing manual steps and enforcing consistency across your team.

Related Hooks