Claude Code Hooks Explained | Agentic Engineering — Transcript
Full transcript
- 0:00AI or LLM's are probabilistic models,
- 0:04which means the model decides what to do
- 0:06next. It generally it follows basic
- 0:09instructions pretty well, but it's not
- 0:11guaranteed. The same prompt can yield
- 0:13multiple different things.
- 0:15And unfortunately, the longer the prompt
- 0:17gets, the longer your context window
- 0:18becomes, and the more often that your AI
- 0:21might start drifting and forgetting
- 0:23basic instructions.
- 0:25Now, unfortunately, for those of us who
- 0:27wants to run long agentic loops, um that
- 0:30can mean that we might start forgetting
- 0:32certain things or certain things might
- 0:33not happen. And
- 0:35and that's why Claude introduced the
- 0:37concept of hooks. And then by the end of
- 0:39this video, you'll know about three
- 0:40types of hooks, when when to use them,
- 0:43and basically how to set it up so that
- 0:46your agents will give you the same
- 0:47results every single time without you
- 0:50needing to reprompt it and do everything
- 0:51to keep a long-running loop.
- 0:54Anything that we tell the agent is
- 0:56context. It's basically just a prompt
- 0:59essentially or some skill file or rules
- 1:02that the model reads and then acts upon.
- 1:05However, hooks are a little bit
- 1:07different.
- 1:08A hook on the hand is something that
- 1:10Claude code uses to run at a fixed point
- 1:12in a in the agent's loop. So,
- 1:16there are phases like you can
- 1:18essentially hook your code into like
- 1:20before a tool runs, after it runs, or
- 1:23even points to stop the agent from
- 1:26finishing before a condition is met. An
- 1:28important part here is that Claude does
- 1:30not decide when the hook runs. So, it
- 1:33runs every single time in a
- 1:35deterministic way. So, however long the
- 1:37session gets, you will always get the
- 1:40same results.
- 1:41But before we continue talking, hi, I'm
- 1:43Josh. I'm a software developer, and I'm
- 1:46making it my personal mission to use AI
- 1:48to automate all the boring work so we
- 1:50can focus on what matters.
- 1:52Before we look at our different
- 1:54examples, I want to paint a mental model
- 1:56of what hooks are because it's not
- 1:58something we can just go to Claude and
- 2:00have a fancy UI gets created. So, hooks
- 2:03essentially are just like we said
- 2:07before, code that gets tied to
- 2:10tool usages in Claude. So, tool usages
- 2:12could be like, you know, grep, bash, and
- 2:15any other custom tools that you might
- 2:17have made, maybe writes, edits to files,
- 2:20and things like that. For every hook,
- 2:22you can define a
- 2:25rules to execute certain code on a on
- 2:28your tool. We will have some of our
- 2:30common executions like before every tool
- 2:32gets ran, we can fire a specific code to
- 2:34do something. After a tool gets ran, we
- 2:36can fire another hook. And then we can
- 2:39also fire hooks when the agent is done.
- 2:43There's actually 33 hooks currently in
- 2:45the documentation, but we're just going
- 2:46to go over these three.
- 2:49There are three types of hooks I want to
- 2:50go through. We'll build each one live,
- 2:52run it on our new project, and just see
- 2:54what happens
- 2:56as we execute.
- 2:58So, the the first example of a hook that
- 3:00we're going through is a block command.
- 3:02So, what happened to me before in
- 3:04several instances where I would tell
- 3:06Claude to go do something, and then in
- 3:09its infinite wisdom, it decided to npm
- 3:11install different packages or actually
- 3:13take control of my browser and go
- 3:14download different files.
- 3:17So, in this instance, I'm going to show
- 3:18you how we can create a a hook to stop
- 3:21certain commands from being ran. And
- 3:23it's good for security perspectives and
- 3:25things like that.
- 3:26So, in this instance, we're going to use
- 3:28the pre-tool use hook. So, before we use
- 3:31something like npm install or we would
- 3:35double-check the context and then block
- 3:37it from happening.
- 3:39So, in this instance, we can create a
- 3:40hook that would trigger before npm gets
- 3:44called, and we can define things like,
- 3:46you know, rules and criterias on what is
- 3:48installed and what's not installed, so
- 3:49we don't completely stop ourselves from
- 3:52installing files, but maybe stop things
- 3:54that we don't or didn't expect.
- 3:58So, here's what this guard will look
- 4:00like and
- 4:01and all hooks are essentially say a JSON
- 4:04file where you kind of define what gets
- 4:07triggered. So, we can just go through
- 4:08the example right here. So, we define a
- 4:10pre-tool use
- 4:12and then you define tools that gets
- 4:14executed that we want to
- 4:17uh fire the hook for. Some basic things
- 4:19are like bad, PowerShell, writes, things
- 4:21like that.
- 4:23Um and then define the hook itself. In
- 4:25this instance, whenever this condition
- 4:28gets triggered, we just want to
- 4:31uh execute a script. Uh this script you
- 4:34can
- 4:36do things like read or define what
- 4:39should or should not be installed or
- 4:41basically anything you want.
- 4:43Like it's your own logic, but that's
- 4:45hooks in a nutshell. You don't have to
- 4:48know much about this, which is actually
- 4:49amazing with AI these days. Uh just ask
- 4:52Claude and Claude will figure it out for
- 4:53you.
- 4:55But, let's go to the demo.
- 4:56Okay, so now we're on Claude code. Let's
- 4:58go through the example of creating a
- 5:00block hook.
- 5:02And let's see what happens.
- 5:08Yeah, so here you can see some of the
- 5:09logic here. We can define specific
- 5:11packages that are not allowed to be
- 5:13installed. So, we can have an allow list
- 5:15already.
- 5:17Inside that's looks like that's what the
- 5:19agent decided it's going to do.
- 5:21Looks like it ran into some work tree
- 5:22problems and get repo issue, but that's
- 5:24okay.
- 5:32Uh as a bonus, it looks like the code
- 5:34actually ran into a
- 5:36a bug and decided to fix itself. So,
- 5:38that's pretty cool.
- 5:42All right, it is completed. Let's see
- 5:44what they what we made.
- 5:47So, it looks like it created a allow
- 5:49list for our hook that already allowed
- 5:51241 packages across in all the projects
- 5:54that I already have. So, that's pretty
- 5:56cool.
- 5:58Um it ran NPM install and other things
- 6:01and it blocked it.
- 6:04And looks like our typical NPM command
- 6:06still works. Uh whenever we scroll up,
- 6:08we can see the show commands and see
- 6:09what happens.
- 6:11Yeah, here I think it's trying to do NPM
- 6:14install right here. And then you can see
- 6:15right here that
- 6:17the command got blocked by the NPM
- 6:19guard. So, they were trying to install
- 6:20some things like NPM install sketchy
- 6:22thing. Well, I'm glad we caught it. Uh
- 6:24we didn't install sketchy thing.
- 6:26But, we see that the hook is doing its
- 6:28job properly. You don't have to you
- 6:30don't have to take Cloud's code word for
- 6:32it because we just scrolled up, but it
- 6:34tested everything that we just talked
- 6:36about.
- 6:38All right, here on VS Code, this is
- 6:40essentially everything that was created.
- 6:42I'm not even going to pretend that we
- 6:44know what any of this code is, but uh
- 6:48now this is the code that gets triggered
- 6:50every time and the NPM command gets
- 6:52called. I'm I'm going to guess this is
- 6:54just parsing through our list parsing
- 6:56through the
- 6:58the package and then just look for our
- 6:59allow list. And so, here's just a list
- 7:01of things that we support or that we can
- 7:04install in our packages.
- 7:06And then here's the hook itself.
- 7:08And you see right here
- 7:09pre-tool use
- 7:11uh NPM guard.
- 7:13So, that's the power of the block hook.
- 7:15It helps prevent you from installing
- 7:17sketchy uh NPM packages
- 7:19among many other things.
- 7:22Now, for the next example, this is
- 7:23something I'm actually very excited
- 7:25about. Uh making sure that you run lint
- 7:27after every time we write code.
- 7:31To achieve this, we're going to use
- 7:32something called a post-tool use. Like
- 7:34the name suggests, it runs after every
- 7:37call succeed. Same rules that we talked
- 7:39about, except in this case the matcher
- 7:42is edit and write.
- 7:44And the only way that it can finish and
- 7:46exit the hook is that we see that the
- 7:49linter is ran.
- 7:51So, you can imagine that this is very
- 7:52good for long-running agentic sessions.
- 7:54You write some code. Um the AI have no
- 7:57way to know if the code is right or not
- 7:59or conform to any standards unless, you
- 8:01know, the model itself is smart. But, by
- 8:03putting this post hook tool, we force
- 8:06the model to run our linters and
- 8:07everything going. And this is how you
- 8:09have strong code that's
- 8:11deterministically enforced wanting you
- 8:14to re-prompt Claude.
- 8:16So, here's a brief example of what that
- 8:19would look like here.
- 8:20Not anything too different from what we
- 8:22already looked at. Only main difference
- 8:24is a post tool use versus a pre-tool
- 8:26use.
- 8:28I know, rocket science. But, let's go
- 8:29through a quick example.
- 8:31I'm just going back to our same Claude
- 8:33session. I'm just going to say, "Next
- 8:37help create a
- 8:40linter hook that makes sure that we run
- 8:45lint on any of our projects and fix it
- 8:50before
- 8:52we are done writing."
- 8:58All right. Uh let's see if it
- 8:59understands my broken English and let's
- 9:02we'll just continue and wait.
- 9:08Oh, well, looks like we skipped to the
- 9:10second example.
- 9:11Uh based on my wording, it said to make
- 9:13the post tool use to run lint. And it
- 9:15also decided to make a stop hook to
- 9:18uh make sure that the linter runs. So,
- 9:21um I guess while it runs, let's just go
- 9:24through it uh
- 9:25quick recap on what a stop hook is.
- 9:29Uh like the name suggests, uh when
- 9:31Claude is done with their turn, uh stop
- 9:33hooks are used to stop Claude from
- 9:35stopping.
- 9:36Now, say that two times fast.
- 9:38And the only way to uh
- 9:41exit it is basically the flag or there's
- 9:43a counter for like, you know, you tried
- 9:45XYZ times
- 9:47and then that will basically
- 9:50force Claude to just go back and try
- 9:52again. Of course, I will say as you
- 9:54continue playing around with stop hooks,
- 9:56you're going to run into these uh
- 9:58situations where especially for longer
- 9:59running sessions where uh Claude would
- 10:02try to justify why the work is done and
- 10:06start making up reasons or doing things
- 10:08to end prematurely before actually going
- 10:11back. So, something to watch out for
- 10:13when you go for long running sessions.
- 10:16It's basically the same thing as we've
- 10:18been doing. You create stop and then uh
- 10:21once it's installed, you just execute
- 10:24some code that gets triggered every
- 10:26single time. Uh there's no matches or
- 10:30anything. It's always fired. Uh so, that
- 10:32will lead into other interesting
- 10:33problems when you start running it on
- 10:35your projects where uh
- 10:38it might you might start seeing
- 10:40uh stop hooks that's for unrelated
- 10:43project interfering with different uh
- 10:46code bases and things that you're
- 10:47working in. So, you can always install
- 10:49it
- 10:49like at a project root instead of at
- 10:51like a root directory. Um so, that the
- 10:54correct
- 10:55stop hook gets fired at the correct
- 10:57time. But, you know, play around with
- 10:59it. I'm sure you'll run into it and get
- 11:01annoyed by it.
- 11:02So, let's go back and check on our uh
- 11:04Claude and see what's been doing.
- 11:18Okay, looks like it's something
- 11:19starting.
- 11:20Uh looks like we made a test project for
- 11:22like a lint test.
- 11:24And then uh we got post tool hook called
- 11:27it out. We ran it after we made it. And
- 11:30then the stop hook fired.
- 11:32Uh I guess it's telling me that it
- 11:34identified this problem, so then it
- 11:35fixed it.
- 11:37And there we go. We got it working.
- 11:41All right, so it looks like it's been
- 11:42done. Uh the lint hook's been set up. Um
- 11:45it looks like they had some complex
- 11:47logic actually about how we want to
- 11:49actually run the linter. It looks like
- 11:51there's like five failed attempts to fix
- 11:53it so we we don't enter a infinite loop.
- 11:55Um it looks like there might be some
- 11:57issues with npm install.
- 11:59Uh like it it considered these
- 12:01scenarios, which is pretty interesting
- 12:02to me. And you know, it tested it live.
- 12:05It wrote some JavaScript with known lint
- 12:07errors and then watched first to trigger
- 12:10the lint errors, and then it also got
- 12:12blocked. And then the client had to fix
- 12:14it before the turn ended.
- 12:16And so it looks like everything's been
- 12:18created. It even gave me these gaps, but
- 12:22it Yeah, I think it's telling me my
- 12:24existing projects have failures. That's
- 12:26okay, I don't care about that right now.
- 12:29But essentially, yeah, I think it's
- 12:30pretty cool how the agent basically
- 12:33planned it out, considered the other
- 12:36hook that we just created, the pre-hook
- 12:38with npm install, and um you know, here
- 12:41are the files.
- 12:42I'm not going to open them because
- 12:43they're essentially the same thing, and
- 12:45let's be honest, in the age of AI, who
- 12:47actually reads code anymore?
- 12:50So after building all three examples,
- 12:52now we can kind of see how we can
- 12:54further increase our ability to code
- 12:56with AI. You know, you can prompt
- 12:59something to execute the majority of the
- 13:01work, and then you can use hooks to
- 13:03enforce certain behaviors.
- 13:05Because
- 13:07uh Coval always run deterministically no
- 13:10matter what, and so
- 13:11you can rely on code, you can now rely
- 13:13on an LLM. Though they are getting
- 13:15better these days. That being said, I
- 13:17will also encourage engineers to not go
- 13:19too hook crazy and enforce everything.
- 13:22Like the lint ones, that's fine. Um
- 13:25and some of the things that you want to
- 13:27always run
- 13:29would make sense.
- 13:31For example, a lot of a lot of projects
- 13:34I personally worked on,
- 13:36like I alluded to earlier, the agent,
- 13:38especially for long-running sessions,
- 13:40would eventually try to justify its work
- 13:42as being done and then just prematurely
- 13:44end itself.
- 13:46And so, as a result of that, I would
- 13:48argue that hooks in some aspects are
- 13:52kind of like a band-aid to an underlying
- 13:54problem. You should like obviously use
- 13:57them. It's a tool.
- 13:59But fundamentally, I would question
- 14:01you know, what was your prompt or spec
- 14:03that you sent to your agent that caused
- 14:05the problem to begin with? Are you
- 14:07telling the agent to do too much? Or is
- 14:10it unclear what you want it to do? No
- 14:12amount of No amount of deterministic
- 14:14code can help save you if you tell the
- 14:16agent to just implement something
- 14:18without like a full detail spec and then
- 14:20you have a hook to say, "Don't stop
- 14:22until it's complete." If you just give
- 14:24an agent
- 14:25a slot prompt, then you know, no amount
- 14:28of hook will save you from that. Unless
- 14:31the hook has rules already telling you
- 14:33what to do. Yeah, so essentially, hooks
- 14:35help you
- 14:36automate a lot of the dev work that you
- 14:39might have to manually do.
- 14:41Like, you know, running commands to like
- 14:42run linters, for example, but it doesn't
- 14:45take away your planning, your prompting,
- 14:47and your thinking. So, as ultimately, at
- 14:50the end of the day, it is still our
- 14:51responsibilities as engineers to
- 14:54actually look at the output that we
- 14:56produce and make sure that everything is
- 14:58correct. And that's essentially a
- 15:00wrap-up on hooks as part of my agentic
- 15:03engineering fundamental series.
- 15:05I've given you three examples of what to
- 15:07use hooks for. I'd love to hear what you
- 15:09use hooks for in your code base. Please
- 15:11leave them in the comments below.
- 15:13Otherwise, I'll see you on the next
- 15:14video. See you.
About this transcript
This page contains the full transcript of Claude Code Hooks Explained | Agentic Engineering by Josh's DevBox, generated from the public captions YouTube serves with the video. The transcript has 2,587 words across 406 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.