Hook Development

Build event-driven automation for Claude Code with intelligent hooks

✨ The solution you've been looking for

Verified
Tested and verified by our team
56596 Stars

This skill should be used when the user asks to "create a hook", "add a PreToolUse/PostToolUse/Stop hook", "validate tool use", "implement prompt-based hooks", "use ${CLAUDE_PLUGIN_ROOT}", "set up event-driven automation", "block dangerous commands", or mentions hook events (PreToolUse, PostToolUse, Stop, SubagentStop, SessionStart, SessionEnd, UserPromptSubmit, PreCompact, Notification). Provides comprehensive guidance for creating and implementing Claude Code plugin hooks with focus on advanced prompt-based hooks API.

claude-code hooks automation validation plugin-development event-driven workflow security
Repository

See It In Action

Interactive preview & real-world examples

Live Demo
Skill Demo Animation

AI Conversation Simulator

See how users interact with this skill

User Prompt

Create a hook to validate file write operations and block access to sensitive files like .env

Skill Processing

Analyzing request...

Agent Response

A PreToolUse hook that checks file paths for security issues and provides approval/denial decisions with explanations

Quick Start (3 Steps)

Get up and running in minutes

1

Install

claude-code skill install hook-development

claude-code skill install hook-development
2

Config

3

First Trigger

@hook-development help

Commands

CommandDescriptionRequired Args
@hook-development validate-tool-executionImplement PreToolUse hooks to validate and secure tool calls before executionNone
@hook-development enforce-completion-standardsUse Stop hooks to ensure tasks meet quality criteria before agent completionNone
@hook-development load-project-contextSet up SessionStart hooks to automatically load project-specific context and environmentNone

Typical Use Cases

Validate Tool Execution

Implement PreToolUse hooks to validate and secure tool calls before execution

Enforce Completion Standards

Use Stop hooks to ensure tasks meet quality criteria before agent completion

Load Project Context

Set up SessionStart hooks to automatically load project-specific context and environment

Overview

Hook Development for Claude Code Plugins

Overview

Hooks are event-driven automation scripts that execute in response to Claude Code events. Use hooks to validate operations, enforce policies, add context, and integrate external tools into workflows.

Key capabilities:

  • Validate tool calls before execution (PreToolUse)
  • React to tool results (PostToolUse)
  • Enforce completion standards (Stop, SubagentStop)
  • Load project context (SessionStart)
  • Automate workflows across the development lifecycle

Hook Types

Use LLM-driven decision making for context-aware validation:

1{
2  "type": "prompt",
3  "prompt": "Evaluate if this tool use is appropriate: $TOOL_INPUT",
4  "timeout": 30
5}

Supported events: Stop, SubagentStop, UserPromptSubmit, PreToolUse

Benefits:

  • Context-aware decisions based on natural language reasoning
  • Flexible evaluation logic without bash scripting
  • Better edge case handling
  • Easier to maintain and extend

Command Hooks

Execute bash commands for deterministic checks:

1{
2  "type": "command",
3  "command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/validate.sh",
4  "timeout": 60
5}

Use for:

  • Fast deterministic validations
  • File system operations
  • External tool integrations
  • Performance-critical checks

Hook Configuration Formats

Plugin hooks.json Format

For plugin hooks in hooks/hooks.json, use wrapper format:

1{
2  "description": "Brief explanation of hooks (optional)",
3  "hooks": {
4    "PreToolUse": [...],
5    "Stop": [...],
6    "SessionStart": [...]
7  }
8}

Key points:

  • description field is optional
  • hooks field is required wrapper containing actual hook events
  • This is the plugin-specific format

Example:

 1{
 2  "description": "Validation hooks for code quality",
 3  "hooks": {
 4    "PreToolUse": [
 5      {
 6        "matcher": "Write",
 7        "hooks": [
 8          {
 9            "type": "command",
10            "command": "${CLAUDE_PLUGIN_ROOT}/hooks/validate.sh"
11          }
12        ]
13      }
14    ]
15  }
16}

Settings Format (Direct)

For user settings in .claude/settings.json, use direct format:

1{
2  "PreToolUse": [...],
3  "Stop": [...],
4  "SessionStart": [...]
5}

Key points:

  • No wrapper - events directly at top level
  • No description field
  • This is the settings format

Important: The examples below show the hook event structure that goes inside either format. For plugin hooks.json, wrap these in {"hooks": {...}}.

Hook Events

PreToolUse

Execute before any tool runs. Use to approve, deny, or modify tool calls.

