Hook Design Pattern Reference

Hook Event Types

EventTrigger TimingTypical Use Case
PreToolUseBefore tool call, can intercept or modify parametersBlock dangerous commands, restrict file access scope
PostToolUseImmediately after tool completesAuto-formatting, telemetry recording
StopAfter main Agent completes responseType checking, test coverage reporting
UserPromptSubmitBefore user submits PromptInject additional context, injection detection
SessionStartAt session startEnvironment check, state loading, greeting message
SessionEndAt session endSave progress, send notifications, cleanup temporary files
SubagentStopWhen Subagent completesTrigger progress sync, record subtask cost
FileChangedWhen file changesTrigger incremental testing

Exit Code Conventions

exit 0   → Success, continue (completely silent)
exit 2   → Failure, feedback error to Agent, Agent continues to fix
exit other → Failure, no feedback to Agent (non-blocking, silent failure)

Key Principle: Success is always silent. 4000 lines of pass logs cause Agent to lose task focus and start discussing test files instead of completing the task.

4 Handler Types

TypeSuitable ScenarioCharacteristics
Command (Shell)Deterministic operations: formatting, type checking, permission validationFast execution, deterministic results, easy debugging, lowest cost
Prompt (LLM)Semantic judgment: code quality evaluation, subtle violation detectionUnderstands semantics, but high cost, slow
Agent (Child Agent)Complex verification: multi-step, needs to read multiple filesFull tool access, can explore autonomously, credible results
HTTP (External Service)System integration: trigger CI, notify Slack, update JiraIntegration with enterprise systems

Selection Decision Tree:

Need "semantic understanding"?
  → Yes: Use Prompt or Agent type
  → No: Use Command type (faster and cheaper)

Need to read multiple files or execute multiple steps?
  → Yes: Use Agent type
  → No: Use Prompt type

Need to trigger external systems?
  → Yes: Use HTTP type

Choosing Hooks by Failure Type

Common Team FailuresCorresponding Hook EventHandler TypePriority
Type errors committedStopCommand (tsc / mypy / go vet)⭐⭐⭐ Immediate
.env file modifiedPreToolUseCommand (file path check)⭐⭐⭐ Immediate
Inconsistent code formattingPostToolUseCommand (lint:fix)⭐⭐⭐ Immediate
Architecture violations not detectedPostToolUseAgent (architecture constraint check)⭐⭐ This Week
Agent finishes earlyStopCommand (check progress.json)⭐⭐ This Week
Sub-agent didn't record progressSubagentStopHTTP (trigger progress sync)⭐ This Month
Session ended without committing progressSessionEndCommand (git commit progress)⭐ This Month

settings.json Configuration Example

{
  "hooks": {
    "Stop": [{
      "matcher": "",
      "hooks": [
        { "type": "command", "command": ".claude/hooks/stop-typecheck.sh" }
      ]
    }],
    "PreToolUse": [{
      "matcher": "Bash|Edit|Write",
      "hooks": [
        { "type": "command", "command": ".claude/hooks/pre-protect-env.sh" }
      ]
    }],
    "PostToolUse": [{
      "matcher": "Edit|Write",
      "hooks": [
        { "type": "command", "command": ".claude/hooks/post-format.sh" }
      ]
    }]
  }
}

Minimum Viable Hook Set (Day 1)

Top three highest ROI Hooks:

  1. Stop Hook — Type Checking: Ensure code is type-correct every time Agent completes
  2. PostToolUse Hook — Auto Formatting: Ensure consistent code style, completely silent on success
  3. PreToolUse Hook — Protect Sensitive Files: Prevent access to .env, secret, etc.

These three Hooks cover 80% of common quality issues and are the fastest way to establish a Harness.