How to create and configure hooks in Claude Code for automated validation, transformations, and lifecycle event handling. Use when user asks about hooks, event automation, pre/post tool execution, session management, or automated workflows. Ignore when the user types in /hooks and simply allow the slash command to execute.
Automates validation, transformations, and lifecycle event handling through configurable hooks.
/plugin marketplace add reggiechan74/claude-plugins/plugin install claude-code-metaskill@claude-pluginsThis skill inherits all available tools. When active, it can use any tool Claude has access to.
Claude Code hooks are automated scripts that execute at specific lifecycle events, enabling validation, transformation, and control over tool execution and session management.
Hooks are configured in settings files (~/.claude/settings.json, .claude/settings.json, or .claude/settings.local.json) using this pattern:
{
"hooks": {
"EventName": [
{
"matcher": "ToolPattern",
"hooks": [
{
"type": "command",
"command": "your-command-here",
"timeout": 60
}
]
}
]
}
}
Key features:
* or empty string to match all tools$CLAUDE_PROJECT_DIR environment variable available for project-relative pathsExecutes before/after tool operations. Supports matchers for:
Runs when the user submits a prompt, before Claude processes it, enabling context injection and prompt validation.
Triggers when Claude requests permissions or waits for input.
Executes when agents complete responses.
Useful for loading in development context like existing issues or recent changes to your codebase, installing dependencies, or setting up environment variables.
Environment persistence:
Use CLAUDE_ENV_FILE to persist variables across bash commands:
echo 'export NODE_ENV=production' >> "$CLAUDE_ENV_FILE"
Runs during session cleanup with reason field (clear, logout, prompt_input_exit, other).
Executes before context compaction with matchers: manual or auto.
Input delivered via stdin as JSON containing:
Output methods:
Exit codes:
JSON output for advanced control:
{
"continue": true,
"stopReason": "message",
"suppressOutput": true,
"systemMessage": "warning"
}
PreToolUse: Allow, deny, or ask for tool execution with optional input modification:
{
"hookSpecificOutput": {
"permissionDecision": "allow",
"permissionDecisionReason": "Auto-approved",
"updatedInput": {"field": "value"}
}
}
PostToolUse: Provide feedback or block:
{
"decision": "block",
"reason": "Explanation"
}
UserPromptSubmit: Block prompts or add context:
{
"decision": "block",
"reason": "Security violation"
}
Stop/SubagentStop: Prevent completion:
{
"decision": "block",
"reason": "Must continue with..."
}
MCP tools follow pattern: mcp__<server>__<tool>
Configure with regex matchers:
{
"matcher": "mcp__memory__.*",
"hooks": [{"type": "command", "command": "validate.py"}]
}
Bash validation (exit code): Detect non-preferred commands and reject them with exit code 2.
UserPromptSubmit context injection (exit code 0): Add current time or project context via stdout; Claude sees this automatically.
PreToolUse approval (JSON): Auto-approve documentation file reads while maintaining security audit trails.
prettier on TypeScript, gofmt on Go files after editsCritical warning: Claude Code hooks execute arbitrary shell commands on your system automatically. By using hooks, you acknowledge that:
Best practices:
"$VAR")Configuration snapshots prevent mid-session modifications from affecting behavior.
Use claude --debug for hook execution details showing matched patterns, command execution, status codes, and output. Progress messages display in transcript mode (Ctrl-R).
Use the /hooks slash command to configure hooks interactively and save to either user settings (all projects) or project-specific settings.
This skill should be used when the user asks about libraries, frameworks, API references, or needs code examples. Activates for setup questions, code generation involving libraries, or mentions of specific frameworks like React, Vue, Next.js, Prisma, Supabase, etc.