Claude Code Hooks and Automation Guide: A complete tutorial on event-driven AI workflows
Claude Code Hooks complete tutorial, learn to use event-driven mechanism to automatically trigger Shell commands, and use Skills to build a powerful automated workflow, with 8 practical Hook examples.
Last Updated:2026-05-26
Table of Contents
1. What are Hooks? How is it different from Skills?
Skills are commands that you actively trigger (/do-something), and Hooks are passively triggered automations—when a certain event occurs, the system automatically executes your preset commands. The two are used together, just like "Skills are the engine and Hooks are the automatic driving."
-
Skills: User Active Trigger (Input/Command Or Natural Language) → Claude Executes The Preset Process
-
Hooks: Events Are Automatically Triggered (Tools Are Called, Messages Are Sent, Instructions Are Completed, Etc.) → The System Automatically Executes Shell Instructions
-
Combined Power: /Deploy Skill Executes Deployment → Deployment Is Completed → Hook Automatically Runs Smoke Test → Smoke Test Passes → Hook Sends Slack Notification
2. Hook event type
Claude Code supports multiple event types, and each event can be bound to different Hooks.
| event | Trigger time | Common uses |
|---|---|---|
| PreToolUse | Before tool call | Intercept dangerous operations and confirm file changes |
| PostToolUse | After tool call | Automatic formatting and Log recording |
| UserPromptSubmit | When a user sends a message | Message preprocessing and automatic bringing into context |
| Stop | When Claude finishes his response | Automatic notifications, output summaries |
| SessionStart | at the beginning of the conversation | Environment initialization, loading context |
3. Setting method: settings.json
Hooks are set in settings.json of Claude Code. This can be set using the /update-config command, or edited manually.
-
Configuration File Location: ~/.Claude/Settings.Json (Global) Or .Claude/Settings.Json (Project Level)
-
Basic Structure: Hooks Objects Are Grouped By Event Type, Each Event Can Be Bound To Multiple Hooks
-
Each Hook Contains: Type (Command), Command (Shell Instruction To Be Executed), Description (Description)
-
Optional Fields: Matcher (Only Triggered For Specific Tool Names), Timeout (Timeout Setting)
-
Priority: Project-Level Settings Will Override Global Settings
Tip
- It is recommended to use the /update-config directive, which will automatically handle JSON format and path issues.
- There is no need to restart Claude Code after modifying settings.json, the settings will take effect immediately
4. Example 1-3: Development process automation
The most commonly used development process is Hook.
-
Example 1 - Automatically Lint After File Modification: Posttooluse + Matcher: Write/Edit → Execute Eslint --Fix Or Black To Automatically Format The Newly Modified File
-
Example 2 - Load Project Status At The Beginning Of The Session: Sessionstart → Execute Git Status And Docker Ps To Bring The Results Into The Session Context
-
Example 3 - Interception Before Dangerous Operations: Pretooluse + Matcher: Bash → Check Whether The Command Contains Dangerous Operations Such As Rm -Rf, Git Push --Force, Etc., And Prevent Execution If Any
5. Example 4-6: Quality Assurance Automation
Automatically integrate quality inspection into your workflow.
-
Example 4 - Automatically Run Tests Before Submission: Pretooluse + Matcher: Bash (When The Command Includes Git Commit) → Run Pytest First, And Then Submit After Passing The Test
-
Example 5 - Translation Safety Check: Posttooluse + Matcher: Write (When Modifying .Po Or Locale Files) → Automatically Execute Python3 Scripts/Check_Translations.Py --Js-Safety
-
Example 6 - Automatic Verification After Json Modification: Posttooluse + Matcher: Write (When Modifying The File Under Data/Guides/) → Automatically Execute ./Scripts/Validate_Json.Sh
6. Example 7-8: Notifications and Records
Let your team receive progress updates automatically.
-
Example 7 — Automatic Notification On Completion: Stop → Send A Summary Of Claude’S Final Response To Slack Or Telegram To Let Team Members Know The Task Is Completed
-
Example 8 - Operation Record: Posttooluse → Record The Type And Parameters Of Each Tool Call To The Log File To Facilitate Subsequent Tracking Of "What Claude Did"
7. Hooks + Skills combination mode
Hooks and Skills can be used together to create powerful automated workflows. Here are three common combination patterns.
-
Mode 1 - Pre-Check: Hook (Pretooluse) Confirms That The Environment Is Normal → Skill Performs The Main Work. For Example, Hook Checks Whether Ci Passes Before Deployment.
-
Mode 2 — Post-Processing Chain: Skill Does The Main Work → Hook (Stop) Performs Post-Processing Automatically. For Example, After /Content-Pipeline Creates Content, Hook Automatically Commits And Notifies
-
Mode 3 - Quality Access Control: Skill Modifies The Program Code → Hook (Posttooluse) Automatically Runs Lint And Tests → Continue Only If All Pass. Ensure That The Code Produced By Ai Meets Quality Standards
Tip
- Don't do too much heavy lifting in Hooks. Hooks should be completed quickly (within seconds), and time-consuming work is handled by Skills
- Hook failure will block Claude's operation. Ensure that Hook instructions are reliable and avoid accidental blocking
8. Debugging and Best Practices
Hook setting troubleshooting methods and design suggestions when problems occur.
-
Hook Is Not Triggered: Confirm Whether The Event Type And Matcher Are Correct. Use The Echo Command To Test Whether The Hook Is Called
-
Hook Blocks Operation: Check The Hook'S Exit Code. A Non-Zero Exit Code Blocks The Operation
-
Hook Is Too Slow: Add Timeout Settings To Prevent Hook From Blocking The Entire Workflow. Default Timeout 60 Seconds
-
Best Practice 1: Keep Hook Instructions Simple—One Hook Does One Thing
-
Best Practice 2: Use Description To Clearly Explain The Purpose Of Hook To Facilitate Team Members’ Understanding.
-
Best Practice 3: Test In Personal Settings First, And Then Push To Project Settings After Confirming It Is Correct.
-
Best Practice 4: Be Especially Cautious When Using Hooks For Sensitive Operations (Such As Automatic Push And Automatic Deployment). It Is Recommended To Add A Confirmation Step.
Key Takeaways
- 1 Hooks are the event-driven mechanism of Claude Code - automatically execute your preset Shell instructions when a specific event occurs
- 2 Used with Skills, you can establish a complete automation chain of "trigger Skill → execution completion → Hook automatic post-processing"
- 3 Common applications: automatic lint before submission, confirmation before tool call, automatic notification after completion
- 4 Set in settings.json, no need to modify the program code, pure setting driver
Related Links
Related Quick Guides
2026 AI Presentation Tools Guide: Create Pro Slides in 10 Minutes
Compare the best AI presentation tools in 2026: Gamma, Beautiful.ai, Canva AI, GenPPT, Plus AI. Includes free plans, pricing, reviews, and recommendations.
2026 AI 工具實用指南:提升工作與生活效率的 10 大應用
從 ChatGPT 到 Claude,全面解析 2026 年最實用的 AI 工具,幫你省時間、提效率、做更好的決策
AI Image Generation Tools Guide 2026: Midjourney vs DALL-E vs SD
Complete 2026 guide to AI image generation tools. Compare Midjourney, DALL-E 3, and Stable Diffusion features, pricing, and prompt writing techniques.
General Disclaimer
The information provided on this site is for reference only. We do not guarantee its completeness or accuracy. Users should determine the applicability of the information on their own.