When an agent edits a file, a check can tell us if the edit broke something. This video shows how a hook runs that check automatically, after every edit.
This function adds a list of item counts and returns the total as an integer. We already have a type checker that can compare the code with that declaration. The checking command is part of the project.
Suppose an edit turns the result into text. The function still promises an integer, so the type checker reports a mismatch. This is a useful error because it identifies both the location and the disagreement. The question here is how that error gets back to the agent.
The agent makes the edit. It runs the type checker itself only if it remembers to. When it forgets, you run the checker and copy its error into the conversation. The agent now has the information it needs to make a correction. The checking tool already did the analysis. Your part was starting it and carrying the result between programs.
Doing this once is easy. Repeating it throughout a session means watching for edits, switching windows and sending messages. We can automate those steps while keeping the same checker and the same useful error.
Now, the edit starts the type checker automatically, and its error goes straight to the model. The text at the bottom is what the hook sends to the model. It is the same error you would have pasted, and you did nothing.
Code registered to run when a particular event occurs is called a hook.
For a coding agent, the software around the model invokes the hook at a specified point in the session. That software is the harness.
The model requests an action, such as editing a file. The harness handles the tool call and returns information about the result to the model.
A hook can run before the tool, when the proposed action is still available to inspect. Another can run after a successful tool call, when the changed file is available to check. For example, our type checker can run after edits were made. Notice that it is the harness that starts it, not the model.
A project instruction can tell the agent to run the checks. The model reads that instruction along with the rest of its context and chooses its actions. With a configured command hook, the harness starts the command when the relevant event matches. These are two different ways to arrange the work.
Starting the command is automatic. Interpreting the error and choosing a good correction still involves the model. We have automated a recurring action, so it now happens every time, not only when someone remembers to run it.
A hook pairs an event in the session with a command. Whenever that event happens, the harness runs the command. Pick the event by when the command is useful. To stop the agent from writing to a protected file, the hook must run before the tool does. A check after the write cannot undo it.
Right after a file is edited is where our type checker belongs. It checks the new contents while the change is fresh.
When the agent says it is done, a hook can run the tests. If they fail, the agent keeps working instead of stopping.
And when the agent is waiting for you, a hook can send a desktop notification to bring you back.
Those four are only a taste. Claude Code offers many more events. There are events for the start and end of a session, for each prompt you send, for permission requests, for subagents, and for when the conversation is compacted. You do not need to memorize them. When you want something to happen automatically, ask the agent which event fits.
Start with a command you already use and understand. Attach it to the event where it helps, then see whether the session needs less intervention. The check still covers only what it was designed to check. The benefit of the hook is that useful feedback arrives without you repeatedly starting the command and carrying its result.
You don't need to remember how hooks are written. To add one, or to find out which events you can use, just ask the agent. What you need to know is what a hook can do for you. The agent handles the syntax.
See you in the next video.