AI & Claude May 2026 · 8 min read

Claude Code Hooks Guide 2026: Automate Your Dev Workflow

claude code hooks — terminal window showing automated commands triggered by Claude Code events

Hooks turn Claude Code into a fully automated dev assistant — running your formatters, tests, and notifications without you asking.

Claude Code hooks are shell commands that run automatically on specific Claude Code events — before a tool is used, after a session ends, when Claude needs attention, and more. They let you wire any external action into Claude's workflow: auto-format code, run tests, send Slack pings, log usage, or even block dangerous commands. This guide explains what hooks are, how to set them up, and gives 8 powerful real-world examples. Beginner-friendly, useful for any developer using Claude Code.

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

EventWhen It FiresCommon Use
PreToolUseBefore any tool is calledBlock dangerous commands, validate inputs
PostToolUseAfter a tool completesAuto-format edited files, run linters
UserPromptSubmitWhen you send a messageLog prompts, inject context
NotificationClaude wants your attentionDesktop notifications, sound alerts
StopClaude finishes a taskRun tests, commit, ping Slack
SubagentStopA sub-agent completesAggregate 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\"'"}
]
claude code hooks — developer writing settings.json file with hook configuration

A small settings.json change can turn Claude Code into a fully automated dev assistant.

Safety & Best Practices

  1. 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.
  2. Keep hook commands simple. One-line shell commands are easy to debug. Big bash scripts hidden inside JSON are not.
  3. Use matchers to limit scope. matcher: "Edit|Write" means the hook only fires for those tools — not every single tool.
  4. Log hook outputs while developing. Add > /tmp/hook.log 2>&1 at the end of new hooks until you trust them.
  5. Never bypass safety. Hooks should add automation, not skip Claude's permission prompts. Wrapping --no-verify into 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.

MAYANK DIGITAL LABS

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.

✅ Custom Hook Library ✅ Team settings.json Setup ✅ CI/CD Integration ✅ Code Quality Hooks ✅ Slack & Notification Hooks ✅ Developer Training
Get a Free Hooks Setup Call →

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.

Fixed-Price ServicesStrategy Call₹499·SEO Audit₹1,999·Ads Audit₹2,499
Get Started →