1
00:00:00,000 --> 00:00:04,239
Okay, so next up we're going to look at hooks, which is one of the kind of pro features of

2
00:00:04,239 --> 00:00:09,039
Cloud Code, which most of the time you don't need. But if you suddenly think, you know what,

3
00:00:09,039 --> 00:00:13,840
I could really use that, it's a cool thing to know that you can do. And I would suggest that

4
00:00:13,840 --> 00:00:18,079
when that happens, you would then bring up Cloud Code's docs and just refresh your memory. So I

5
00:00:18,079 --> 00:00:22,159
want to give you a sense of how it works, and we'll make one and you'll get a sort of the gist

6
00:00:22,159 --> 00:00:25,600
of it. But don't feel like you have to remember all of this. It's just one of those things that

7
00:00:25,600 --> 00:00:31,200
if you need it, you know how to do it, you know where to go and find out more. So a hook is

8
00:00:31,200 --> 00:00:36,720
something which in a certain circumstance can kick off making Cloud Code do something. And

9
00:00:36,720 --> 00:00:41,360
those circumstances are called events. And they're things like when Cloud is about to make a make a

10
00:00:41,360 --> 00:00:46,319
tool call, maybe it's about to run a shell command, or when it's just finishing work, it just thinks

11
00:00:46,319 --> 00:00:53,040
I'm done now. It's moments like that, they're considered events, and they can trigger a hook,

12
00:00:53,040 --> 00:00:58,000
they can say, okay, something's happened. And I want to make sure that Cloud Code always when

13
00:00:58,000 --> 00:01:03,680
that happens, it calls something something that I've fixed in advanced. That is what a hook is

14
00:01:03,680 --> 00:01:09,120
all about an event triggering something that you want. So the kind of classic examples of it,

15
00:01:09,120 --> 00:01:15,839
there's this thing that it constantly seems to forget with me that I wanted to use a UV run

16
00:01:15,839 --> 00:01:21,440
instead of Python, it always goes back to Python, which is the older pre UV way of doing it,

17
00:01:21,440 --> 00:01:26,400
you could add a hook so that before it calls a tool, any shell script, you always check if it's

18
00:01:26,400 --> 00:01:33,120
doing Python something. And if so, you say no, not Python, always UV. That would be an example of a

19
00:01:33,120 --> 00:01:36,879
time where you might want to use a hook. Another one from from Claude's documentation, they have

20
00:01:36,879 --> 00:01:42,480
one where if it's ever stopping to ask for your permission, it could send you a notification

21
00:01:42,480 --> 00:01:46,559
so that you could just leave it running and go off to the other room. And rather than coming back two

22
00:01:46,559 --> 00:01:50,239
hours later and realizing it's not made any progress, it's been waiting for you, you would

23
00:01:50,239 --> 00:01:55,599
get some kind of notification. So that's a cool one. So things like that are time to use hooks.

24
00:01:55,599 --> 00:02:00,319
And then one of the absolute standard ones that are very common that we're going to use now,

25
00:02:00,319 --> 00:02:06,480
because it's so useful, is having a hook that's triggered by an event of when Claude has finished

26
00:02:06,480 --> 00:02:11,679
doing its work. When Claude is finished, we can seize the moment to do something else like a

27
00:02:11,679 --> 00:02:17,520
review. And as it happens, this is the way that the Ralph loop thing is implemented. I believe

28
00:02:17,520 --> 00:02:23,039
they've done it with a hook so that as soon as it finishes work, it's then sent a new

29
00:02:23,039 --> 00:02:27,679
prompt that says, hey, I don't think you've finished. Try again. And that's how it works.

30
00:02:27,679 --> 00:02:32,880
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

31
00:02:32,880 --> 00:02:37,839
of work. And like so many things that could belong in a few places, but .claude is usually the place

32
00:02:37,839 --> 00:02:44,240
to go. It's in the .claude folder. And you may already have a file called just settings.json.

33
00:02:44,240 --> 00:02:51,039
But if not, I'm going to make that file right now. I'm going to make a file called settings.json.

34
00:02:51,919 --> 00:02:57,360
And this is the file into which you can put your hooks. And we're going to tell this hook

