Claude Code & Terminal AI

Claude Code Hooks: Automate Your Workflow with Event-Driven Commands (2026)

Hooks let you run custom shell commands before or after Claude Code actions. Auto-format on save, auto-test on commit, custom notifications -- all triggered automatically. Here's how to set them up.

Claude Code hooks are custom shell commands that execute automatically when specific events happen during your Claude Code session. Think of them as automated responses to Claude Code's actions -- when it writes a file, you can auto-format it. When it creates a commit, you can auto-run tests. When it finishes a task, you can send a notification.

Why hooks matter

Without hooks, you manually run formatting, testing, and verification after Claude Code makes changes. With hooks, all of that happens automatically. Your workflow becomes:

  • Tell Claude Code what to build
  • Claude Code writes the code
  • Hooks auto-format, auto-test, auto-lint -- all in the background
  • You review clean, tested output
This eliminates entire categories of manual work from your development process.

How hooks work

Hooks are configured in your Claude Code settings file (~/.claude/settings.json or project-level .claude/settings.json). Each hook specifies:

  • Event: When to trigger (before/after file write, commit, etc.)
  • Command: The shell command to execute
  • Matcher (optional): File patterns to filter which events trigger the hook

Common hook patterns

Auto-format on file save

When Claude Code writes a TypeScript file, automatically run Prettier:

``json { "hooks": { "afterFileWrite": [ { "command": "npx prettier --write $FILE", "matcher": "*/.{ts,tsx,js,jsx}" } ] } } `

Auto-lint after edits

Run ESLint on every file Claude Code modifies:

`json { "hooks": { "afterFileWrite": [ { "command": "npx eslint --fix $FILE", "matcher": "*/.{ts,tsx}" } ] } } `

Run tests before commit

Prevent commits if tests fail:

`json { "hooks": { "beforeCommit": [ { "command": "npm test" } ] } } `

Custom notification on completion

Send yourself a notification when Claude Code finishes a long task:

`json { "hooks": { "afterResponse": [ { "command": "osascript -e 'display notification "Claude Code finished" with title "Done"'" } ] } } `

Available hook events

|-------|---------------|
EventWhen it fires
afterFileWriteAfter Claude Code writes or edits a file
beforeCommitBefore Claude Code creates a git commit
afterResponseAfter Claude Code finishes responding
beforeToolUseBefore Claude Code uses any tool

Best practices

Keep hooks fast. Slow hooks block Claude Code. Auto-formatting a single file is fine. Running your entire test suite on every file save is not.

Use matchers. Don't run TypeScript hooks on CSS files. Matchers let you scope hooks to relevant file types.

Test hooks manually first. Before adding a hook, run the command manually to make sure it works. A broken hook can disrupt your entire workflow.

Use project-level settings. Put hooks in your project's .claude/settings.json so they're specific to that project and shared with your team.

Advanced: chaining hooks

You can run multiple commands on the same event:

`json { "hooks": { "afterFileWrite": [ { "command": "npx prettier --write $FILE", "matcher": "*/.ts" }, { "command": "npx eslint --fix $FILE", "matcher": "*/.ts" } ] } } `

Hooks run in order. If one fails, subsequent hooks for that event are skipped.

Getting started

  • Open your project's .claude/settings.json` (create it if it doesn't exist)
  • Add a simple hook -- start with auto-formatting
  • Test it by asking Claude Code to write a file
  • Verify the hook fired by checking the output
Hooks are one of the features that separate basic Claude Code usage from power usage. Once you set them up, you never go back.

Frequently asked questions

What are Claude Code hooks?

Hooks are custom shell commands that run automatically when specific events happen in Claude Code -- like when it writes a file, creates a commit, or finishes a response. They let you auto-format code, run tests, send notifications, and automate any shell command as part of your Claude Code workflow.

How do I set up Claude Code hooks?

Add hooks to your settings file at .claude/settings.json (project-level) or ~/.claude/settings.json (global). Each hook specifies an event (like afterFileWrite), a command to run, and an optional file matcher pattern. Start with a simple auto-format hook and build from there.

Can hooks run tests automatically?

Yes. Use the beforeCommit event to run your test suite before Claude Code creates a git commit. If tests fail, the commit is blocked. This prevents broken code from being committed.

Do hooks slow down Claude Code?

They can if the commands are slow. Keep hooks fast -- auto-formatting a single file takes milliseconds, but running a full test suite on every file save would block the workflow. Use file matchers to limit when hooks fire.

Related guides

Need help implementing this?

//prometheus does onsite AI consulting and implementation in Milwaukee. We set it up, train your team, and make sure it works.

let's talk