Get your API key

Lesson 4 of 5

Hooks: checks that run every single time

Add a "waiting for you" alert, block edits to .env, auto-format after edits, and learn why every hook needs a careful read first.

9 min Log in to open
One step at a time, like flipping pages.

By the end of this lesson you will add two safe hooks in Claude Code, an alert when the agent is waiting for you and a guard that blocks edits to your .env file, and you will know how to read any hook before you trust it.

You need: Claude Code in a project folder (Codex and OpenCode are covered in step 5) · Time: 9 minutes

  1. Your rules file says "never touch .env". Your skill says "run the formatter after editing". Most days the agent follows them. But rules and skills are requests to the AI model: on a long day, after a compacted chat, it can skip one.

    A hook is different. It is a command that the tool itself (Claude Code, not the AI model) runs at a fixed moment, every time. The main moments:

    • PreToolUse: before the agent uses a tool, like editing a file. A hook here can block the action.
    • PostToolUse: after a tool finishes. Good for formatting a file or running tests.
    • Stop: when the agent finishes its answer.
    • Notification: when Claude Code needs your attention, for example to approve something.
    A row of five boxes, You ask, PreToolUse, Edit or Write, PostToolUse and Stop, with notes below: PreToolUse can block, PostToolUse can check, Stop can tell you
    Hooks attach to fixed moments. They run every time, whether or not the agent remembers.
    Why?

    A rule the agent may forget belongs in a hook. Claude Code's own docs give this advice: when a skill's rule must hold every time, move it into a hook.

Log in free to read the rest of this lesson

Every lesson is free. A free account unlocks all of them and saves your progress.

Still to come in this lesson:

  1. Your first hook: "the agent is waiting for you"
  2. Guard your .env file
  3. Tidy files after every edit
  4. Hooks in Codex and OpenCode
  5. Read every hook before you trust it
  6. Recap