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
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.
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:
- Your first hook: "the agent is waiting for you"
- Guard your .env file
- Tidy files after every edit
- Hooks in Codex and OpenCode
- Read every hook before you trust it
- Recap