Claude Code Hooks — Custom Automation Recipes
Collection of ready-to-use Claude Code hook recipes for automating code formatting, testing, notifications, and security checks. Copy-paste into settings.json. Community-maintained.
What it is
Claude Code Hooks are shell commands that execute automatically before or after specific tool uses within Claude Code sessions. They work like git hooks but for your AI coding assistant. This collection provides ready-to-use recipes for auto-formatting code, running tests, sending notifications, scanning for security issues, and enforcing workflow policies.
The collection targets developers using Claude Code who want to enforce code quality standards and automate repetitive tasks around AI-generated code. Each recipe is a JSON snippet you paste into your settings.json.
How it saves time or tokens
Without hooks, you need to manually run formatters, linters, and tests after every file Claude Code writes or edits. Hooks automate this entire class of tasks. A PostToolUse hook on Write and Edit operations means every generated file is automatically formatted, and pre-commit quality checks happen without manual intervention.
The token estimate for this workflow is approximately 2,600 tokens. Hooks run as shell commands outside the LLM context, so they consume zero tokens.
How to use
- Open Claude Code settings:
claude config
Or edit ~/.claude/settings.json directly.
- Add a hook to auto-format every file Claude writes:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || npx prettier --write $CLAUDE_FILE_PATH 2>/dev/null || true"
}
]
}
}
- Claude Code now auto-formats every file it creates or modifies.
Example
Multiple hook recipes for different automation needs:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"command": "echo 'Running: '$CLAUDE_COMMAND >> ~/.claude/audit.log"
}
],
"PostToolUse": [
{
"matcher": "Write|Edit",
"command": "npx biome check --write $CLAUDE_FILE_PATH 2>/dev/null || true"
}
],
"Stop": [
{
"command": "terminal-notifier -message 'Claude Code finished' -title 'Done' 2>/dev/null || true"
}
]
}
}
Hook event types:
PreToolUse -- Before a tool runs (can block execution)
PostToolUse -- After a tool completes
Notification -- When Claude sends a notification
Stop -- When Claude finishes a response
Related on TokRepo
- AI Tools for Automation -- Workflow automation tools
- AI Tools for Coding -- Developer productivity integrations
Common pitfalls
- Hook commands run synchronously. A slow command (e.g., running a full test suite on every file save) blocks Claude Code until it completes. Keep hook commands fast or run them asynchronously with
&. - The
$CLAUDE_FILE_PATHvariable is only available for Write and Edit tool matchers. Other tools have different environment variables. Check the Claude Code documentation for the full variable list. - Hooks that exit with a non-zero status on PreToolUse can block the tool from running. Always add
|| trueto PostToolUse hooks to prevent false failures from interrupting the workflow.
Frequently Asked Questions
Four events: PreToolUse (before a tool runs, can block), PostToolUse (after a tool completes), Notification (on notifications), and Stop (when Claude finishes responding). Each accepts a matcher pattern and shell command.
Yes. PreToolUse hooks that exit with a non-zero status prevent the tool from running. This is useful for security policies like blocking writes to protected directories. PostToolUse hooks do not block.
No. Hooks run as shell commands in a separate process. They do not add to the LLM context or consume API tokens. The token cost is only for the hook configuration in settings.json.
Hooks go in ~/.claude/settings.json under the hooks key. You can also set project-level hooks in .claude/settings.json within your project directory for team-shared automation.
Yes. The Stop event fires when Claude finishes a response. Use it to trigger desktop notifications, Slack messages, or any shell command. The example uses terminal-notifier on macOS.
Citations (3)
- Anthropic Claude Code Documentation— Claude Code hooks run shell commands before and after tool use
- Anthropic Docs— Claude Code settings configuration reference
- Biome— Biome formatter and linter for JavaScript and TypeScript
Related on TokRepo
Source & Thanks
Community-maintained collection based on Claude Code documentation.
Contributions welcome — share your hook recipes!
Discussion
Related Assets
/babysit — Auto-Respond to PR Review Comments
Open-source slash command that watches a PR for review comments and auto-pushes fixes. Inspired by Boris Cherny's /babysit pattern.
/loop — Local Recurring Task Scheduler (Boris-Style)
Open-source slash command for recurring local Claude Code tasks with a 3-day safety cap. Inspired by Boris Cherny's /loop scheduler.
/batch — Parallel Worktree Migration Slash Command
Open-source slash command that splits a migration across parallel git worktrees. Inspired by Boris Cherny's /batch worktree pattern.