Mastering Claude Code

Hooks and Guardrails

Deterministic automation that Claude cannot ignore — shell commands, HTTP calls, and LLM prompts that fire at lifecycle events to enforce formatting, block dangerous operations, and audit every action.

The enforcement gap

Every layer you have learned so far — CLAUDE.md, rules, skills, agents — works by giving Claude instructions and trusting it to follow them. Claude is very good at following instructions. But "very good" is not "guaranteed."

A CLAUDE.md instruction that says "always run the linter after editing TypeScript files" works most of the time. But in a long session, in the middle of a complex multi-file refactoring, Claude might skip it. Not maliciously — it simply did not prioritise that instruction over the immediate task. The linting instruction was one of 200 lines competing for attention.

Hooks close this gap. A hook is a deterministic automation that fires at a specific lifecycle event in Claude Code. It does not depend on Claude choosing to follow an instruction. It executes automatically, every time, at the exact moment it is configured to fire.

Hooks can:

  • Block actions — prevent Claude from executing a tool call entirely
  • Validate output — check that a file edit meets standards before it is committed to the conversation
  • Run side effects — trigger linting, formatting, notifications, logging
  • Inject context — add information to Claude's context based on what is happening

The mental model: if CLAUDE.md is the employee handbook and skills are the standard operating procedures, hooks are the automated compliance checks. They run whether or not anyone remembers to run them.

?

Has Claude ever done something in your project that it was 'supposed to' not do?