Claude Code Hooks Guide 2026: Automate Your Dev Workflow
Hooks turn Claude Code into a fully automated dev assistant — running your formatters, tests, and notifications without you asking.
What are Claude Code Hooks?
Claude Code hooks are shell commands that fire automatically when Claude Code does certain things — before a tool runs, after a session ends, when you submit a prompt, when Claude stops. They are configured in your settings.json file and let you wire any action into Claude's workflow.
The mental model: hooks are like cron jobs, but instead of running on a time schedule, they run on Claude Code events. Anything you can do from the command line, a hook can do — formatters, linters, tests, notifications, logging, file backups.
All Hook Events Available
| Event | When It Fires | Common Use |
|---|---|---|
PreToolUse | Before any tool is called | Block dangerous commands, validate inputs |
PostToolUse | After a tool completes | Auto-format edited files, run linters |
UserPromptSubmit | When you send a message | Log prompts, inject context |
Notification | Claude wants your attention | Desktop notifications, sound alerts |
Stop | Claude finishes a task | Run tests, commit, ping Slack |
SubagentStop | A sub-agent completes | Aggregate results, status updates |
How to Set Up Your First Hook
Hooks live in ~/.claude/settings.json (user-level) or .claude/settings.json (per-project). Here is a minimal example that plays a sound every time Claude finishes:
{
"hooks": {
"Stop": [
{
"type": "command",
"command": "say 'Claude is done'"
}
]
}
}
On macOS this will literally speak "Claude is done" out loud. On Windows you might use powershell -c "[console]::beep(800,300)". On Linux: paplay /usr/share/sounds/freedesktop/stereo/complete.oga.
Save the file. The next time Claude finishes a task, the hook fires. No restart needed.
8 Powerful Real-World Examples
1. Auto-format every file Claude edits
"PostToolUse": [
{
"matcher": "Edit|Write",
"type": "command",
"command": "prettier --write $CLAUDE_FILE_PATH"
}
]
2. Run tests after every change
"Stop": [
{"type":"command","command":"npm test"}
]
3. Block dangerous bash commands
"PreToolUse": [
{
"matcher": "Bash",
"type": "command",
"command": "echo \"$CLAUDE_BASH_COMMAND\" | grep -q 'rm -rf' && exit 1 || exit 0"
}
]
4. Slack notification when Claude needs you
"Notification": [
{
"type":"command",
"command":"curl -X POST -d 'Claude needs attention' $SLACK_WEBHOOK"
}
]
5. Log every prompt for auditing
"UserPromptSubmit": [
{"type":"command","command":"echo \"$(date): $CLAUDE_USER_PROMPT\" >> ~/claude-log.txt"}
]
6. Auto-commit completed work
"Stop": [
{"type":"command","command":"git add -A && git commit -m 'WIP: Claude session' --no-verify"}
]
7. Backup files before any edit
"PreToolUse": [
{"matcher":"Edit","type":"command","command":"cp $CLAUDE_FILE_PATH $CLAUDE_FILE_PATH.bak"}
]
8. Desktop notification on completion
"Stop": [
{"type":"command","command":"osascript -e 'display notification \"Done\" with title \"Claude\"'"}
]
A small settings.json change can turn Claude Code into a fully automated dev assistant.
Safety & Best Practices
- Test on a sandbox project first. A bad PreToolUse hook can lock you out of using Claude. Test on a throwaway repo before adding to your main project.
- Keep hook commands simple. One-line shell commands are easy to debug. Big bash scripts hidden inside JSON are not.
- Use matchers to limit scope.
matcher: "Edit|Write"means the hook only fires for those tools — not every single tool. - Log hook outputs while developing. Add
> /tmp/hook.log 2>&1at the end of new hooks until you trust them. - Never bypass safety. Hooks should add automation, not skip Claude's permission prompts. Wrapping
--no-verifyinto a hook is asking for trouble.
Hooks run with your user permissions. Anything you can do from the terminal, a hook can do — including deleting files. Read every hook you copy from the internet before pasting it into your settings.
For more Claude developer context, see our guides on Claude Skills Feature and Claude Agent SDK Guide.
Need Custom Claude Code Hooks for Your Team?
At Mayank Digital Labs, we configure production-grade Claude Code setups — hooks, Skills, Agents, and CI integrations — for engineering teams. We build the automation layer once so your developers spend more time shipping and less time on plumbing.
No commitment. A 30-minute call to map your team's automation needs.
References & Further Reading
Looking for the best information about Claude Code hooks? Quick takeaway: add 2 hooks today. Start with PostToolUse for auto-formatting and Stop for desktop notifications. From there, you will discover more automations naturally. Hooks are the most underused power feature in Claude Code.
Frequently Asked Questions
What are Claude Code hooks?
Claude Code hooks are shell commands that run automatically when specific events happen in Claude Code — before a tool is used, after a session ends, when Claude stops, and more. They let you automate everything from auto-formatting code to sending notifications to logging activity.
What hook events does Claude Code support?
Common hook events include: PreToolUse (before any tool), PostToolUse (after), Stop (when Claude finishes), UserPromptSubmit (when you send a message), Notification (when Claude needs your attention), and SubagentStop (when a sub-agent ends). Each can run any shell command.
How do I add a Claude Code hook?
Edit ~/.claude/settings.json (or .claude/settings.json in your project). Add a hooks block specifying the event and the command. For example: hooks: {Stop: [{type: 'command', command: 'say done'}]} will make your computer speak 'done' every time Claude finishes a task.
Can hooks block Claude actions?
Yes. PreToolUse hooks can prevent dangerous tool calls. For example, you can block any Bash command that contains 'rm -rf' or block Edits to sensitive files. The hook returns a non-zero exit code to deny the action and a clear error message to Claude.
Are hooks safe to use?
Hooks run with your user permissions on your machine — same as any shell command. Be careful with PreToolUse hooks that delete or modify files automatically. Test new hooks on a side project first. Never use hooks to bypass safety checks like Claude Code's permission prompts.