SkillsApr 7, 2026·2 min read

Claude Code Hooks — Automate Your AI Workflow

Built-in automation system for Claude Code. Run shell commands on events like file edits, tool calls, and notifications. Lint on save, auto-test, and more.

SK
Skill Factory · Community
Quick Use

Use it first, then decide how deep to go

This block should tell both the user and the agent what to copy, install, and apply first.

Add hooks to .claude/settings.json:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "eslint --fix $CLAUDE_FILE_PATH"
      }
    ]
  }
}

Now every file edit by Claude Code auto-runs ESLint.

What are Claude Code Hooks?

Claude Code Hooks are automation triggers that run shell commands in response to Claude Code events. They let you enforce coding standards, run tests, validate changes, and integrate with external tools — all automatically, without Claude needing to remember to do it.

Answer-Ready: Claude Code Hooks are built-in automation triggers that run shell commands on events like file edits, tool calls, and notifications. Enforce linting, auto-testing, and custom validation in your Claude Code workflow.

Best for: Developers who want automated quality checks in their Claude Code workflow. Works with: Claude Code CLI and IDE extensions. Setup time: Under 2 minutes.

Core Features

1. Event Types

Event Fires When
PreToolUse Before a tool executes
PostToolUse After a tool completes
Notification When Claude sends a notification
Stop When Claude finishes a turn
SubagentStop When a subagent completes

2. Tool Matchers

Target specific tools:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Edit|Write",
        "command": "prettier --write $CLAUDE_FILE_PATH"
      },
      {
        "matcher": "Bash",
        "command": "echo 'Command executed: $CLAUDE_TOOL_INPUT'"
      }
    ]
  }
}

3. Common Hook Patterns

Auto-lint on file changes:

{
  "matcher": "Edit|Write",
  "command": "eslint --fix $CLAUDE_FILE_PATH 2>/dev/null || true"
}

Run tests after code changes:

{
  "matcher": "Edit",
  "command": "npm test -- --related $CLAUDE_FILE_PATH 2>&1 | tail -5"
}

Type-check TypeScript files:

{
  "matcher": "Edit|Write",
  "command": "[[ $CLAUDE_FILE_PATH == *.ts ]] && npx tsc --noEmit 2>&1 | head -20 || true"
}

Block commits without tests:

{
  "matcher": "Bash",
  "command": "if echo $CLAUDE_TOOL_INPUT | grep -q 'git commit'; then npm test || exit 1; fi"
}

4. Environment Variables

Variable Content
CLAUDE_FILE_PATH Path of the affected file
CLAUDE_TOOL_INPUT JSON input to the tool
CLAUDE_TOOL_OUTPUT JSON output from the tool

5. Hook Output

Hook stdout is fed back to Claude as context:

{
  "matcher": "Edit",
  "command": "python check_style.py $CLAUDE_FILE_PATH"
}

If the style checker reports issues, Claude sees the output and can fix them.

Advanced: PreToolUse Blocking

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Edit",
        "command": "python validate_edit.py",
        "blocking": true
      }
    ]
  }
}

If the command exits non-zero, the tool call is blocked.

FAQ

Q: Do hooks slow down Claude Code? A: Hooks run synchronously, so heavy commands add latency. Keep hooks fast (< 2s) or make them async.

Q: Can hooks modify Claude's behavior? A: Yes, hook stdout is fed back as context. You can guide Claude's next action.

Q: Where do I put the settings file? A: .claude/settings.json in your project root (project-level) or ~/.claude/settings.json (global).

🙏

Source & Thanks

Built into Claude Code by Anthropic.

Documentation: docs.anthropic.com/en/docs/claude-code/hooks

Discussion

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

Related Assets