Okay, so next up we're going to look at hooks, which is one of the kind of pro features of
Cloud Code, which most of the time you don't need. But if you suddenly think, you know what,
I could really use that, it's a cool thing to know that you can do. And I would suggest that
when that happens, you would then bring up Cloud Code's docs and just refresh your memory. So I
want to give you a sense of how it works, and we'll make one and you'll get a sort of the gist
of it. But don't feel like you have to remember all of this. It's just one of those things that
if you need it, you know how to do it, you know where to go and find out more. So a hook is
something which in a certain circumstance can kick off making Cloud Code do something. And
those circumstances are called events. And they're things like when Cloud is about to make a make a
tool call, maybe it's about to run a shell command, or when it's just finishing work, it just thinks
I'm done now. It's moments like that, they're considered events, and they can trigger a hook,
they can say, okay, something's happened. And I want to make sure that Cloud Code always when
that happens, it calls something something that I've fixed in advanced. That is what a hook is
all about an event triggering something that you want. So the kind of classic examples of it,
there's this thing that it constantly seems to forget with me that I wanted to use a UV run
instead of Python, it always goes back to Python, which is the older pre UV way of doing it,
you could add a hook so that before it calls a tool, any shell script, you always check if it's
doing Python something. And if so, you say no, not Python, always UV. That would be an example of a
time where you might want to use a hook. Another one from from Claude's documentation, they have
one where if it's ever stopping to ask for your permission, it could send you a notification
so that you could just leave it running and go off to the other room. And rather than coming back two
hours later and realizing it's not made any progress, it's been waiting for you, you would
get some kind of notification. So that's a cool one. So things like that are time to use hooks.
And then one of the absolute standard ones that are very common that we're going to use now,
because it's so useful, is having a hook that's triggered by an event of when Claude has finished
doing its work. When Claude is finished, we can seize the moment to do something else like a
review. And as it happens, this is the way that the Ralph loop thing is implemented. I believe
they've done it with a hook so that as soon as it finishes work, it's then sent a new
prompt that says, hey, I don't think you've finished. Try again. And that's how it works.
So we're going to use a hook. Now we're going to set one up to do a review at the end of any piece
of work. And like so many things that could belong in a few places, but .claude is usually the place
to go. It's in the .claude folder. And you may already have a file called just settings.json.
But if not, I'm going to make that file right now. I'm going to make a file called settings.json.
And this is the file into which you can put your hooks. And we're going to tell this hook
that we want the event to be when Claude is stopping. And we're going to tell it what we
want to do. And when you say what you want to do, you've got three different types of thing.
And you don't need to commit this to memory. Just have a sense of it. The three different
things you could do is run a shell command. Just give a command that needs to be run at that point.
You could make a prompt to Claude. It could be some instructions you want to give Claude.
Or it could be an agent. You want Claude to spawn a sub-agent to do something. And that way,
it's kept off the context. Those are the three things that you could have triggered by an event
as part of a hook. That's the terminology. That's how it fits together. And if in doubt,
you can always just Google Claude code hooks and read their docs. They're super clear. But now,
let's make one that is going to do a code review whenever we hit a stopping point with Claude.
Okay. So, I've pasted in the JSON that describes the hooks. You can also do this interactively by
going to slash hooks. You can then interactively. These are the different events that can trigger a
hook. Pre tool use, post tool use, post tool use, user failure, notification, user prompt submit,
session start, and stop. And we're going to add one for stop. You can see it here. But you can
add them. I can come in here and I can add a new hook that way. So, you can do it all through the
menus. But we're doing one for stop. And then for stop, for that event, we get to choose what are
the hooks that you want to be triggered when that happens. You can also, should you wish, put in
a matcher in here, which gives the circumstances, a sort of filter on under what circumstances would
this hook be called. But if you need to do that, you can look up the docs. All right. So, then you
specify what do you want to happen when Claude code stops? Well, we want to run a command. Now,
as I say, three things can go there. Command, prompt, or agents. And the prompt and agents
ones are a little bit fiddly. They have some constraints on the permissions that it has. So,
you can't do things like writing to a file. It just matters how it responds. So, there's various
things you can do. But it takes a lot of experimenting to get it to work predictably
and robustly. The easiest one is command, which runs a shell command. And this is pretty bullet
proof. And as it happens, at least I have a really cool shell command to run. You may not have done
this one. But if you have, then you can experiment this with me. Because it's so cool. We can run
that codex exec command. That is going to run a command on the shell prompt, which is actually
going to launch a separate agent, a codex agent. And it's going to call codex exec. And here comes
the same thing as before. But I'm just slightly twisting it. I'm not saying review the plan.md.
I'm saying review the changes since the last commit and write results to a file named planning
review.md. That is the command. So, I've put that in there as my hook. That's what we're
going to be running. And just to show it, if I go slash hooks to look in at my hooks,
and I come all the way down to the stop hooks. Oh, there are more hooks, actually. I'd stopped
after that. But there's also subagent start and stop. There are hooks you can have for when it's
about to compact. So, just before it compacts the context, you could shove in extra stuff,
reminders. You want to stay in there. The session end is when an entire session is ending,
when it's asking for permission, when it's setting up. And then here are some other things. Task
completed. All right. Anyway, we are going into stop. And you can see that that codex exec
is in there. That is the one that I'm showing you on the screen here. By the way, I should also
mention I just deleted the command and the subagent that we configured earlier to keep things nice and
simple, so there aren't multiple ways to review changes and things. And so, we don't have this in
two places. So, we've just got this hook, and it's time to try it out. And so, now I'm going to say,
please make a concise readme.md for the project. And so, I'm just running that command in normal
Claude code. And so, Claude is going to go ahead and make a readme. It's going to search to see
what's going on and then make a nice concise readme. And what we're hoping to see is that
when it's finished, it's going to trigger this stop hook. And we should see that here. And what
we probably won't see straight here, but what we should know is that it's then going to spawn
codex behind the scenes. So, it wants to make this edit. I'm going to say yes, go ahead and
make that edit. And now, now behind the scenes, done. It's finished. It's running the stop hook.
And so, that stop hook is now running. And what we know is actually happening is that that is
launching codex separately, running this command you see here. Codex is going to look at what's
changed since the last commit. And codex is going to use that to write a file, review.md,
and it completes. And that's really cool. That all happened as a command launched as part of
this stop hook. And you can see that what Claude says is that Claude is done. The readme covers
the essentials. Claude isn't aware that codex has come in and done a code review. And it also,
part of that review, it's noticed that I removed, like I said, the sub-agent and I removed the
command that we don't need anymore. And it sees that as a potential workflow regression. And then
the readme that mentions that it's got some things that haven't yet been implemented, but that
doesn't sound too severe. But it's cool that everything works. We've got this workflow happening.
And that is what hooks are. And as I say, the main thing about hooks is that you shouldn't use them
unless you need to. They're one of those extra complexities that might be handy for your situation.
But wait until you need it. If so, skim through the docs. Now that you've got the sort of basic
lay of the land, you understand that settings.json, or you can do it through the menus, and that you
have an event that will trigger a hook, and that you can have command or a prompt or an agent
that is triggered as a result, the command is the most predictable one.
That's basically what you need to know.