YouTube2Text

Claude Code Hooks Explained | Agentic Engineering — Transcript

by Josh's DevBox · 2,587 words · 406 segments · language en · Watch on YouTube

Full transcript

  1. 0:00AI or LLM's are probabilistic models,
  2. 0:04which means the model decides what to do
  3. 0:06next. It generally it follows basic
  4. 0:09instructions pretty well, but it's not
  5. 0:11guaranteed. The same prompt can yield
  6. 0:13multiple different things.
  7. 0:15And unfortunately, the longer the prompt
  8. 0:17gets, the longer your context window
  9. 0:18becomes, and the more often that your AI
  10. 0:21might start drifting and forgetting
  11. 0:23basic instructions.
  12. 0:25Now, unfortunately, for those of us who
  13. 0:27wants to run long agentic loops, um that
  14. 0:30can mean that we might start forgetting
  15. 0:32certain things or certain things might
  16. 0:33not happen. And
  17. 0:35and that's why Claude introduced the
  18. 0:37concept of hooks. And then by the end of
  19. 0:39this video, you'll know about three
  20. 0:40types of hooks, when when to use them,
  21. 0:43and basically how to set it up so that
  22. 0:46your agents will give you the same
  23. 0:47results every single time without you
  24. 0:50needing to reprompt it and do everything
  25. 0:51to keep a long-running loop.
  26. 0:54Anything that we tell the agent is
  27. 0:56context. It's basically just a prompt
  28. 0:59essentially or some skill file or rules
  29. 1:02that the model reads and then acts upon.
  30. 1:05However, hooks are a little bit
  31. 1:07different.
  32. 1:08A hook on the hand is something that
  33. 1:10Claude code uses to run at a fixed point
  34. 1:12in a in the agent's loop. So,
  35. 1:16there are phases like you can
  36. 1:18essentially hook your code into like
  37. 1:20before a tool runs, after it runs, or
  38. 1:23even points to stop the agent from
  39. 1:26finishing before a condition is met. An
  40. 1:28important part here is that Claude does
  41. 1:30not decide when the hook runs. So, it
  42. 1:33runs every single time in a
  43. 1:35deterministic way. So, however long the
  44. 1:37session gets, you will always get the
  45. 1:40same results.
  46. 1:41But before we continue talking, hi, I'm
  47. 1:43Josh. I'm a software developer, and I'm
  48. 1:46making it my personal mission to use AI
  49. 1:48to automate all the boring work so we
  50. 1:50can focus on what matters.
  51. 1:52Before we look at our different
  52. 1:54examples, I want to paint a mental model
  53. 1:56of what hooks are because it's not
  54. 1:58something we can just go to Claude and
  55. 2:00have a fancy UI gets created. So, hooks
  56. 2:03essentially are just like we said
  57. 2:07before, code that gets tied to
  58. 2:10tool usages in Claude. So, tool usages
  59. 2:12could be like, you know, grep, bash, and
  60. 2:15any other custom tools that you might
  61. 2:17have made, maybe writes, edits to files,
  62. 2:20and things like that. For every hook,
  63. 2:22you can define a
  64. 2:25rules to execute certain code on a on
  65. 2:28your tool. We will have some of our
  66. 2:30common executions like before every tool
  67. 2:32gets ran, we can fire a specific code to
  68. 2:34do something. After a tool gets ran, we
  69. 2:36can fire another hook. And then we can
  70. 2:39also fire hooks when the agent is done.
  71. 2:43There's actually 33 hooks currently in
  72. 2:45the documentation, but we're just going
  73. 2:46to go over these three.
  74. 2:49There are three types of hooks I want to
  75. 2:50go through. We'll build each one live,
  76. 2:52run it on our new project, and just see
  77. 2:54what happens
  78. 2:56as we execute.
  79. 2:58So, the the first example of a hook that
  80. 3:00we're going through is a block command.
  81. 3:02So, what happened to me before in
  82. 3:04several instances where I would tell
  83. 3:06Claude to go do something, and then in
  84. 3:09its infinite wisdom, it decided to npm
  85. 3:11install different packages or actually
  86. 3:13take control of my browser and go
  87. 3:14download different files.
  88. 3:17So, in this instance, I'm going to show
  89. 3:18you how we can create a a hook to stop
  90. 3:21certain commands from being ran. And
  91. 3:23it's good for security perspectives and
  92. 3:25things like that.
  93. 3:26So, in this instance, we're going to use
  94. 3:28the pre-tool use hook. So, before we use
  95. 3:31something like npm install or we would
  96. 3:35double-check the context and then block
  97. 3:37it from happening.
  98. 3:39So, in this instance, we can create a
  99. 3:40hook that would trigger before npm gets
  100. 3:44called, and we can define things like,
  101. 3:46you know, rules and criterias on what is
  102. 3:48installed and what's not installed, so
  103. 3:49we don't completely stop ourselves from
  104. 3:52installing files, but maybe stop things
  105. 3:54that we don't or didn't expect.
  106. 3:58So, here's what this guard will look
  107. 4:00like and
  108. 4:01and all hooks are essentially say a JSON
  109. 4:04file where you kind of define what gets
  110. 4:07triggered. So, we can just go through
  111. 4:08the example right here. So, we define a
  112. 4:10pre-tool use
  113. 4:12and then you define tools that gets
  114. 4:14executed that we want to
  115. 4:17uh fire the hook for. Some basic things
  116. 4:19are like bad, PowerShell, writes, things
  117. 4:21like that.
  118. 4:23Um and then define the hook itself. In
  119. 4:25this instance, whenever this condition
  120. 4:28gets triggered, we just want to
  121. 4:31uh execute a script. Uh this script you
  122. 4:34can
  123. 4:36do things like read or define what
  124. 4:39should or should not be installed or
  125. 4:41basically anything you want.
  126. 4:43Like it's your own logic, but that's
  127. 4:45hooks in a nutshell. You don't have to
  128. 4:48know much about this, which is actually
  129. 4:49amazing with AI these days. Uh just ask
  130. 4:52Claude and Claude will figure it out for
  131. 4:53you.
  132. 4:55But, let's go to the demo.
  133. 4:56Okay, so now we're on Claude code. Let's
  134. 4:58go through the example of creating a
  135. 5:00block hook.
  136. 5:02And let's see what happens.
  137. 5:08Yeah, so here you can see some of the
  138. 5:09logic here. We can define specific
  139. 5:11packages that are not allowed to be
  140. 5:13installed. So, we can have an allow list
  141. 5:15already.
  142. 5:17Inside that's looks like that's what the
  143. 5:19agent decided it's going to do.
  144. 5:21Looks like it ran into some work tree
  145. 5:22problems and get repo issue, but that's
  146. 5:24okay.
  147. 5:32Uh as a bonus, it looks like the code
  148. 5:34actually ran into a
  149. 5:36a bug and decided to fix itself. So,
  150. 5:38that's pretty cool.
  151. 5:42All right, it is completed. Let's see
  152. 5:44what they what we made.
  153. 5:47So, it looks like it created a allow
  154. 5:49list for our hook that already allowed
  155. 5:51241 packages across in all the projects
  156. 5:54that I already have. So, that's pretty
  157. 5:56cool.
  158. 5:58Um it ran NPM install and other things
  159. 6:01and it blocked it.
  160. 6:04And looks like our typical NPM command
  161. 6:06still works. Uh whenever we scroll up,
  162. 6:08we can see the show commands and see
  163. 6:09what happens.
  164. 6:11Yeah, here I think it's trying to do NPM
  165. 6:14install right here. And then you can see
  166. 6:15right here that
  167. 6:17the command got blocked by the NPM
  168. 6:19guard. So, they were trying to install
  169. 6:20some things like NPM install sketchy
  170. 6:22thing. Well, I'm glad we caught it. Uh
  171. 6:24we didn't install sketchy thing.
  172. 6:26But, we see that the hook is doing its
  173. 6:28job properly. You don't have to you
  174. 6:30don't have to take Cloud's code word for
  175. 6:32it because we just scrolled up, but it
  176. 6:34tested everything that we just talked
  177. 6:36about.
  178. 6:38All right, here on VS Code, this is
  179. 6:40essentially everything that was created.
  180. 6:42I'm not even going to pretend that we
  181. 6:44know what any of this code is, but uh
  182. 6:48now this is the code that gets triggered
  183. 6:50every time and the NPM command gets
  184. 6:52called. I'm I'm going to guess this is
  185. 6:54just parsing through our list parsing
  186. 6:56through the
  187. 6:58the package and then just look for our
  188. 6:59allow list. And so, here's just a list
  189. 7:01of things that we support or that we can
  190. 7:04install in our packages.
  191. 7:06And then here's the hook itself.
  192. 7:08And you see right here
  193. 7:09pre-tool use
  194. 7:11uh NPM guard.
  195. 7:13So, that's the power of the block hook.
  196. 7:15It helps prevent you from installing
  197. 7:17sketchy uh NPM packages
  198. 7:19among many other things.
  199. 7:22Now, for the next example, this is
  200. 7:23something I'm actually very excited
  201. 7:25about. Uh making sure that you run lint
  202. 7:27after every time we write code.
  203. 7:31To achieve this, we're going to use
  204. 7:32something called a post-tool use. Like
  205. 7:34the name suggests, it runs after every
  206. 7:37call succeed. Same rules that we talked
  207. 7:39about, except in this case the matcher
  208. 7:42is edit and write.
  209. 7:44And the only way that it can finish and
  210. 7:46exit the hook is that we see that the
  211. 7:49linter is ran.
  212. 7:51So, you can imagine that this is very
  213. 7:52good for long-running agentic sessions.
  214. 7:54You write some code. Um the AI have no
  215. 7:57way to know if the code is right or not
  216. 7:59or conform to any standards unless, you
  217. 8:01know, the model itself is smart. But, by
  218. 8:03putting this post hook tool, we force
  219. 8:06the model to run our linters and
  220. 8:07everything going. And this is how you
  221. 8:09have strong code that's
  222. 8:11deterministically enforced wanting you
  223. 8:14to re-prompt Claude.
  224. 8:16So, here's a brief example of what that
  225. 8:19would look like here.
  226. 8:20Not anything too different from what we
  227. 8:22already looked at. Only main difference
  228. 8:24is a post tool use versus a pre-tool
  229. 8:26use.
  230. 8:28I know, rocket science. But, let's go
  231. 8:29through a quick example.
  232. 8:31I'm just going back to our same Claude
  233. 8:33session. I'm just going to say, "Next
  234. 8:37help create a
  235. 8:40linter hook that makes sure that we run
  236. 8:45lint on any of our projects and fix it
  237. 8:50before
  238. 8:52we are done writing."
  239. 8:58All right. Uh let's see if it
  240. 8:59understands my broken English and let's
  241. 9:02we'll just continue and wait.
  242. 9:08Oh, well, looks like we skipped to the
  243. 9:10second example.
  244. 9:11Uh based on my wording, it said to make
  245. 9:13the post tool use to run lint. And it
  246. 9:15also decided to make a stop hook to
  247. 9:18uh make sure that the linter runs. So,
  248. 9:21um I guess while it runs, let's just go
  249. 9:24through it uh
  250. 9:25quick recap on what a stop hook is.
  251. 9:29Uh like the name suggests, uh when
  252. 9:31Claude is done with their turn, uh stop
  253. 9:33hooks are used to stop Claude from
  254. 9:35stopping.
  255. 9:36Now, say that two times fast.
  256. 9:38And the only way to uh
  257. 9:41exit it is basically the flag or there's
  258. 9:43a counter for like, you know, you tried
  259. 9:45XYZ times
  260. 9:47and then that will basically
  261. 9:50force Claude to just go back and try
  262. 9:52again. Of course, I will say as you
  263. 9:54continue playing around with stop hooks,
  264. 9:56you're going to run into these uh
  265. 9:58situations where especially for longer
  266. 9:59running sessions where uh Claude would
  267. 10:02try to justify why the work is done and
  268. 10:06start making up reasons or doing things
  269. 10:08to end prematurely before actually going
  270. 10:11back. So, something to watch out for
  271. 10:13when you go for long running sessions.
  272. 10:16It's basically the same thing as we've
  273. 10:18been doing. You create stop and then uh
  274. 10:21once it's installed, you just execute
  275. 10:24some code that gets triggered every
  276. 10:26single time. Uh there's no matches or
  277. 10:30anything. It's always fired. Uh so, that
  278. 10:32will lead into other interesting
  279. 10:33problems when you start running it on
  280. 10:35your projects where uh
  281. 10:38it might you might start seeing
  282. 10:40uh stop hooks that's for unrelated
  283. 10:43project interfering with different uh
  284. 10:46code bases and things that you're
  285. 10:47working in. So, you can always install
  286. 10:49it
  287. 10:49like at a project root instead of at
  288. 10:51like a root directory. Um so, that the
  289. 10:54correct
  290. 10:55stop hook gets fired at the correct
  291. 10:57time. But, you know, play around with
  292. 10:59it. I'm sure you'll run into it and get
  293. 11:01annoyed by it.
  294. 11:02So, let's go back and check on our uh
  295. 11:04Claude and see what's been doing.
  296. 11:18Okay, looks like it's something
  297. 11:19starting.
  298. 11:20Uh looks like we made a test project for
  299. 11:22like a lint test.
  300. 11:24And then uh we got post tool hook called
  301. 11:27it out. We ran it after we made it. And
  302. 11:30then the stop hook fired.
  303. 11:32Uh I guess it's telling me that it
  304. 11:34identified this problem, so then it
  305. 11:35fixed it.
  306. 11:37And there we go. We got it working.
  307. 11:41All right, so it looks like it's been
  308. 11:42done. Uh the lint hook's been set up. Um
  309. 11:45it looks like they had some complex
  310. 11:47logic actually about how we want to
  311. 11:49actually run the linter. It looks like
  312. 11:51there's like five failed attempts to fix
  313. 11:53it so we we don't enter a infinite loop.
  314. 11:55Um it looks like there might be some
  315. 11:57issues with npm install.
  316. 11:59Uh like it it considered these
  317. 12:01scenarios, which is pretty interesting
  318. 12:02to me. And you know, it tested it live.
  319. 12:05It wrote some JavaScript with known lint
  320. 12:07errors and then watched first to trigger
  321. 12:10the lint errors, and then it also got
  322. 12:12blocked. And then the client had to fix
  323. 12:14it before the turn ended.
  324. 12:16And so it looks like everything's been
  325. 12:18created. It even gave me these gaps, but
  326. 12:22it Yeah, I think it's telling me my
  327. 12:24existing projects have failures. That's
  328. 12:26okay, I don't care about that right now.
  329. 12:29But essentially, yeah, I think it's
  330. 12:30pretty cool how the agent basically
  331. 12:33planned it out, considered the other
  332. 12:36hook that we just created, the pre-hook
  333. 12:38with npm install, and um you know, here
  334. 12:41are the files.
  335. 12:42I'm not going to open them because
  336. 12:43they're essentially the same thing, and
  337. 12:45let's be honest, in the age of AI, who
  338. 12:47actually reads code anymore?
  339. 12:50So after building all three examples,
  340. 12:52now we can kind of see how we can
  341. 12:54further increase our ability to code
  342. 12:56with AI. You know, you can prompt
  343. 12:59something to execute the majority of the
  344. 13:01work, and then you can use hooks to
  345. 13:03enforce certain behaviors.
  346. 13:05Because
  347. 13:07uh Coval always run deterministically no
  348. 13:10matter what, and so
  349. 13:11you can rely on code, you can now rely
  350. 13:13on an LLM. Though they are getting
  351. 13:15better these days. That being said, I
  352. 13:17will also encourage engineers to not go
  353. 13:19too hook crazy and enforce everything.
  354. 13:22Like the lint ones, that's fine. Um
  355. 13:25and some of the things that you want to
  356. 13:27always run
  357. 13:29would make sense.
  358. 13:31For example, a lot of a lot of projects
  359. 13:34I personally worked on,
  360. 13:36like I alluded to earlier, the agent,
  361. 13:38especially for long-running sessions,
  362. 13:40would eventually try to justify its work
  363. 13:42as being done and then just prematurely
  364. 13:44end itself.
  365. 13:46And so, as a result of that, I would
  366. 13:48argue that hooks in some aspects are
  367. 13:52kind of like a band-aid to an underlying
  368. 13:54problem. You should like obviously use
  369. 13:57them. It's a tool.
  370. 13:59But fundamentally, I would question
  371. 14:01you know, what was your prompt or spec
  372. 14:03that you sent to your agent that caused
  373. 14:05the problem to begin with? Are you
  374. 14:07telling the agent to do too much? Or is
  375. 14:10it unclear what you want it to do? No
  376. 14:12amount of No amount of deterministic
  377. 14:14code can help save you if you tell the
  378. 14:16agent to just implement something
  379. 14:18without like a full detail spec and then
  380. 14:20you have a hook to say, "Don't stop
  381. 14:22until it's complete." If you just give
  382. 14:24an agent
  383. 14:25a slot prompt, then you know, no amount
  384. 14:28of hook will save you from that. Unless
  385. 14:31the hook has rules already telling you
  386. 14:33what to do. Yeah, so essentially, hooks
  387. 14:35help you
  388. 14:36automate a lot of the dev work that you
  389. 14:39might have to manually do.
  390. 14:41Like, you know, running commands to like
  391. 14:42run linters, for example, but it doesn't
  392. 14:45take away your planning, your prompting,
  393. 14:47and your thinking. So, as ultimately, at
  394. 14:50the end of the day, it is still our
  395. 14:51responsibilities as engineers to
  396. 14:54actually look at the output that we
  397. 14:56produce and make sure that everything is
  398. 14:58correct. And that's essentially a
  399. 15:00wrap-up on hooks as part of my agentic
  400. 15:03engineering fundamental series.
  401. 15:05I've given you three examples of what to
  402. 15:07use hooks for. I'd love to hear what you
  403. 15:09use hooks for in your code base. Please
  404. 15:11leave them in the comments below.
  405. 15:13Otherwise, I'll see you on the next
  406. 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.