Claude Code Hooks Summary
Key Concepts:
- Cloud Code Hooks: Mechanisms to intercept and modify Claude Code's behavior at various stages (pre-tool, post-tool, user prompt, etc.).
- Pre-tool Hook: Executes before a tool is run by Claude Code.
- Post-tool Hook: Executes after a tool is run by Claude Code.
- User Prompt Submit: Allows filtering of user input before Claude Code processes it.
- Notification Hook: Triggers notifications (e.g., Slack, desktop) upon task completion.
- Session Start Hook: Executes when a new Claude Code session begins.
- Tool Matcher: Specifies which tool a hook applies to (e.g., bash).
jq: A command-line JSON processor, a prerequisite for using Cloud Code Hooks.
1. Introduction to Cloud Code Hooks
- Cloud Code is powerful for application development and modification, but can make mistakes or violate organizational rules.
- Cloud Code Hooks address this by allowing customization and control over Claude Code's actions.
- Examples of use cases: automatic formatting, logging, automated feedback, blocking modifications to sensitive files, and sending notifications.
2. Types of Hooks and Their Functionality
- Pre-tool Hook: Runs before a tool is executed. Allows modification of the tool's input.
- Post-tool Hook: Runs after a tool is executed. Allows modification of the tool's output.
- User Prompt Submit: Filters user input before Claude Code processes it, enabling enforcement of company policies.
- Notification: Sends notifications upon task completion (e.g., Slack, desktop).
- Stop Runs: Triggers when Claude Code finishes responding or a sub-agent task completes.
- Pre-Compact: Runs before Claude Code performs a compact operation.
- Session Start: Runs when a new Claude Code session starts, allowing for welcome messages or rule displays.
3. Prerequisites and Installation
jqInstallation: Required for processing JSON data. Can be installed viabrew install jqon macOS or downloaded from jq.lang for other operating systems.- Cloud Code Installation: Install Cloud Code via the provided command in the terminal.
4. Using Pre-tool Hook: A Logging Example
- Demonstrates how to log bash commands executed by Claude Code.
- Steps:
- Type
/hooksin the Claude Code terminal to access the hooks menu. - Select "pre-tool hook."
- Add a new matcher and choose "bash" as the tool.
- Add a new hook with the following command:
echo "Tool Input Command: $(jq -r .tool_input_command) Tool Input Description: $(jq -r .tool_input_description)" >> bash_command_log.txt- This command appends the tool input command and description to the
bash_command_log.txtfile.
- This command appends the tool input command and description to the
- Save the hook locally.
- Restart Claude Code for the hook to take effect.
- Execute a bash command in Claude Code (e.g.,
print using bash tool say welcome to m present channel). - The command and description will be logged in
bash_command_log.txt.
- Type
- Explanation:
jq -r .tool_input_commandextracts the tool input command from the JSON payload.jq -r .tool_input_descriptionextracts the tool input description from the JSON payload.
- The hook configuration is stored in
settings-local.json.
5. Other Hook Examples and Use Cases
- Post-tool Hook: Can be used to run Python code or bash scripts after a tool completes.
- Notification Hook: Can trigger Slack notifications using Python scripts and Slack hooks.
- User Prompt Submit: Can filter user input to enforce company policies by checking for specific words.
- Session Start: Can display welcome messages or rules when a new session begins.
- Formatting Hook: Uses
npx prettierfor automatic code formatting. - File Protection Hook: Uses a Python script to prevent overwriting important files like
package-lock.jsonor.git.
6. Customization and Flexibility
- Hooks can execute arbitrary shell commands, providing full control over Claude Code's behavior.
- Python scripts can be used for complex logic and integrations.
- Hooks can be stored locally, at the project level, or computer-wide.
7. Notable Quotes
- (Implied) "Hooks execute arbitrary shell commands," highlighting the power and potential risk of using hooks.
8. Technical Terms and Concepts
- Tool Input Command: The specific command being executed by a tool.
- Tool Input Description: The user's description of what the tool should do.
- OSA Script: AppleScript, used for Mac OS notifications.
- NPX Prettier: A code formatter.
9. Logical Connections
- The video progresses from introducing the problem (Claude Code's potential for errors) to presenting the solution (Cloud Code Hooks).
- It then explains the different types of hooks and provides a detailed example of how to use the pre-tool hook for logging.
- Finally, it showcases other use cases and customization options.
10. Conclusion
Cloud Code Hooks provide a powerful mechanism to customize and control Claude Code's behavior. By using hooks, developers can enforce coding standards, log activity, filter user input, and integrate with other services. The flexibility of hooks allows for a wide range of use cases, making Claude Code a more reliable and customizable tool. The ability to filter input and monitor conversations behind the scenes is particularly impressive.
AI summaries can miss context or contain errors. Check important details against the original video.





