<< All versions

Skill v1.0.0

currentAutomated scan100/100
evgeniy-mikhailove/rlm-claude-code-os/hook-automation-patterns
──Details
PublishedNovember 15, 2026 at 02:24 AM
Content Hashsha256:59dcb026f9efe5bf...
Git SHA0618f6138c74
──Files
Files (1 file, 4.0 KB)
SKILL.md4.0 KBactive
SKILL.md · 153 lines · 4.0 KB

name: 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

EventWhen it firesBest used for
PreToolUseBefore any tool callValidation, permission checks, logging
PostToolUseAfter any tool callFormatting, security scan, metrics
SessionStartSession beginsLoad context, restore state
SessionEndSession endsCompress memory, save state
StopAgent task completesFinal checks, notifications

Pattern 1: Auto-Format on Save

Prevent CI failures from formatting issues. Fires after every Edit/Write.

json
{
"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.

json
{
"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)

bash
# /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.

json
{
"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.

json
{
"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.

json
{
"hooks": {
"PostToolUse": [{
"matcher": "Edit|Write",
"hooks": [{
"type": "command",
"command": "~/.claude/hooks/smart-commit.sh \"$CLAUDE_TOOL_FILE\""
}]
}]
}
}

Hook Environment Variables

VariableContent
$CLAUDE_TOOL_NAMETool that fired (Edit, Bash, etc.)
$CLAUDE_TOOL_FILEFile path for file-based tools
$CLAUDE_TOOL_INPUTRaw input to the tool
$CLAUDE_SESSION_IDCurrent session ID
$CLAUDE_PROJECT_DIRProject root directory

Config Location

ScopeFile
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
All versions