How Hooks Work
PreToolUse Hook Fires
Before the edit executes, PreToolUse hooks run:
- Doc file warning checks if it’s a non-standard .md file
- Strategic compact suggests manual compaction if needed
Hook Types
ECC uses five hook event types:PreToolUse
When: Before a tool executesCan: Block execution (exit code 2) or warn (stderr)Use cases:
- Block dev servers outside tmux
- Warn about non-standard file creation
- Secret detection in prompts
- Rate limiting validation
PostToolUse
When: After a tool completesCan: Analyze output, trigger followup actionsCannot: Block execution (already completed)Use cases:
- Auto-format code after edits
- Run type checks
- Log PR URLs after creation
- Background build analysis
Stop
When: After each Claude responseUse cases:
- Audit all modified files
- Extract session patterns
- Update metrics
SessionStart
When: Session beginsUse cases:
- Load previous context
- Detect package manager
- Initialize session state
SessionEnd
When: Session endsUse cases:
- Persist session state
- Extract patterns for continuous learning
- Save checkpoints
PreCompact
When: Before context compactionUse cases:
- Save state before compaction
- Log compaction event
- Archive context
Hooks in ECC
PreToolUse Hooks
Block Dev Servers Outside Tmux
Block Dev Servers Outside Tmux
Matcher:
BashBehavior: Blocks npm run dev, pnpm dev, yarn dev, bun run dev outside tmuxExit code: 2 (blocks execution)Why: Ensures you can access server logs later via tmux attachExample:Tmux Reminder for Long Commands
Tmux Reminder for Long Commands
Matcher:
BashBehavior: Warns about long-running commands (tests, builds, docker)Exit code: 0 (warns only)Commands matched:npm install,npm testcargo build,makedocker,pytest,vitest,playwright
Git Push Reminder
Git Push Reminder
Matcher:
BashBehavior: Reminds to review changes before git pushExit code: 0 (warns only)Example:Doc File Warning
Doc File Warning
Matcher:
WriteBehavior: Warns about non-standard .md/.txt filesExit code: 0 (warns only)Allowed files:- README.md, CLAUDE.md, CONTRIBUTING.md
- CHANGELOG.md, LICENSE, SKILL.md
- Files in
docs/orskills/directories
Strategic Compact Suggestion
Strategic Compact Suggestion
Matcher:
Edit|WriteBehavior: Suggests manual /compact every ~50 tool callsExit code: 0 (warns only)Example:PostToolUse Hooks
PR Logger
PR Logger
Matcher:
BashBehavior: Logs PR URL after gh pr createExample:Build Analysis (Background)
Build Analysis (Background)
Matcher:
BashBehavior: Async analysis after build commandsAsync: Yes (doesn’t block)Timeout: 30 secondsCommands matched: npm run build, yarn build, tsc, webpackPrettier Auto-Format
Prettier Auto-Format
Matcher:
EditBehavior: Runs Prettier on JS/TS files after editsFiles matched: .js, .jsx, .ts, .tsxExample:TypeScript Check
TypeScript Check
Matcher:
EditBehavior: Runs tsc --noEmit after editing .ts/.tsx filesExample:Console.log Warning
Console.log Warning
Matcher:
EditBehavior: Warns about console.log statements in edited filesExample:Lifecycle Hooks
Session Start
Session Start
Event:
SessionStartActions:- Loads previous session context from
.claude/session-state.json - Detects package manager (npm, pnpm, yarn, bun)
- Initializes continuous learning observer
Pre-Compact
Pre-Compact
Event:
PreCompactActions:- Saves current state to
.claude/pre-compact-state.json - Archives context for potential recovery
Console.log Audit (Stop)
Console.log Audit (Stop)
Event:
Stop (after each response)Actions:- Scans all modified files for
console.log - Reports findings with file paths and line numbers
Session End
Session End
Event:
SessionEndActions:- Persists session state to
.claude/session-state.json - Evaluates session for extractable patterns
- Updates continuous learning instincts
Hook Input Schema
Hooks receive JSON on stdin:Exit Codes
Hooks communicate via exit codes:| Exit Code | Meaning | Effect |
|---|---|---|
| 0 | Success | Continue execution, show warnings (stderr) |
| 2 | Block | Stop tool execution (PreToolUse only) |
| Other | Error | Logged but does not block |
Writing Custom Hooks
Hooks are Node.js scripts that read JSON from stdin:Add to hooks.json
Common Hook Recipes
Warn About TODO Comments
Block Large File Creation
Auto-Format Python with Ruff
Require Test Files for New Source
Cross-Platform Compatibility
All ECC hooks use Node.js for maximum compatibility across Windows, macOS, and Linux. Avoid bash-specific syntax.Hook Best Practices
Use Exit Code 2 Sparingly
Use Exit Code 2 Sparingly
Blocking hooks (exit 2) should only be used for critical issues like security violations or dev server misconfigurations. Most hooks should warn (stderr) instead.
Keep Hooks Fast
Keep Hooks Fast
Hooks run synchronously (except async hooks). Slow hooks delay tool execution. Keep hook logic under 100ms.
Always Output Original Data
Always Output Original Data
Hooks must
console.log(data) to pass the original input through. Forgetting this breaks the tool chain.Use Async for Slow Operations
Use Async for Slow Operations
Build analysis, background jobs, or slow checks should use
"async": true to avoid blocking.Test Hook Exit Codes
Test Hook Exit Codes
Verify that blocking hooks (exit 2) actually block tool execution, and warning hooks (exit 0) don’t.
Disabling Hooks
To disable a hook, override it in~/.claude/settings.json:
Next Steps
Explore Rules
Learn about always-follow guidelines
Hook Reference
Full catalog of all hooks
Memory Persistence
Session lifecycle hooks for context management
Custom Hooks
Step-by-step guide to building your own hooks