35
00:02:57,360 --> 00:03:02,399
that we want the event to be when Claude is stopping. And we're going to tell it what we

36
00:03:02,399 --> 00:03:07,199
want to do. And when you say what you want to do, you've got three different types of thing.

37
00:03:07,199 --> 00:03:10,720
And you don't need to commit this to memory. Just have a sense of it. The three different

38
00:03:10,720 --> 00:03:16,240
things you could do is run a shell command. Just give a command that needs to be run at that point.

39
00:03:16,240 --> 00:03:20,960
You could make a prompt to Claude. It could be some instructions you want to give Claude.

40
00:03:20,960 --> 00:03:27,759
Or it could be an agent. You want Claude to spawn a sub-agent to do something. And that way,

41
00:03:27,759 --> 00:03:33,919
it's kept off the context. Those are the three things that you could have triggered by an event

42
00:03:33,919 --> 00:03:38,559
as part of a hook. That's the terminology. That's how it fits together. And if in doubt,

43
00:03:38,559 --> 00:03:44,320
you can always just Google Claude code hooks and read their docs. They're super clear. But now,

44
00:03:44,320 --> 00:03:50,559
let's make one that is going to do a code review whenever we hit a stopping point with Claude.

45
00:03:50,559 --> 00:03:56,160
Okay. So, I've pasted in the JSON that describes the hooks. You can also do this interactively by

46
00:03:56,160 --> 00:04:02,320
going to slash hooks. You can then interactively. These are the different events that can trigger a

47
00:04:02,320 --> 00:04:08,880
hook. Pre tool use, post tool use, post tool use, user failure, notification, user prompt submit,

48
00:04:08,880 --> 00:04:14,080
session start, and stop. And we're going to add one for stop. You can see it here. But you can

49
00:04:14,080 --> 00:04:18,640
add them. I can come in here and I can add a new hook that way. So, you can do it all through the

50
00:04:18,640 --> 00:04:24,160
menus. But we're doing one for stop. And then for stop, for that event, we get to choose what are

51
00:04:24,160 --> 00:04:30,160
the hooks that you want to be triggered when that happens. You can also, should you wish, put in

52
00:04:30,720 --> 00:04:37,359
a matcher in here, which gives the circumstances, a sort of filter on under what circumstances would

53
00:04:37,359 --> 00:04:41,760
this hook be called. But if you need to do that, you can look up the docs. All right. So, then you

54
00:04:41,760 --> 00:04:47,920
specify what do you want to happen when Claude code stops? Well, we want to run a command. Now,

55
00:04:47,920 --> 00:04:53,200
as I say, three things can go there. Command, prompt, or agents. And the prompt and agents

56
00:04:53,200 --> 00:04:57,920
ones are a little bit fiddly. They have some constraints on the permissions that it has. So,

57
00:04:57,920 --> 00:05:02,720
you can't do things like writing to a file. It just matters how it responds. So, there's various

58
00:05:02,720 --> 00:05:07,119
things you can do. But it takes a lot of experimenting to get it to work predictably

59
00:05:07,119 --> 00:05:12,640
and robustly. The easiest one is command, which runs a shell command. And this is pretty bullet

60
00:05:12,640 --> 00:05:18,399
proof. And as it happens, at least I have a really cool shell command to run. You may not have done

61
00:05:18,399 --> 00:05:22,799
this one. But if you have, then you can experiment this with me. Because it's so cool. We can run

62
00:05:22,799 --> 00:05:28,880
that codex exec command. That is going to run a command on the shell prompt, which is actually

63
00:05:28,880 --> 00:05:36,000
going to launch a separate agent, a codex agent. And it's going to call codex exec. And here comes

64
00:05:36,000 --> 00:05:42,799
the same thing as before. But I'm just slightly twisting it. I'm not saying review the plan.md.

65
00:05:42,799 --> 00:05:49,679
I'm saying review the changes since the last commit and write results to a file named planning

66
00:05:49,760 --> 00:05:56,079
review.md. That is the command. So, I've put that in there as my hook. That's what we're

67
00:05:56,079 --> 00:06:00,480
going to be running. And just to show it, if I go slash hooks to look in at my hooks,

