What Are Hooks?
Hooks are shell commands that execute automatically in response to agent lifecycle events. They enable:- Session initialization - Set up context at start
- Tool interception - Validate or modify tool calls
- Event reactions - Respond to specific actions
Hook Types
Session Hooks
Execute when a session starts or ends:Tool Hooks
Execute before or after specific tool calls:Configuration
Location
Hooks are configured in.claude/settings.json:
Schema
Event Types
| Event | Trigger | Common Uses |
|---|---|---|
SessionStart | Agent session begins | Load context, show status |
SessionEnd | Agent session ends | Save state, cleanup |
PreToolUse | Before any tool executes | Validation, logging |
PostToolUse | After any tool executes | Formatting, side effects |
Notification | Agent sends notification | Alerts, integrations |
Examples
Auto-Format on Write
Squad Status on Session Start
Git Commit Validation
Notify on Completion
Environment Variables
Hooks have access to context via environment variables:| Variable | Description |
|---|---|
TOOL_NAME | Name of the tool being called |
TOOL_INPUT | Input parameters (JSON) |
TOOL_OUTPUT | Output result (post-hooks only) |
TOOL_OUTPUT_PATH | File path (for file operations) |
SESSION_ID | Current session identifier |
Best Practices
- Keep hooks fast (< 10 seconds typical)
- Use timeouts to prevent hangs
- Log hook failures for debugging
- Test hooks in isolation before deploying
- Use matchers to target specific tools