Hooks & Automation
Trigger automated actions at specific points in Claude Code's workflow.
What Are Hooks
Hooks are shell commands that run automatically at specific points during Claude Code's processing. They enable automation that would otherwise require manual intervention — formatting code, running tests, playing notification sounds, blocking unsafe edits, injecting context.
Hook Events
| Event | When It Fires | Use Case |
|---|---|---|
| PreToolUse | Before a tool executes | Block dangerous operations, validate inputs |
| PostToolUse | After a tool executes | Run formatting, linting, tests after edits |
| Notification | When notifications are sent | Play sounds, send alerts, log activity |
| UserPromptSubmit | When the user submits a prompt | Inject context, load branch-specific memory |
| Stop | Right before Claude finishes its response | Post-processing, notifications, cleanup |
Creating a Hook
Hooks are configured through the /hooks slash command or directly in settings files. Each hook specifies a shell command and which event triggers it.
{
"hooks": {
"UserPromptSubmit": {
"command": "./scripts/context-switcher.sh \"$PROMPT\"",
"description": "Load relevant context based on branch and query"
},
"Stop": {
"command": "uv run play_sound.py",
"description": "Play notification sound when Claude finishes"
}
}
}Hooks execute shell commands with your full user permissions. They can modify, delete, or access any file your user account can. Only use hooks from trusted sources and review them carefully before installing.
Advanced: Context Injection Hooks
One of the most powerful hook patterns is using UserPromptSubmit to dynamically inject context. The hook can inspect the current Git branch and the user's prompt, then append relevant context files to CLAUDE.md. This makes context switching automatic and branch-aware.