Task 3.3

Hooks & Automation

Claude Code hooks are user-defined shell commands that run automatically at specific points in the Claude Code workflow. Hooks let you customize Claude Code's behavior without modifying its source — running linters after file edits, validating tool parameters before execution, or logging all tool calls for audit.

Hook Types

Claude Code supports several hook points: PreToolCall (runs before a tool executes), PostToolCall (runs after a tool executes), Notification (runs when Claude Code sends a notification), and Stop (runs when Claude finishes a response turn).

Each hook is configured with a matcher that determines which tool calls trigger it, and a command that specifies what shell command to run. Hooks can access tool call details through environment variables.

Hook Configuration

Hooks are defined in Claude Code settings (project or user level) under the 'hooks' key. Each hook specifies: the hook type (PreToolCall, PostToolCall, etc.), a matcher (tool name pattern), and the command to execute.

For example, a PostToolCall hook on the Edit tool could run a formatter after every file edit: when the matcher is 'Edit' and the command is 'prettier --write $CLAUDE_FILE_PATH'. The hook receives context about the tool call through environment variables.

Practical Hook Patterns

Common hook patterns include: auto-formatting after file edits (PostToolCall on Edit/Write), running tests after code changes (PostToolCall on Edit/Write), validating file paths before operations (PreToolCall on file tools), logging all tool calls for audit (PostToolCall on all tools), and sending notifications on completion (Stop hook).

PreToolCall hooks can block tool execution by returning a non-zero exit code. This enables validation gates — for example, preventing writes to protected directories.

Key Concept

Hooks Are Programmatic Guardrails You Control

Hooks give you programmatic control over Claude Code's behavior at the tool execution level. Unlike system prompt instructions (which are suggestions), hooks are shell commands that execute unconditionally. A PreToolCall hook that returns non-zero blocks the tool call entirely. This makes hooks the strongest form of local guardrail available in Claude Code.

Exam Traps

EXAM TRAP

Confusing hooks with MCP tools

Hooks are shell commands that run around tool calls. MCP tools are capabilities exposed to the model. Hooks are invisible to the model; MCP tools are visible and callable.

EXAM TRAP

Not knowing that PreToolCall can block execution

PreToolCall hooks can prevent tool execution by returning a non-zero exit code. This is a key mechanism for implementing tool-level guardrails.

EXAM TRAP

Ignoring hook execution order

When multiple hooks match the same tool call, they execute in definition order. The exam may test whether you understand how hook ordering affects behavior.

Check Your Understanding

You want to automatically run ESLint after every file edit Claude Code makes, and block any edit to files in the /config directory. Which hooks should you configure?

Build Exercise

Configure Claude Code Hooks

Intermediate30 minutes

What you'll learn

  • Configure PreToolCall and PostToolCall hooks
  • Use hooks for auto-formatting
  • Implement hook-based guardrails
  • Debug hook execution
  1. Add a PostToolCall hook that runs a formatter (e.g., prettier) whenever Claude Code edits a TypeScript file.

    WHY: Auto-formatting ensures consistent code style regardless of Claude's output formatting.

    YOU SHOULD SEE: After Claude edits a .ts file, the formatter runs automatically.

  2. Add a PreToolCall hook that blocks Bash commands containing 'rm -rf' or 'sudo'.

    WHY: PreToolCall guardrails prevent dangerous commands from executing.

    YOU SHOULD SEE: Attempts to run rm -rf or sudo are blocked with an error message.

  3. Add a PostToolCall hook that logs all tool calls (tool name, parameters, timestamp) to an audit log file.

    WHY: Audit logging provides visibility into what Claude Code is doing in your project.

    YOU SHOULD SEE: A growing log file with entries for every tool call.

  4. Add a Stop hook that runs the test suite when Claude finishes a response. Show a notification with test results.

    WHY: Automatic testing after each Claude response catches issues immediately.

    YOU SHOULD SEE: Tests run after Claude completes each response, with results displayed.

Sources

Previous

CLAUDE.md Files