68
00:06:00,480 --> 00:06:06,239
and I come all the way down to the stop hooks. Oh, there are more hooks, actually. I'd stopped

69
00:06:06,239 --> 00:06:11,359
after that. But there's also subagent start and stop. There are hooks you can have for when it's

70
00:06:11,359 --> 00:06:16,799
about to compact. So, just before it compacts the context, you could shove in extra stuff,

71
00:06:16,799 --> 00:06:21,920
reminders. You want to stay in there. The session end is when an entire session is ending,

72
00:06:21,920 --> 00:06:26,720
when it's asking for permission, when it's setting up. And then here are some other things. Task

73
00:06:26,720 --> 00:06:32,559
completed. All right. Anyway, we are going into stop. And you can see that that codex exec

74
00:06:32,559 --> 00:06:37,359
is in there. That is the one that I'm showing you on the screen here. By the way, I should also

75
00:06:37,359 --> 00:06:43,920
mention I just deleted the command and the subagent that we configured earlier to keep things nice and

76
00:06:43,920 --> 00:06:48,480
simple, so there aren't multiple ways to review changes and things. And so, we don't have this in

77
00:06:48,480 --> 00:06:52,559
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,

78
00:06:52,559 --> 00:07:02,880
please make a concise readme.md for the project. And so, I'm just running that command in normal

79
00:07:02,880 --> 00:07:07,920
Claude code. And so, Claude is going to go ahead and make a readme. It's going to search to see

80
00:07:07,920 --> 00:07:11,679
what's going on and then make a nice concise readme. And what we're hoping to see is that

81
00:07:11,679 --> 00:07:17,679
when it's finished, it's going to trigger this stop hook. And we should see that here. And what

82
00:07:17,679 --> 00:07:22,320
we probably won't see straight here, but what we should know is that it's then going to spawn

83
00:07:22,880 --> 00:07:27,119
codex behind the scenes. So, it wants to make this edit. I'm going to say yes, go ahead and

84
00:07:27,119 --> 00:07:34,079
make that edit. And now, now behind the scenes, done. It's finished. It's running the stop hook.

85
00:07:34,079 --> 00:07:38,640
And so, that stop hook is now running. And what we know is actually happening is that that is

86
00:07:38,640 --> 00:07:44,320
launching codex separately, running this command you see here. Codex is going to look at what's

87
00:07:44,320 --> 00:07:51,040
changed since the last commit. And codex is going to use that to write a file, review.md,

88
00:07:51,040 --> 00:07:57,519
and it completes. And that's really cool. That all happened as a command launched as part of

89
00:07:57,519 --> 00:08:02,079
this stop hook. And you can see that what Claude says is that Claude is done. The readme covers

90
00:08:02,079 --> 00:08:11,839
the essentials. Claude isn't aware that codex has come in and done a code review. And it also,

91
00:08:12,399 --> 00:08:18,320
part of that review, it's noticed that I removed, like I said, the sub-agent and I removed the

92
00:08:18,320 --> 00:08:24,640
command that we don't need anymore. And it sees that as a potential workflow regression. And then

93
00:08:24,640 --> 00:08:30,880
the readme that mentions that it's got some things that haven't yet been implemented, but that

94
00:08:30,880 --> 00:08:35,919
doesn't sound too severe. But it's cool that everything works. We've got this workflow happening.

95
00:08:35,919 --> 00:08:41,359
And that is what hooks are. And as I say, the main thing about hooks is that you shouldn't use them

96
00:08:41,359 --> 00:08:47,039
unless you need to. They're one of those extra complexities that might be handy for your situation.

97
00:08:47,039 --> 00:08:52,000
But wait until you need it. If so, skim through the docs. Now that you've got the sort of basic

98
00:08:52,000 --> 00:08:56,880
lay of the land, you understand that settings.json, or you can do it through the menus, and that you

99
00:08:56,880 --> 00:09:01,520
have an event that will trigger a hook, and that you can have command or a prompt or an agent

100
00:09:01,520 --> 00:09:05,280
that is triggered as a result, the command is the most predictable one.

101
00:09:05,280 --> 00:09:07,280
That's basically what you need to know.