Example (prompt-based):

 1{
 2  "PreToolUse": [
 3    {
 4      "matcher": "Write|Edit",
 5      "hooks": [
 6        {
 7          "type": "prompt",
 8          "prompt": "Validate file write safety. Check: system paths, credentials, path traversal, sensitive content. Return 'approve' or 'deny'."
 9        }
10      ]
11    }
12  ]
13}

Output for PreToolUse:

1{
2  "hookSpecificOutput": {
3    "permissionDecision": "allow|deny|ask",
4    "updatedInput": {"field": "modified_value"}
5  },
6  "systemMessage": "Explanation for Claude"
7}

PostToolUse

Execute after tool completes. Use to react to results, provide feedback, or log.

Example:

 1{
 2  "PostToolUse": [
 3    {
 4      "matcher": "Edit",
 5      "hooks": [
 6        {
 7          "type": "prompt",
 8          "prompt": "Analyze edit result for potential issues: syntax errors, security vulnerabilities, breaking changes. Provide feedback."
 9        }
10      ]
11    }
12  ]
13}

Output behavior:

  • Exit 0: stdout shown in transcript
  • Exit 2: stderr fed back to Claude
  • systemMessage included in context

Stop

Execute when main agent considers stopping. Use to validate completeness.

Example:

 1{
 2  "Stop": [
 3    {
 4      "matcher": "*",
 5      "hooks": [
 6        {
 7          "type": "prompt",
 8          "prompt": "Verify task completion: tests run, build succeeded, questions answered. Return 'approve' to stop or 'block' with reason to continue."
 9        }
10      ]
11    }
12  ]
13}

Decision output:

1{
2  "decision": "approve|block",
3  "reason": "Explanation",
4  "systemMessage": "Additional context"
5}

SubagentStop

Execute when subagent considers stopping. Use to ensure subagent completed its task.

Similar to Stop hook, but for subagents.

UserPromptSubmit

Execute when user submits a prompt. Use to add context, validate, or block prompts.

Example:

 1{
 2  "UserPromptSubmit": [
 3    {
 4      "matcher": "*",
 5      "hooks": [
 6        {
 7          "type": "prompt",
 8          "prompt": "Check if prompt requires security guidance. If discussing auth, permissions, or API security, return relevant warnings."
 9        }
10      ]
11    }
12  ]
13}

SessionStart

Execute when Claude Code session begins. Use to load context and set environment.

Example:

 1{
 2  "SessionStart": [
 3    {
 4      "matcher": "*",
 5      "hooks": [
 6        {
 7          "type": "command",
 8          "command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/load-context.sh"
 9        }
10      ]
11    }
12  ]
13}

Special capability: Persist environment variables using $CLAUDE_ENV_FILE:

1echo "export PROJECT_TYPE=nodejs" >> "$CLAUDE_ENV_FILE"

See examples/load-context.sh for complete example.

SessionEnd

Execute when session ends. Use for cleanup, logging, and state preservation.

PreCompact

Execute before context compaction. Use to add critical information to preserve.

Notification

Execute when Claude sends notifications. Use to react to user notifications.

Hook Output Format

Standard Output (All Hooks)

1{
2  "continue": true,
3  "suppressOutput": false,
4  "systemMessage": "Message for Claude"
5}
  • continue: If false, halt processing (default true)
  • suppressOutput: Hide output from transcript (default false)
  • systemMessage: Message shown to Claude

Exit Codes

  • 0 - Success (stdout shown in transcript)
  • 2 - Blocking error (stderr fed back to Claude)
  • Other - Non-blocking error

Hook Input Format

All hooks receive JSON via stdin with common fields:

1{
2  "session_id": "abc123",
3  "transcript_path": "/path/to/transcript.txt",
4  "cwd": "/current/working/dir",
5  "permission_mode": "ask|allow",
6  "hook_event_name": "PreToolUse"
7}

Event-specific fields:

  • PreToolUse/PostToolUse: tool_name, tool_input, tool_result
  • UserPromptSubmit: user_prompt
  • Stop/SubagentStop: reason

Access fields in prompts using $TOOL_INPUT, $TOOL_RESULT, $USER_PROMPT, etc.

Environment Variables

Available in all command hooks:

  • $CLAUDE_PROJECT_DIR - Project root path
  • $CLAUDE_PLUGIN_ROOT - Plugin directory (use for portable paths)
  • $CLAUDE_ENV_FILE - SessionStart only: persist env vars here
  • $CLAUDE_CODE_REMOTE - Set if running in remote context

Always use ${CLAUDE_PLUGIN_ROOT} in hook commands for portability:

1{
2  "type": "command",
3  "command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/validate.sh"
4}

Plugin Hook Configuration

