Transcript for:
Claude Code Hooks Overview

Claude code hooks. By default, cloud code is more popular and powerful to create any application, build any application and modify any application right within your ID and also from CLI. But the issue is that clot code can make mistakes and also you have custom filters or custom rules within your organization to follow. That's when we have cloud code hooks. So here are some examples. You can do automatic formatting. You can log all the things happening behind the scenes. You can provide automated feedback when clot code produce code that does not follow your codebased conventions. You can block modification to production files or sensitive directories. Also, you are able to send notifications such as Slack notification or your computer notification once a task is done. And this is powerful. Here is an example pre hook. This is for logging purpose. It automatically adds log to this path. So this is one example and I'm going to take you through step by step how you can use these tools to make sure you can stay cloud code to exactly what you want by defining different rules. That's exactly what we're going to see today. Let's get started. So there are different hooks such as pre-tool hook. So before running a tool, this will be run and post tool hook. After running the tool, this will be run. So it's like a AI agent having access to multiple tools but before calling the tool and after calling the tool we can modify the output. Also whenever we type something on the terminal we can filter that input before cloud code processes it and the notification will automatically add the notification such as slack notification and the stop runs when claude code finishes responding. Sub agent stop that runs when sub aent task complete. Pre- compact runs before cloud code is about to run a compact operation. Session start runs when cloud code starts a new session or receives an existing session. So these are the hooks which you can use and modify the text or the interaction or filter the output. So as a prerequisite you need jq installed. You can go to jq.lang and download for Mac OS, Windows or for Linux. You can also install using brew install jq for Mac. So that's exactly what I'm going to do. So coming to my terminal, I'm going to say brew install jq and then click enter. Now that's installing and it's done. Next I'm going to install cloud code and running that on my terminal and that is done. Then just type in clude and then click enter. Now I can ask it to do anything. Maybe write an application or change your existing application. I'll provide the link in the description for you to try the complete tutorial which I created before to get an idea. But in regards to this hooks, you can just type / hooks and click enter. Now here is the information. So hooks execute arbitrary shell commands. So which means you are providing full permission to modify files. This is fine when you try to use a dedicated computer for a specific purpose. And also you know that there are not many important files here. And here are the list of hooks. Pre-tool hook, post tool hook, notification, user prompt, summit, session start. So first let's start with pre-tool hook. Clicking on that and then you can add new matcher. So clicking on that. So tool matcher. So there are different tools listed here. So this could even include all the MCP tools which you're trying to add to clot code. It'll be listed here. So for now I'm going to use the bash tool. So whenever bash tool is used, I want it to run a specific command or a script. So I'm choosing bash and clicking enter. Now it's asking me if I need to add a new hook. Clicking on add new hook. And they've got few examples. So here is the command which I'm entering here. What this does is that it automatically takes the tool input command. So that is the bash tool input command the tool input description and then add that to this file bash command log dot. So that's it. I'm clicking enter and I'm going to save that locally. So you can see the different locations that this hooks can be stored. So it can be stored locally on the local current folder on the project level also it can be stored on a computerwide. So for now I'm going to save it locally. So clicking local and that's clicking escape to come back and now we are going to test it. Here you can see the thing what we added to test this I would generally recommend to exit and then restart clot because sometime I've seen we need to restart it for it to work. So once after you restart meanwhile I'm opening the file which is in this location bash command log.ext. So I've opened that file to see what's happening in this file. So coming back to cloud code terminal here I'm going to say print using bash tool say welcome to m present channel and click. Now it's running and now it says welcome to m present channel. So you can see there's a tool being used that is a bash tool. So let's open the file. So here when I open the file this is how it looks like. So this is the command which was run and this is my initial prompt. So my prompt got added with this echo command. So if I open my settings local.json that's where it automatically stores all the hooks. You can see here starting its tool input command. So that is the input command here from claude and this is what I typed in the terminal. So that is here tool input description that is what I typed. So both of this got saved in this claude bash command log.teext. So in that way you can log what's happening behind the scenes. By default it's very difficult to understand what's happening behind the scenes. All the list of communication happening between different tools. This makes it easy. You can also add filters and modify your response. Here you can also run any Python code. Just say Python and then file py and automatically it will run that python file. You can add as much as details as you want in that python file. Next, let's try other tools like hooks. So, post tool use. So, here I've added other can say bash add new hook. See these are the example commands which you can try running. So, I said python. So, you can even copy this and paste it here and run any python code. You can have your bash script in a particular location. You can even run that directly from here. So, that will be run after completing a tool call notification. You can add notification such as OSA script. This is for Mac OS. Similarly for Slack, you can run a Python script which can automatically trigger your Slack hooks. Next is user prompt summit. So if you want to check if particular words are used just for filtering purpose. For example, your company might have a rule saying that a particular words can't be used. So you can filter that and then pass those prompt to claude to make sure the company policy is being handled correctly. And finally session start you can run or add any script here. It could be a welcome message or a list of rules whenever someone starts a session. Here is an example of formatting. So whenever CL tries to edit, multi-edit or write it can format using this command that is npxier. For markdown formatting, you can also try like this markdown formatter py. That's where you got the code to format markdown. And here is the markdown formatted code. So I'll provide this link in the description for you to try and copy and test it for custom notification. As I mentioned before, this is for Linux notify sent for file protection hook. You can use this Python script which will prevent you from overwriting or package lock JSON file or even get. So that is a quick overview of the list of hook available here. Do try and let me know in the comments below what you think about this. Overall, I'm really impressed considering you are able to filter your input. You're able to monitor all the conversation behind the scenes. Do like, share, and subscribe if you like the video. And I created another video. I'll put the link in here. It's about clawed code other features which you want to try and I highly recommend for you to watch and I will see you