Skip to main content
TechWolf

Automating with hooks

8 min intermediate

Think of the command you run after every single edit, because you always have to. Or the moment you finish a prompt and go and check whether anything broke. Or the login you forget until an agent has already failed on it.

None of that needs you. It needs something to notice, and something to run.

Everything here is Claude Code. Codex has its own version of some of it, so the shapes travel even if the commands do not.

What you can hook into

A hook is a command Claude Code runs by itself whenever a particular thing happens. You pick the moment, you pick the command, and from then on it just happens.

The moments you can hook into are the obvious ones. When a session starts or ends. When you send a prompt, or the agent finishes answering. Before or after the agent uses a tool.

That covers most of what people want:

  • Run your formatter after every file edit, so you never look at badly formatted code again.
  • Get a desktop notification when the agent finishes, so you can go do something else.
  • Run the test suite after an edit and hand the failures straight back.
  • Check something before an action happens and block it if it looks wrong. Guards like that are the whole of the next course.

Hooks live in the same settings file as your permissions, and Claude Code writes them for you if you describe what you want.

Hands-on

01

Paste this into Claude Code:

Create a hook that sends me a desktop notification whenever you finish responding.

It shows you what it will set up. Read that back, then approve.

02

Kick off a slow task, switch to another tab, and wait for the ping. If it fires, a step you used to do by hand, checking whether it is done yet, now happens for you.

03

Now write the preflight one. Name the thing your work depends on, a login, a token, a VPN, a running server, and ask for it:

Add a SessionStart hook that checks ___ when a session opens and prints a one-line
warning if it is broken. It must warn me without ever blocking the session.

Read it back before approving, then open a fresh session and watch it run.

04

Open your settings file and read both hooks you just added. You are looking at commands that will run on your machine automatically from now on, so knowing where they live matters as much as having them.

Reflect

  • Look at the last thing you babysat for twenty minutes. What were you actually watching for? That thing is usually a hook waiting to be written.
  • What is the step you always forget until it is too late? A hook that checks it at the start of a session costs you five minutes to write, once.
4 / 9 in Building your own tools
Previous