In plugins, define hooks in hooks/hooks.json:

 1{
 2  "PreToolUse": [
 3    {
 4      "matcher": "Write|Edit",
 5      "hooks": [
 6        {
 7          "type": "prompt",
 8          "prompt": "Validate file write safety"
 9        }
10      ]
11    }
12  ],
13  "Stop": [
14    {
15      "matcher": "*",
16      "hooks": [
17        {
18          "type": "prompt",
19          "prompt": "Verify task completion"
20        }
21      ]
22    }
23  ],
24  "SessionStart": [
25    {
26      "matcher": "*",
27      "hooks": [
28        {
29          "type": "command",
30          "command": "bash ${CLAUDE_PLUGIN_ROOT}/scripts/load-context.sh",
31          "timeout": 10
32        }
33      ]
34    }
35  ]
36}

Plugin hooks merge with user’s hooks and run in parallel.

Matchers

Tool Name Matching

Exact match:

1"matcher": "Write"

Multiple tools:

1"matcher": "Read|Write|Edit"

Wildcard (all tools):

1"matcher": "*"

Regex patterns:

1"matcher": "mcp__.*__delete.*"  // All MCP delete tools

Note: Matchers are case-sensitive.

Common Patterns

 1// All MCP tools
 2"matcher": "mcp__.*"
 3
 4// Specific plugin's MCP tools
 5"matcher": "mcp__plugin_asana_.*"
 6
 7// All file operations
 8"matcher": "Read|Write|Edit"
 9
10// Bash commands only
11"matcher": "Bash"

Security Best Practices

Input Validation

Always validate inputs in command hooks:

 1#!/bin/bash
 2set -euo pipefail
 3
 4input=$(cat)
 5tool_name=$(echo "$input" | jq -r '.tool_name')
 6
 7# Validate tool name format
 8if [[ ! "$tool_name" =~ ^[a-zA-Z0-9_]+$ ]]; then
 9  echo '{"decision": "deny", "reason": "Invalid tool name"}' >&2
10  exit 2
11fi

Path Safety

Check for path traversal and sensitive files:

 1file_path=$(echo "$input" | jq -r '.tool_input.file_path')
 2
 3# Deny path traversal
 4if [[ "$file_path" == *".."* ]]; then
 5  echo '{"decision": "deny", "reason": "Path traversal detected"}' >&2
 6  exit 2
 7fi
 8
 9# Deny sensitive files
10if [[ "$file_path" == *".env"* ]]; then
11  echo '{"decision": "deny", "reason": "Sensitive file"}' >&2
12  exit 2
13fi

See examples/validate-write.sh and examples/validate-bash.sh for complete examples.

Quote All Variables

1# GOOD: Quoted
2echo "$file_path"
3cd "$CLAUDE_PROJECT_DIR"
4
5# BAD: Unquoted (injection risk)
6echo $file_path
7cd $CLAUDE_PROJECT_DIR

Set Appropriate Timeouts

1{
2  "type": "command",
3  "command": "bash script.sh",
4  "timeout": 10
5}

Defaults: Command hooks (60s), Prompt hooks (30s)

Performance Considerations

Parallel Execution

All matching hooks run in parallel:

 1{
 2  "PreToolUse": [
 3    {
 4      "matcher": "Write",
 5      "hooks": [
 6        {"type": "command", "command": "check1.sh"},  // Parallel
 7        {"type": "command", "command": "check2.sh"},  // Parallel
 8        {"type": "prompt", "prompt": "Validate..."}   // Parallel
 9      ]
10    }
11  ]
12}

Design implications:

  • Hooks don’t see each other’s output
  • Non-deterministic ordering
  • Design for independence

Optimization

  1. Use command hooks for quick deterministic checks
  2. Use prompt hooks for complex reasoning
  3. Cache validation results in temp files
  4. Minimize I/O in hot paths

Temporarily Active Hooks

Create hooks that activate conditionally by checking for a flag file or configuration:

Pattern: Flag file activation

 1#!/bin/bash
 2# Only active when flag file exists
 3FLAG_FILE="$CLAUDE_PROJECT_DIR/.enable-strict-validation"
 4
 5if [ ! -f "$FLAG_FILE" ]; then
 6  # Flag not present, skip validation
 7  exit 0
 8fi
 9
10# Flag present, run validation
11input=$(cat)
12# ... validation logic ...

Pattern: Configuration-based activation

 1#!/bin/bash
 2# Check configuration for activation
 3CONFIG_FILE="$CLAUDE_PROJECT_DIR/.claude/plugin-config.json"
 4
 5if [ -f "$CONFIG_FILE" ]; then
 6  enabled=$(jq -r '.strictMode // false' "$CONFIG_FILE")
 7  if [ "$enabled" != "true" ]; then
 8    exit 0  # Not enabled, skip
 9  fi
