Skip to main content
Hooks are event-driven automations that fire before or after Claude Code tool executions. They enforce code quality, catch mistakes early, and automate repetitive checks.

How Hooks Work

1

PreToolUse hooks

Run before the tool executes. They can block (exit code 2) or warn (stderr without blocking).
2

PostToolUse hooks

Run after the tool completes. They can analyze output but cannot block.
3

Stop hooks

Run after each Claude response.
4

SessionStart/SessionEnd hooks

Run at session lifecycle boundaries.
5

PreCompact hooks

Run before context compaction, useful for saving state.

Hook Types

PreToolUse Hooks

PostToolUse Hooks

Lifecycle Hooks

Hook Configuration

Hooks are defined in hooks.json in your plugin directory. Here’s the complete configuration:
hooks.json

Hook Input Schema

Hooks receive tool information as JSON on stdin:

Exit Codes

  • 0 — Success (continue execution)
  • 2 — Block the tool call (PreToolUse only)
  • Other non-zero — Error (logged but does not block)

Async Hooks

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

Cross-Platform Notes

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

Next Steps

Memory Persistence

Learn about session lifecycle hooks

Strategic Compact

Understand compaction suggestions

Custom Hooks

Create your own hooks