SkillsApr 6, 2026·2 min read

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.

TL;DR
Claude Code hooks are shell commands that auto-run before or after tool use for formatting, testing, and security checks.
§01

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.

§02

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.

§03

How to use

  1. Open Claude Code settings:
claude config

Or edit ~/.claude/settings.json directly.

  1. 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"
      }
    ]
  }
}
  1. Claude Code now auto-formats every file it creates or modifies.
§04

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
§05

Related on TokRepo

§06

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_PATH variable 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 || true to PostToolUse hooks to prevent false failures from interrupting the workflow.

Frequently Asked Questions

What hook events are available?+

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.

Can hooks block tool execution?+

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.

Do hooks consume tokens?+

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.

Where do I configure hooks?+

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.

Can I use hooks for notifications?+

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)
🙏

Source & Thanks

Community-maintained collection based on Claude Code documentation.

Contributions welcome — share your hook recipes!

Discussion

Sign in to join the discussion.
No comments yet. Be the first to share your thoughts.

Related Assets