Nine Niche Tool Station
Back to List

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.

Claude Code Hooks automation event driven Workflow settings.json CI/CD

Last Updated:2026-05-26

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
flowchart LR subgraph Skills[Skills active trigger] A[User input/command] --> B[Claude execution flow] end subgraph Hooks[Hooks passive trigger] C[Event Occurrence] --> D[Automatically execute Shell command] end B -->|Complete event|C

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
flowchart TD subgraph mode 1 [mode 1: pre-check] A1[Hook: PreToolUse] --> A2{The environment is normal?} A2 -->|Yes| A3[Skill execution] A2 -->|No| A4[Block and remind] end subgraph mode 2[mode 2: post-processing chain] B1[Skill completed] --> B2[Hook: Stop] B2 --> B3[automatic commit] B2 --> B4[Send notification] end subgraph mode 3[Mode 3: Quality Access Control] C1[Skill Modify Code] --> C2[Hook: PostToolUse] C2 --> C3[automatic lint + test] C3 --> C4{Passed?} C4 -->|Yes| C5[Continue] C4 -->|No| C6[Block] end

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
ℹ️

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.

Feedback