Claude Code Basics Lesson 42 of 46
Hooks: The Thing That Always Happens
- A hook is an action that fires on its own at a precise point in the loop.
- The third type of control: instructions steer, permissions forbid, hooks guarantee.
- It doesn't depend on the model remembering it: it just happens.
- It's the most advanced thing in the course, and that's why it's at the end.
Three ways to be in control
In the course you've learned two ways to tell Claude Code how to behave, and you've seen them clash more than once:
Instructions (the file, skills) steer: they tell it how you'd like things to go. They work almost always and guarantee nothing.
Permissions forbid: they prevent by design, whatever the model decides.
The third one is missing, and it's the only one that solves a specific category of problem: making something happen. Not preventing, not suggesting — guaranteeing that a certain action always occurs, at a precise moment.
That third one is called a hook.
The problem it solves
Take something that has to happen every time. "After every edit to the documents, make me a backup copy." "Before every publish, check that the numbers add up."
You can write it in the instructions. And nine times out of ten it works — but the tenth time it doesn't, because instructions are recommendations addressed to someone who has to remember them amid everything else.
With a hook, that "after every edit" stops being a request: it becomes a mechanical fact. It happens because it's attached there, not because someone remembers it.
What it is, concretely
A hook is an action attached to a moment. You pick the moment — "as soon as a session starts", "before it touches a file", "when it's done" — and you pick the action. From then on, every time that moment is reached, the action fires.
And here's why this lesson is at the end of the course and not at the start: the moments you can attach to are the pieces of the agent's loop. You learned that loop in lesson 3. Without it, you wouldn't even know where to attach the thing.
What you use it for, in practice
Automatic checks: after every edit, run a verification — without having to ask for it.
Guardrails: before touching certain files, stop. Yes, this looks like a prohibition, and in the next lesson we'll see the difference.
Alerts: giving you a heads-up when it's done or when it's waiting on your answer, so you're not sitting there watching.
Fresh context: at the start of every session, putting an up-to-date piece of information in front of it that changes day by day.
What's next
In the next lesson we look at the points in the loop you can attach to — and one of them has a power instructions will never have: stopping a move right as it's about to fire.
Did you get it?
Two minutes, no grade. Trying to answer — even getting it wrong — cements the concepts far more than rereading. Not in the mood? Skip it: the lesson still counts as complete.
What's the difference between writing something in the instructions and putting it in a hook?
It's the same distinction as with permissions, applied to actions instead of prohibitions: instructions ask, the hook guarantees. If something must always happen, you don't leave it up to someone who might forget.
Why are hooks at the end of the course?
A hook attaches to a moment in the agent's cycle. Without knowing what that cycle is — which is what you learned in chapter 1 — there's no way to choose where to attach it.
-
Chapter 1 Introduction
-
Chapter 2 What's Underneath
-
Chapter 3 You Choose the Engine
-
Chapter 4 First Time at the Controls
-
Chapter 5 What You Let It Touch
-
Chapter 6 What It Knows About You
-
Chapter 7 Skills
-
Chapter 8 How Many You Put to Work
-
Chapter 9 What It's Connected To
-
Chapter 10 Automating the Setup
-
Chapter 11 Wrap-Up