Skill v1.0.0
currentAutomated scan100/100name: hook-automation-patterns description: Claude Code hook patterns for automatic formatting, security scanning, usage measurement, and permission routing. Use when setting up automated behaviors triggered before/after tool use without manual invocation. Covers PreToolUse, PostToolUse, and session lifecycle hooks with ready-to-use JSON configs. id: hook-automation-patterns runtime: claude-only canonical_path: ~/.claude/skills/hook-automation-patterns/SKILL.md updated: 2026-05-18 tags: [hooks, automation, formatting, security, measurement, settings] source: shanraisshan/claude-code-best-practice metadata: version: "1.0.0"
Hook Automation Patterns
Hooks run shell commands automatically on Claude Code events. Zero manual intervention.
Hook Lifecycle
| Event | When it fires | Best used for | |
|---|---|---|---|
PreToolUse | Before any tool call | Validation, permission checks, logging | |
PostToolUse | After any tool call | Formatting, security scan, metrics | |
SessionStart | Session begins | Load context, restore state | |
SessionEnd | Session ends | Compress memory, save state | |
Stop | Agent task completes | Final checks, notifications |
Pattern 1: Auto-Format on Save
Prevent CI failures from formatting issues. Fires after every Edit/Write.
{"hooks": {"PostToolUse": [{"matcher": "Edit|Write","hooks": [{"type": "command","command": "npx prettier --write \"$CLAUDE_TOOL_FILE\" 2>/dev/null || true"}]}]}}
Pattern 2: Security Scanner
Catch secrets and vulnerabilities after every file change.
{"hooks": {"PostToolUse": [{"matcher": "Edit|Write","hooks": [{"type": "command","command": "~/.claude/hooks/security-scanner.sh \"$CLAUDE_TOOL_FILE\""}]}]}}
Pattern 3: On-Demand Safety Hooks (Slash Commands)
# /careful — blocks destructive bash operations# activate before working near production code# /freeze <dir> — blocks edits outside specified directory# scopes agent to a single folder
Pattern 4: Permission Routing
Route expensive/risky Bash operations through an approval gate.
{"hooks": {"PreToolUse": [{"matcher": "Bash","hooks": [{"type": "command","command": "~/.claude/hooks/permission-router.sh \"$CLAUDE_TOOL_INPUT\""}]}]}}
Pattern 5: Skill Usage Measurement
Track which skills fire too rarely (undertriggering) or too often.
{"hooks": {"PreToolUse": [{"matcher": ".*","hooks": [{"type": "command","command": "echo \"$(date -Iseconds),$CLAUDE_TOOL_NAME,$CLAUDE_SESSION_ID\" >> ~/.claude/skill-usage.log 2>/dev/null || true"}]}]}}
Pattern 6: Smart Commit on Edit
Auto-commit file changes in git repos with a generated message.
{"hooks": {"PostToolUse": [{"matcher": "Edit|Write","hooks": [{"type": "command","command": "~/.claude/hooks/smart-commit.sh \"$CLAUDE_TOOL_FILE\""}]}]}}
Hook Environment Variables
| Variable | Content | |
|---|---|---|
$CLAUDE_TOOL_NAME | Tool that fired (Edit, Bash, etc.) | |
$CLAUDE_TOOL_FILE | File path for file-based tools | |
$CLAUDE_TOOL_INPUT | Raw input to the tool | |
$CLAUDE_SESSION_ID | Current session ID | |
$CLAUDE_PROJECT_DIR | Project root directory |
Config Location
| Scope | File | |
|---|---|---|
| Global (all projects) | ~/.claude/settings.local.json | |
| Project-specific | .claude/settings.json in project root |
Project settings override global. Both use the same JSON schema.
Anti-Patterns
- Hooks that fail loudly and block the main agent flow — always add
|| true - Heavy operations in PreToolUse — adds latency to every tool call
- Hooks that modify the file they're scanning — creates loop risk
- Missing
2>/dev/null— stderr from hooks can confuse Claude's output parsing