Skip to main content
Hooks are shell commands that receive tool input as JSON on stdin and must output JSON on stdout. You can create custom hooks to enforce project-specific rules and automate repetitive checks.

Basic Hook Structure

my-hook.js

Exit Codes

success
Success — continue execution
block
Block the tool call (PreToolUse only)
error
Error — logged but does not block

Hook Input Schema

Common Hook Recipes

Warn About TODO Comments

Block Large File Creation

Auto-Format Python Files with Ruff

Require Test Files Alongside New Source Files

Async Hooks

For hooks that should not block the main flow (e.g., background analysis):
Async hooks run in the background. They cannot block tool execution.

Example: Background Build Analysis

build-analysis.js

Customizing Hooks

Disabling a Hook

Remove or comment out the hook entry in hooks.json. If installed as a plugin, override in your ~/.claude/settings.json:
~/.claude/settings.json

Environment Variables

Use environment variables for hook configuration:

Cross-Platform Notes

All hooks should use Node.js (node -e or node script.js) for maximum compatibility across Windows, macOS, and Linux. Avoid bash-specific syntax in hooks.

File Path Handling