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.
Table of Contents
1. What are Hooks? How is it different from Skills?
-
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
| 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
-
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
-
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
-
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
-
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
-
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 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 Tools Guide: ChatGPT / Claude / Gemini comparison + 10 practical applications
This article includes comparison of the most powerful AI tools in 2026, 5 major categories of applications for writing/design/programming/office/study, getting started with Prompt engineering, free vs paid judgment, and a 7-day entry plan
2026 AI briefing tool complete comparison: actual test evaluation of 8 major tools such as Gamma, Tome, and Beautiful AI
Actual test of AI presentation tools: 8 tools including Gamma, Tome, Beautiful AI, Canva AI, Presentations AI, complete comparison based on needs, budget, and quality, and make a professional presentation in 10 minutes.
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.