Claude Code Hooks: Automate Your Workflow
TL;DR
- Claude Code hooks let you run your own scripts at specific points in the AI workflow.
- You set them up in your project settings file under a hooks section.
- Common events include before a tool runs, after a tool runs, on notifications, and on stop.
- Hooks run as shell commands, so anything your terminal can do, your hook can do.
- Practical uses: auto-format code, run tests after edits, ping Slack when a task finishes.
What are Claude Code hooks?
Claude Code hooks are trigger points built into Claude Code. When something happens inside a Claude Code session, a hook can fire a shell command in response.
Think of it like this. Claude writes a file. Your hook notices. Your hook runs a formatter. Done. You never touched it.
Hooks sit in your Claude Code settings. They are just configuration. No extra tools required. No plugins. You describe the event, write the shell command, and Claude Code handles the rest. This is not magic. It is just automation wired into the right place.
Set it once. Let the machine do the repetitive work while you focus on what actually matters.
Which events can you hook into?
There are four main hook events in Claude Code right now.
Before a tool runs. This fires before Claude calls a tool. The tool has not run yet. You can use this to log what Claude is about to do, or to run a pre-check. Some people use it to gate certain tool calls in sensitive repos.
After a tool runs. This fires after a tool completes. It is the most useful one for most developers. Claude just edited a file, ran a command, or returned search results. Your hook fires next. Auto-formatting lives here. So does running a quick lint check.
On notification. This fires when Claude Code sends you a notification. That is usually a signal that something needs your attention or that a long task finished. You can pipe these into Slack, a desktop notifier, or a log file.
On stop. This fires when the agent finishes its turn. The full task is done. This is where you would trigger a test suite, send a summary to a webhook, or write a completion timestamp to a log.
If you want the full breakdown of what Claude Code can do, start with the Claude Code complete guide.
How do you set up hooks?
You put hooks in your Claude Code settings file. The settings file lives at the .claude folder in your project root, or in your global Claude Code config if you want them everywhere.
Inside the settings file, there is a top-level key called hooks. Under that, you name each hook event. Under each event, you provide a list of hook entries. Each entry has a command field. That command is what runs.
For after-tool hooks, you can also add a matcher field. The matcher filters which tool calls trigger the hook. If you only want to format after file writes, set the matcher to target the Write tool. Without a matcher, the hook fires after every tool call of that event type.
The command field takes a plain shell string. You can call any CLI tool, any script, any binary on your path. If you want to run multiple commands, chain them or write a small shell script and call that.
One important thing: before-tool hooks run synchronously. Claude Code waits for the hook to finish before it proceeds. After-tool, stop, and notification hooks run in the background by default so they do not slow your session down. For more on how settings and permissions interact, read the Claude Code settings and permissions guide.
Hooks are just shell commands. If your terminal can do it, your hook can do it.
Practical example: auto-format after every file write
This is the most common hook people set up first.
You set an after-tool hook with a matcher that targets the Write tool. The command calls your formatter. For a JavaScript or TypeScript project, that might be Prettier pointed at the file that was just written. For Python, it might be Black or Ruff.
Claude Code passes context to your hook command as environment variables. One of those variables is the path of the file that was just written. Your formatter reads that path and formats exactly that file. Fast. No full-repo scan on every write.
The result: every file Claude touches comes out formatted. No manual step. At Devwiz, this is one of the first things set up on any Claude Code project. It pays for itself in the first hour.
Practical example: run tests after code changes
An after-tool hook on the Write tool can also run your test suite, or a subset of it.
The key is scoping it right. Running your full test suite after every single file write would be slow and annoying. What works better is running only the tests related to the file that changed.
Most test runners support path-based filtering. Your hook grabs the file path from the environment variable Claude Code provides, works out which test file covers it, and runs just that test.
If the tests pass, nothing happens. The session continues. If they fail, Claude Code sees the output and can self-correct on the next turn. You just built a feedback loop without writing a single line of framework code. This pairs well with Claude Code skills, which let you package workflows like this into reusable, shareable units.
Practical example: notify when a task finishes
Long tasks are the main place the notification and stop hooks earn their keep.
Claude Code can run for a while on complex tasks. You do not want to sit there watching it. You want to go do something else and get pinged when it is done.
A stop hook that calls a shell command to send a desktop notification works well on Mac with the built-in osascript command. On Linux, notify-send does the same job.
If you want a Slack ping instead, write a small curl command that hits your Slack webhook URL with a message. Put that in the stop hook command field. Every time an agent turn finishes, Slack gets a message.
You can get as creative as you like. Some people log completion times to a file to track how long different types of tasks take. Others post to a Discord channel. Others trigger a webhook that kicks off a downstream process. The mechanism is always the same. Shell command. Environment variables for context. Fire and go.
What should you not use hooks for?
Hooks are powerful, but they are not the right tool for everything.
Do not use hooks for anything that needs to change what Claude Code is about to do. Hooks observe and react. They do not intercept and change. If you want to shape Claude Code behaviour more deeply, look at Claude Code tips and patterns for prompt and workflow strategies instead.
Do not write hooks that take a long time to run on the before-tool event. Claude Code waits for those to finish. A slow before-tool hook makes every tool call feel sluggish.
Do not use hooks as a replacement for proper CI. They are a development-time feedback loop. Your actual test suite and lint checks should still run in CI on pull requests.
FAQ
What shell does Claude Code use to run hook commands?
It uses your system shell. On Mac and Linux that is usually bash or zsh. On Windows it would be cmd or PowerShell depending on your setup. Write your commands accordingly.
Can hooks access Claude Code context, like what the agent is working on?
Yes. Claude Code passes context through environment variables. For tool hooks, you get things like the tool name, the file path involved, and the tool input and output. The exact variables depend on the event type.
Do hooks slow down my Claude Code session?
Before-tool hooks can, because Claude Code waits for them. After-tool, stop, and notification hooks run in the background and should not. Keep your before-tool hooks fast.
Can I disable a hook temporarily without deleting it?
Yes. Add a disabled flag to the hook entry in your settings file. The hook stays there but will not fire. Remove the flag or set it back to false when you want it again.
Start automating your Claude Code workflow
Hooks are one of those things that feel small until you use them daily. A formatter that runs itself. Tests that fire without prompting. A Slack ping when the job is done. Each one is a tiny friction removal. Together they add up.
If you are serious about getting the most out of Claude Code in your business, a proper setup is worth it. Want help mapping out how AI tools like this can work inside your operation? Book a free AI transformation session and we will work through it together.
About the Author
The AI Orchestrator
AI Orchestrator and entrepreneur with 10+ years building digital products. Helping $1M+ business owners scale with AI systems, automation, and implementation through The AI Orchestrators, Devwiz, and Njin.