5 Hooks That Save Tokens

Copy-paste into ~/.claude/settings.json. Each hook targets a proven token waste pattern.

1. Warn on Excessive File Reads PostToolUse

Claude reads 20+ files per session on average. Each read costs tokens. This hook warns when reads exceed a threshold.

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Read",
      "hooks": [{
        "type": "command",
        "command": "bash -c 'L=/tmp/cc-reads.txt;C=$(($(cat $L 2>/dev/null||echo 0)+1));echo $C>$L;[ $C -ge 15 ]&&echo \"⚠ $C files read. Consider /compact\" >&2'"
      }]
    }]
  }
}

2. Detect Repeated Writes PostToolUse

Writing the same file 3+ times means Claude is stuck in a retry loop — burning tokens for no progress.

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "Write",
      "hooks": [{
        "type": "command",
        "command": "bash -c 'F=$(cat|jq -r .tool_input.file_path//empty);[ -z \"$F\" ]&&exit 0;L=/tmp/cc-writes.txt;echo $F>>$L;C=$(grep -c \"$F\" $L);[ $C -ge 3 ]&&echo \"⚠ $F written $C times\" >&2'"
      }]
    }]
  }
}

3. Block Large File Reads PreToolUse

Reading a 10,000-line file wastes thousands of tokens. This hook blocks reads over a configurable limit.

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Read",
      "hooks": [{
        "type": "command",
        "command": "bash -c 'F=$(cat|jq -r .tool_input.file_path//empty);[ -z \"$F\" ]&&exit 0;[ -f \"$F\" ]||exit 0;L=$(wc -l<\"$F\");[ $L -gt 500 ]&&echo \"BLOCKED: $F has $L lines. Use offset/limit.\" >&2&&exit 2;exit 0'"
      }]
    }]
  }
}

4. Cap Subagent Spawning PreToolUse

Each subagent consumes tokens independently. Without limits, Claude spawns 5+ agents that each burn through quota.

{
  "hooks": {
    "PreToolUse": [{
      "matcher": "Agent",
      "hooks": [{
        "type": "command",
        "command": "bash -c 'L=/tmp/cc-agents.txt;C=$(($(cat $L 2>/dev/null||echo 0)+1));echo $C>$L;[ $C -gt 3 ]&&echo \"BLOCKED: $C agents spawned. Max 3.\" >&2&&exit 2'"
      }]
    }]
  }
}

5. Session Token Budget PostToolUse

Track cumulative tool calls. After 50 calls, warn that the session is getting expensive.

{
  "hooks": {
    "PostToolUse": [{
      "matcher": "",
      "hooks": [{
        "type": "command",
        "command": "bash -c 'L=/tmp/cc-calls.txt;C=$(($(cat $L 2>/dev/null||echo 0)+1));echo $C>$L;[ $C -eq 50 ]&&echo \"⚠ 50 tool calls. Consider /compact or /clear.\" >&2;[ $C -eq 100 ]&&echo \"⚠ 100 calls. Session very expensive.\" >&2'"
      }]
    }]
  }
}

Install All 5 + 690 More

npx @gaebalai/cc-guard

772 hooks total. 8 core safety hooks auto-installed. Token guards available via --examples.

These 5 hooks are from View on GitHub Chapter 4. The book covers 6 more optimization layers: CLAUDE.md structure, cache management, model selection, workflow design, and 32 failure patterns with fixes. Ch.1 free →

Free Token Checkup Effort Calculator CLAUDE.md Guide