Guide for creating Claude Code hooks that respond to events during execution. This skill should be used when users want to create or configure hooks for validation, automation, context injection, or workflow control. Triggers on "create a hook", "add a hook", "configure hooks", or "set up automation".
This skill guides the creation of Claude Code hooks that respond to events during Claude's execution, enabling automation, validation, context injection, and workflow customization.
IMPORTANT: Before creating hooks, fetch up-to-date official documentation using WebFetch to ensure accuracy with the latest Claude Code hook APIs and schemas.
Primary references (always fetch these):
https://code.claude.com/docs/en/hooks-guide.md — User-facing hook guide covering setup, common use cases, and troubleshootinghttps://code.claude.com/docs/en/hooks.md — Complete technical reference with all 12 hook events, JSON schemas, and decision controlSupplementary references (fetch when relevant):
https://code.claude.com/docs/en/plugins.md — Plugin hooks distribution, hooks/hooks.json format for distributing hooks via pluginshttps://code.claude.com/docs/en/plugins-reference.md — Technical reference for hooks in plugin contexthttps://code.claude.com/docs/en/sub-agents.md — Hooks in subagent frontmatter, SubagentStart/SubagentStop eventsHooks are executable commands (typically scripts) that run in response to specific Claude Code events. They enable:
Use hooks for:
Use skills for:
Use commands for:
Follow this workflow to create hooks effectively.
Determine whether this should be a project hook (shared with team) or personal hook (just for you).
If unclear, ask: "Should this be a project hook (shared with team) or personal hook (just for you)?"
Project hooks (.claude/settings.json):
.claude/ directoryPersonal hooks (~/.claude/settings.json):
Default behavior if not specified:
.claude/ directory → Project hookBased on the user's goal, select the appropriate hook event:
Common Events:
startup, resume, clear, compact)Additional Events:
manual, auto)See references/hook-events.md for complete event details, use cases, and selection guidance.
For PreToolUse and PostToolUse events, specify which tools trigger the hook:
Exact match: "matcher": "Write" - Only Write tool
Multiple tools: "matcher": "Write|Edit" - Write or Edit tools
Pattern match: "matcher": "Notebook.*" - All Notebook tools
All tools: "matcher": "*" or "matcher": "" - Every tool
MCP tools: "matcher": "mcp__github__.*" - All GitHub MCP tools
For SessionStart: Use "matcher": "startup|resume" for specific sources
For PreCompact: Use "matcher": "manual" or "matcher": "auto"
Other events don't use matchers - omit the matcher field entirely.
Create the hook script that implements the desired behavior.
Script structure:
Exit codes:
0 - Success (stdout shown in transcript, except UserPromptSubmit/SessionStart which adds to context)2 - Blocking error (stderr fed to Claude automatically)Example script structure:
#!/usr/bin/env python3
import json
import sys
# Read input