# Claude Code Hooks: MUST KNOW to Keep Claude Agent in Control!

Source: https://www.youtube.com/watch?v=CEODfvJLIGQ
Recap page: https://rapidrecap.app/video/CEODfvJLIGQ
Generated: 2025-08-14T18:33:09.91+00:00

---
## Quick Overview

Claude Code allows users to customize its behavior through "hooks" – shell commands that execute at specific points in Claude's workflow, enabling actions like automatic code formatting, custom notifications, and logging. The video demonstrates how to install Claude Code, configure hooks using the `/hooks` command, and provides examples for `PreToolUse`, `PostToolUse`, `Notification`, `UserPromptSubmit`, and `SessionStart` events, emphasizing the need for security best practices when implementing custom scripts.

**Key Points:**
- Claude Code hooks are user-defined shell commands that execute at specific points in Claude Code's workflow, enabling customization and automation.
- Hooks can be configured for various events, including `PreToolUse`, `PostToolUse`, `Notification`, `UserPromptSubmit`, and `SessionStart`.
- The tutorial demonstrates installing Claude Code and `jq`, and configuring a `PreToolUse` hook using the `/hooks` command to log `Bash` tool usage.
- An example shows automatic TypeScript code formatting using `prettier` via a `PostToolUse` hook.
- Custom notifications can be triggered using `osascript` for macOS when Claude Code needs input.
- Security best practices are emphasized, warning against arbitrary shell commands without proper validation and cautioning users about their responsibility.
- The video provides code examples for formatting markdown files and protecting sensitive files using Python scripts as hooks.

![Screenshot at 02:38: The terminal displays the "Welcome to Claude Code!" message and the current working directory after successful installation, indicating the user is ready to interact with the tool and its features.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-38.png)

**Context:** This video tutorial focuses on "Claude Code hooks," a feature of the Claude Code tool that allows users to extend and customize its functionality by registering shell commands to run at specific points in its execution. The tutorial aims to guide users through the process of setting up and utilizing these hooks for various automation and control purposes within their development workflow.

## Detailed Analysis

This video tutorial explains how to use "hooks" with Claude Code to customize its behavior and automate tasks. Hooks are user-defined shell commands that execute at various points in Claude Code's lifecycle, offering deterministic control over its actions. The tutorial covers the installation of Claude Code via npm and the prerequisite `jq` tool for JSON processing. It then walks through the process of configuring hooks by using the `/hooks` command in the Claude Code terminal. The video showcases different hook events, including `PreToolUse` (runs before tool calls, can block them), `PostToolUse` (runs after tool calls), `Notification` (when Claude sends notifications), `UserPromptSubmit` (when the user submits a prompt), and `SessionStart` (when a new session begins). Several practical examples are demonstrated: logging tool usage by matching the `Bash` tool and redirecting output to a file, implementing automatic code formatting for TypeScript files using `prettier`, and setting up custom notifications using `osascript` for macOS. The video also highlights security best practices, such as using absolute paths for custom scripts, avoiding `sudo`, and validating input paths to prevent malicious execution. Finally, it briefly touches upon file protection hooks that prevent edits to sensitive files like `.env` or `package-lock.json`. The tutorial concludes by encouraging viewers to experiment with different hooks and share their findings.

### Introduction to Claude Code Hooks

- Hooks are user-defined shell commands that execute at specific points in Claude Code's lifecycle, allowing customization of its behavior.

### Installation

- Install Claude Code using `npm install -g @anthropic-ai/claude-code`. Ensure `jq` is installed for JSON processing.

### Hook Configuration

- Use the `/hooks` command in the terminal to manage hook configurations. Hooks can be saved locally or globally.

### Hook Events Overview

- Key events include `PreToolUse`, `PostToolUse`, `Notification`, `UserPromptSubmit`, and `SessionStart`.

### Example Hooks Demonstrated

- Logging tool usage with `Bash` and `jq`, automatic TypeScript formatting with `prettier`, custom notifications with `osascript`, and file protection against editing sensitive files.

### Security Best Practices

- Use absolute paths, avoid `sudo`, validate inputs, and review documentation before implementing hooks.

![Screenshot at 00:01: The title slide "Get started with Claude Code hooks" introduces the topic of customizing Claude Code's behavior.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-00-01.png)
![Screenshot at 00:10: The "Claude Code" interface is shown, highlighting its integration with IDEs like VS Code and JetBrains.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-00-10.png)
![Screenshot at 00:25: A list of example use cases for hooks is presented, including notifications, automatic formatting, logging, feedback, and custom permissions.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-00-25.png)
![Screenshot at 01:14: The "Hook Events Overview" section lists various hook events such as PreToolUse, PostToolUse, UserPromptSubmit, Notification, and Stop.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-01-14.png)
![Screenshot at 01:51: The SessionStart hook is explained, which runs when Claude Code starts or resumes a session.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-01-51.png)
![Screenshot at 02:11: The installation process for jq is shown, detailing commands for different operating systems like Fedora, openSUSE, and Arch Linux.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-11.png)
![Screenshot at 02:26: The command brew install jq is executed in the terminal to install jq on macOS.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-26.png)
![Screenshot at 02:31: The output of npm install -g @anthropic-ai/claude-code shows successful installation of Claude Code.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-31.png)
![Screenshot at 02:38: The welcome message for Claude Code is displayed in the terminal after installation, along with available commands.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-38.png)
![Screenshot at 02:54: The "Hook Configuration" section provides critical security warnings and best practices for using hooks, emphasizing user responsibility and potential risks of arbitrary shell commands execution. It lists available hook events and their descriptions and asks the user to acknowledge risks before proceeding. The user selects "1. PreToolUse" to proceed with configuring a hook for this event type. The terminal then shows the "PreToolUse - Tool Matchers" menu, offering options to add new hooks or select existing ones. The user chooses to add a new hook and selects "Bash" as the tool matcher, indicating that the hook will apply to Bash commands. The video then displays the command input field for the hook, showing an example command using jq to log tool input and description to a file. The user confirms the hook configuration by pressing Enter. The terminal confirms that the hook has been added to the local settings. The user then exits the hook configuration menu and restarts Claude Code to apply the changes. The user prompts Claude Code to "fix typecheck errors". The log file bash-command-log.txt is opened to show the captured command output. Finally, the user demonstrates testing the hook by using the echo command to print a message, which is then logged to the bash-command-log.txt file, confirming the hook's functionality. The video then proceeds to showcase other hook examples, including PostToolUse for code formatting, Notification for sending desktop alerts, UserPromptSubmit for filtering user input, and SessionStart for custom session messages. The markdown formatting hook example shows how to automatically fix markdown issues using a Python script. The file protection hook example demonstrates preventing edits to sensitive files like .env or package-lock.json using a Python script. The video concludes by encouraging viewers to try these hooks and share their experiences, also suggesting other videos on Claude Code features and customization.](https://ss.rapidrecap.app/screens/CEODfvJLIGQ/00-02-54.png)
