Hooks System: Pre/Post Sampling Architecture
Overview
The Hooks System is Claude Code's extension mechanism for modifying behavior without changing core code. It provides interception points before and after key operations, enabling git tracking, metrics, safety checks, and custom extensions.
Plain text
┌─────────────────────────────────────────────────────────────────────────────┐
│ HOOKS SYSTEM ARCHITECTURE │
├─────────────────────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ HOOK TYPES │ │
│ │ │ │
│ │ PRE-SAMPLING HOOKS │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │ pre_tool │ │ pre_edit │ │ pre_bash │ │ pre_git │ │ │
│ │ │ _sample │ │ _sample │ │ _sample │ │ _sample │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │ │ │ │ │
│ │ ▼ ▼ ▼ ▼ │ │
│ │ Run BEFORE the operation ──► Can cancel, modify, or log │ │
│ │ │ │
│ ├────────────────────────────────────────────────────────────────────│ │
│ │ │ │
│ │ POST-SAMPLING HOOKS │ │
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
│ │ │post_tool │ │post_edit │ │post_bash │ │post_git │ │ │
│ │ │_sample │ │_sample │ │_sample │ │_sample │ │ │
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
│ │ │ │ │ │ │ │
│ │ ▼ ▼ ▼ ▼ │ │
│ │ Run AFTER the operation ──► Record result, update state │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
│ ┌─────────────────────────────────────────────────────────────────────┐ │
│ │ HOOK EXECUTION FLOW │ │
│ │ │ │
│ │ 1. Tool execution requested │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ 2. Run all PRE hooks (sequential) │ │
│ │ ┌────────┐ ┌────────┐ ┌────────┐ │ │
│ │ │pre_tool│──►│pre_edit│──►│pre_bash│──► ... │ │
│ │ │_sample │ │_sample │ │_sample │ │ │
│ │ └────────┘ └────────┘ └────────┘ │ │
│ │ │ │ │
│ │ If any returns │cancel│ ──► Abort operation │ │
│ │ │ │
│ │ ▼ │ │
│ │ 3. Execute the actual tool │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ 4. Run all POST hooks (parallel-safe) │ │
│ │ ┌────────┐ ┌────────┐ ┌────────┐ │ │
│ │ │post_tool│ │post_edit│ │post_git│ │ │
│ │ │_sample │ │_sample │ │_sample │ │ │
│ │ └────────┘ └────────┘ └────────┘ │ │
│ │ │ │ │ │ │
│ │ └────────────┴────────────┘ │ │
│ │ │ │ │
│ │ ▼ │ │
│ │ 5. Continue with next operation │ │
│ │ │ │
│ └─────────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────────┘