Module 05 — Hooks & automation

In this module

You'll understand why CLAUDE.md is not enough and how hooks deliver deterministic guarantees. You'll know how to wire in security guardrails, automate linting after each edit, and force checks before the agent considers its work done.

5.1 — Stochastic vs Deterministic: why hooks exist

Since the start of this training, you've been configuring Claude Code with CLAUDE.md files, project instructions, and team conventions. All of that works — most of the time. The fundamental problem: CLAUDE.md is advisory. Claude reads it, interprets it, and does its best. But "its best" is not a guarantee. An LLM can forget an instruction, misinterpret it, or decide it doesn't apply in a given context.

Hooks solve this problem. They are shell commands or LLM prompts that run automatically at precise points in the Claude Code lifecycle. They aren't "suggested" — they are executed. When you configure a hook that blocks file deletions, that hook will run on every attempt, no exceptions.

Think of the difference between a "speed limit 50 km/h" sign (CLAUDE.md) and an automated speed camera that systematically flashes (hooks). The first informs, the second enforces.

Hooks are configured in JSON in your settings files (.claude/settings.json for the project, ~/.claude/settings.json for global) and trigger on precise lifecycle events: session start, tool use, stop, context compaction, notification. Each event is an interception point where you can wire one or more hooks.

{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "echo '{\"decision\": \"deny\", \"reason\": \"Command blocked by hook\"}'",
            "timeout": 5
          }
        ]
      }
    ]
  }
}

Two types of hooks are available: command (deterministic shell script) and prompt (contextual LLM evaluation). The first is fast and predictable, the second is capable of semantic judgment. Picking between the two depends on the nature of the check.

Key takeaways

• CLAUDE.md = advisory (Claude tries to comply); hooks = deterministic (execution guaranteed)

• Two types of hooks: command (shell) and prompt (LLM)

• JSON configuration in settings files (project or global)

• Hooks are Claude Code's professional enforcement mechanism

In this module

  1. 5.1 — Stochastic vs Deterministic: why hooks exist Available
  2. 5.2 — Lifecycle events: where to wire your hooks
  3. 5.3 — Anatomy of a hook: JSON structure, matcher, timeout
  4. 5.4 — PreToolUse: the main gatekeeper
  5. 5.5 — PostToolUse: the automatic feedback loop
  6. 5.6 — Stop and SubagentStop: enforce quality before closing
  7. 5.7 — SessionStart, UserPromptSubmit, PreCompact: enriching context
  8. 5.8 — Security hooks: block, audit, validate
  9. 5.9 — Productivity hooks: linting, formatting, pre-commit
  10. 5.10 — Assembling a complete hooks configuration

Locked content

Unlock the full course to access this module.

Unlock

Already purchased? Recover access