10fi
11
12# Enabled, run hook logic
13input=$(cat)
14# ... hook logic ...

Use cases:

  • Enable strict validation only when needed
  • Temporary debugging hooks
  • Project-specific hook behavior
  • Feature flags for hooks

Best practice: Document activation mechanism in plugin README so users know how to enable/disable temporary hooks.

Hook Lifecycle and Limitations

Hooks Load at Session Start

Important: Hooks are loaded when Claude Code session starts. Changes to hook configuration require restarting Claude Code.

Cannot hot-swap hooks:

  • Editing hooks/hooks.json won’t affect current session
  • Adding new hook scripts won’t be recognized
  • Changing hook commands/prompts won’t update
  • Must restart Claude Code: exit and run claude again

To test hook changes:

  1. Edit hook configuration or scripts
  2. Exit Claude Code session
  3. Restart: claude or cc
  4. New hook configuration loads
  5. Test hooks with claude --debug

Hook Validation at Startup

Hooks are validated when Claude Code starts:

  • Invalid JSON in hooks.json causes loading failure
  • Missing scripts cause warnings
  • Syntax errors reported in debug mode

Use /hooks command to review loaded hooks in current session.

Debugging Hooks

Enable Debug Mode

1claude --debug

Look for hook registration, execution logs, input/output JSON, and timing information.

Test Hook Scripts

Test command hooks directly:

1echo '{"tool_name": "Write", "tool_input": {"file_path": "/test"}}' | \
2  bash ${CLAUDE_PLUGIN_ROOT}/scripts/validate.sh
3
4echo "Exit code: $?"

Validate JSON Output

Ensure hooks output valid JSON:

1output=$(./your-hook.sh < test-input.json)
2echo "$output" | jq .

Quick Reference

Hook Events Summary

EventWhenUse For
PreToolUseBefore toolValidation, modification
PostToolUseAfter toolFeedback, logging
UserPromptSubmitUser inputContext, validation
StopAgent stoppingCompleteness check
SubagentStopSubagent doneTask validation
SessionStartSession beginsContext loading
SessionEndSession endsCleanup, logging
PreCompactBefore compactPreserve context
NotificationUser notifiedLogging, reactions

Best Practices

DO:

  • ✅ Use prompt-based hooks for complex logic
  • ✅ Use ${CLAUDE_PLUGIN_ROOT} for portability
  • ✅ Validate all inputs in command hooks
  • ✅ Quote all bash variables
  • ✅ Set appropriate timeouts
  • ✅ Return structured JSON output
  • ✅ Test hooks thoroughly

DON’T:

  • ❌ Use hardcoded paths
  • ❌ Trust user input without validation
  • ❌ Create long-running hooks
  • ❌ Rely on hook execution order
  • ❌ Modify global state unpredictably
  • ❌ Log sensitive information

Additional Resources

Reference Files

For detailed patterns and advanced techniques, consult:

  • references/patterns.md - Common hook patterns (8+ proven patterns)
  • references/migration.md - Migrating from basic to advanced hooks
  • references/advanced.md - Advanced use cases and techniques

Example Hook Scripts

Working examples in examples/:

  • validate-write.sh - File write validation example
  • validate-bash.sh - Bash command validation example
  • load-context.sh - SessionStart context loading example

Utility Scripts

Development tools in scripts/:

  • validate-hook-schema.sh - Validate hooks.json structure and syntax
  • test-hook.sh - Test hooks with sample input before deployment
  • hook-linter.sh - Check hook scripts for common issues and best practices

External Resources

Implementation Workflow

To implement hooks in a plugin:

  1. Identify events to hook into (PreToolUse, Stop, SessionStart, etc.)
  2. Decide between prompt-based (flexible) or command (deterministic) hooks
  3. Write hook configuration in hooks/hooks.json
  4. For command hooks, create hook scripts
  5. Use ${CLAUDE_PLUGIN_ROOT} for all file references
  6. Validate configuration with scripts/validate-hook-schema.sh hooks/hooks.json
  7. Test hooks with scripts/test-hook.sh before deployment
  8. Test in Claude Code with claude --debug
  9. Document hooks in plugin README

Focus on prompt-based hooks for most use cases. Reserve command hooks for performance-critical or deterministic checks.

What Users Are Saying

Real feedback from the community

Environment Matrix

Dependencies

Claude Code runtime environment
bash shell for command hooks
jq for JSON processing (recommended)

Framework Support

Claude Code Plugin API ✓ (required) Prompt-based hooks ✓ (recommended) Command hooks ✓

Context Window

Token Usage ~1K-3K tokens for hook configurations, varies with prompt complexity

Security & Privacy

Information

Author
anthropics
Updated
2026-01-30
Category
productivity-tools