
Lesson Description
The "Claude Code Hooks" Lesson is part of the full, Cursor & Claude Code: Professional AI Setup course featured in this preview video. Here's what you'd learn in this lesson:
Steve explains using Claude Code hooks like PreToolUse, PostsToolUse, and Stop. Claude Code hooks are user-defined shell commands that execute at various points in its lifecycle. They are configured in the settings.json file at the user or project level.
Transcript from the "Claude Code Hooks" Lesson
[00:00:00]
>> Steve Kinney: The other thing you can do is these things called Claude hooks. Literally before, as I was taking the screenshot this morning, they added a new one called Session Start. These are bash commands right now, only that it will run at various different lifecycle moments. So session start, anyone take a lucky guess when that happens?
[00:00:25]
I'll give you a hint, the matchers are startup, resume, and clear. That's when you start a new cloud session. Pre compact happens right before it compacts. So if you wanted to do something, I will be honest with you, this is a relatively new feature. The only one that I've seen people use is on Stop.
[00:00:48]
They either use. There's this thing you can get from Homebrew called Terminal notifier, which will show you a notification. The more ridiculous among us will. And I hooked it up to the Twilio API to text me every time Claude's done with something like, wow, okay, make a sound.
[00:01:05]
Sure. I don't need a text message every time Claude is done with something. But you could do it. And so any of these lifecycle hooks. I have a ridiculous one in this file that I have that I will just show you, because why not? But it's not something I'd necessarily recommend.
[00:01:25]
When the user does a sprint, the one that I was considering that I might do soon is either on Stop. Yeah, I think on stop, I might do one called summarize everything you just learned into a file and you'll see that I have this ridiculous hook right now that is like me iterating towards that plan, which is I have one where I'm listening for everything and writing the payload into a file so I can look at all them and think about it later.
[00:01:56]
Yeah, that's who I am. But arguably the useful ones are pre tool use and post tool Use. Tool is anything that it's going to do, right? That could be calling an MCP server, but you can get a sense of what some of these are. So if it's going to run a shell command.
[00:02:17]
Hey, you're going to run a shell command. Let me look at that. It's git commit, no verify. No, you're not. Absolutely not. You're going to write to eslint config to go comment something out. I'm blocking that, right? You're gonna web fetch and you're gonna an API token in there?
[00:02:44]
[LAUGH] No, so you can kinda use it. And you can always block these rules in general, but you can also, what were you going to. You can block all Git commands. You can block all git commit commands, but if you want to go look at the git commit command, say that four times fast and make sure it doesn't have a no verify flag.
[00:03:07]
Then you wanna pipe it into a Bash script to just check it. And you give it exit code of 2, cuz 0s usually good, 1 is usually used for errors. A lot of times 2 is a little bit more specific. I'm saying stop. It will stop and it will not continue.
[00:03:22]
There's some other things you can do. I'll show you the output in a second. But yeah, if you're gonna edit particular files that you're not allowed to edit, it will get mad at you. Yeah, so on and so forth. Post tool use is like, you just did the thing.
[00:03:42]
This one I use for like, okay, go ESLint that file or go run Prettier on it, honestly, right? Or something along those lines. Or run the test file on that. You can kind of like say, like, you just did a thing. You can step in. My current use cases are pretty easy, which is like eslint the file you just touched, Prettier it.
[00:04:03]
I'm also doing this thing where I log every single one of them to a directory right now so I can look at them later, which is going to make my file look ridiculous. But we'll ignore that for a second. So, yeah, I set an environment variable and I pass them all into the same script.
[00:04:18]
So ignore that for a moment. Did you edit, multi edit or write? Okay, cool. Take the file path. Yes. Gnarly bash command inside of a string. Turns out you can get Claude to write them though, so that's fun. And then if Prettier fails, exit 2 and be like, we have a problem here, right?
[00:04:36]
Cool, run ESLint with a max warnings of zero. So if it triggers a warning, we're in trouble, right? It has to fix its own warnings. If I cause a warning, the build will pass. If it touches a file and it triggers a warning, it's not allowed to do it.
[00:04:54]
So I have more draconian rules for it than I have for myself. It's not fair. It's fine. And then ESLint failed and it'll give you the file and why and send it in to go handle it. So you can kind of like, that's part of the reasons why I can trust fix the lint issues, because it is literally stopped from moving on.
[00:05:11]
Because sometimes, all right, here's Claude Code's bad habit. Fixed all the high priority Things didn't get around to the low priority things. I'm sorry, I didn't specify which ones were optional. That's not your choice to make. Yeah, so some of these are to compel it into good habits, right?
[00:05:40]
There is a library that I haven't played around with as much as I want to yet, but I will under hooks. Somebody made this one. It's a TypeScript library where instead of those long strings of wild bash, you can literally just have some TypeScript that will. You can write Typescript code against this and it will put in the line of bash to have it go to your typescript file, which is effectively what I'm doing at this point when I log them all in there.
[00:06:08]
It was the beginning stages of that. So I can then analyze them with TypeScript and make decisions so I can get a little more. Cuz even I was editing some of those strings of bash by hand and I don't want to anymore. I'll show you what I have, but this library is probably better than what I have.
[00:06:23]
So you should use this instead. But yeah, then each payload you can basically say async function pre tool use. You can just define all the hooks like this and it will then all of the things that pass through actually let you write TypeScript to say if it's okay or not.
[00:06:41]
Another example is like I said, my buddy Rowland does one where it's like every either task as a tool or pre tool use or the user prompt, it says go check the CSV of tasks. If this is a big enough task, isn't just an EslinFix, put it on that and then go to the top priority thing on that list.
[00:07:07]
So you can even tell it to do something and it will go back, it'll put it on the list, but it will continue on what he decided. He's a product manager, so this is his thing is putting stuff in order, and I want what I want now. And he'll have it check in and add things to the list and check stuff off and approve and sort the list, which I think is incredibly powerful.
[00:07:27]
I don't wanna do that. But this is a cool tool. We can just use TypeScript instead of that wild Bash stuff, tell it whether or not it's allowed to continue, so on and so forth. And it handles all the standard in, standard out stuff for you. So I was gonna write this myself and it exists, so I didn't.
[00:07:44]
I had the beginning of it in that file we saw a second ago. Or yeah, I am kinda right now I'm just writing all of them to a file so I could look at what they looked like and I wanna see them in practice. What are the tools that it calls?
[00:08:05]
For me to truly understand all the things you'll see gonna do this task, I'm going to write to a file. I have a ridiculous temp directory right now, which this is just every single thing that it has done, honestly, probably for days,
>> Steve Kinney: Right, and so I can kinda get a sense of what are the things it can do, what are the things I want to respond to, so on and so forth.
[00:08:31]
I don't look at this right now and expect to know anything I don't actually. I'm just recording it right now so I can kind of like pop in and look at it later and try to say like, cool, this is the name of a tool that he uses.
[00:08:41]
Like here's a post tool used for grep and here's the files that I got. I could theoretically take this and like remove certain files I just didn't want it to ever know about. I don't know that I need to do that. I'm just trying to get a sense of what you could do.
[00:08:53]
So I've been recording them for a little bit. Not a lot to take away here, but like again, a way to kind of like try to get an understanding of the inner workings of, of what a given tool does. So it's usually good for blocking or saying, hey, these are all pre tool use.
[00:09:13]
And a post tool use is it just did a Bash. This is one that we totally probably did just a second ago or two. Yeah, this is when it was adding the TRPC server. So it's like, hey, if you're gonna add stuff, I could theoretically respond it with a check in with me first.
[00:09:31]
Now it did. It was part of the plan. I approved it. But you could, if there are things that are happening that you don't want to happen, you could theoretically step in and either say you're never allowed to do this or have some introspection on like when you are and are not allowed to do it.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops