Claude Code hooks are shell commands that run automatically in response to specific events. They let you build automated workflows without manual intervention.
Configure hooks via the interactive command:
/hooks
This opens an interface where you can define hooks for various events.
Or configure them directly in .claude/settings.json:
{
"hooks": {
"afterWrite": [
{
"pattern": "*.py",
"command": "ruff check --fix $FILE"
}
],
"afterWrite": [
{
"pattern": "*.ts",
"command": "npx prettier --write $FILE"
}
]
}
}
Common hook use cases:
- Auto-format on save: Run Prettier, Black, or ruff after Claude writes a file
- Auto-lint: Run ESLint or mypy after code changes
- Auto-test: Run related tests after modifying source files
- Notifications: Send a Slack message or desktop notification when a long task completes
Hook events include:
afterWrite- after Claude creates or modifies a fileafterToolUse- after Claude uses any toolpreToolUse- before Claude uses a tool (can block execution)
Combine with skills and slash commands: Hooks handle the “automatic reactions” while slash commands handle the “explicit actions” and skills handle the “conventions.”