Week 7 of 12 🊝

Hooks & Automation

Hooks āđāļĨāļ°āļĢāļ°āļšāļšāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļī

Hooks & Automation

1. Introduction / āļšāļ—āļ™āļģ

Hooks āļ„āļ·āļ­āļŠāļ„āļĢāļīāļ›āļ•āđŒāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļīāļ—āļĩāđˆāļ—āļģāļ‡āļēāļ™āļ•āļēāļĄāđ€āļŦāļ•āļļāļāļēāļĢāļ“āđŒāđ€āļ‰āļžāļēāļ°āđƒāļ™ Claude Code āļŠāļēāļĄāļēāļĢāļ–āļˆāļąāļ”āļĢāļđāļ›āđāļšāļšāđ‚āļ„āđ‰āļ”āļŦāļĨāļąāļ‡āđāļāđ‰āđ„āļ‚ āļ›āđ‰āļ­āļ‡āļāļąāļ™āļ„āļģāļŠāļąāđˆāļ‡āļ­āļąāļ™āļ•āļĢāļēāļĒ āļŠāđˆāļ‡āļāļēāļĢāđāļˆāđ‰āļ‡āđ€āļ•āļ·āļ­āļ™ āđāļĨāļ°āļšāļąāļ‡āļ„āļąāļšāđƒāļŠāđ‰āļāļŽāđ‚āļ›āļĢāđ€āļˆāļāļ•āđŒ — āļ—āļąāđ‰āļ‡āļŦāļĄāļ”āđ‚āļ”āļĒāđ„āļĄāđˆāļ•āđ‰āļ­āļ‡āļ—āļģāļ”āđ‰āļ§āļĒāļ•āļ™āđ€āļ­āļ‡
Hooks are automated scripts that run at specific lifecycle events in Claude Code. They can format code after edits, block dangerous commands, send notifications, and enforce project rules — all without manual intervention.

Fun Facts / āļ‚āđ‰āļ­āđ€āļ—āđ‡āļˆāļˆāļĢāļīāļ‡āļ—āļĩāđˆāļ™āđˆāļēāļŠāļ™āđƒāļˆ

  • Exit code 2 blocks the action and tells Claude why.
  • Hooks can be shell commands, HTTP calls, or AI prompts!
  • PostToolUse hooks are great for auto-formatting code.

2. Vocabulary / āļ„āļģāļĻāļąāļžāļ—āđŒ

Click a card to flip it. āļ„āļĨāļīāļāļāļēāļĢāđŒāļ”āđ€āļžāļ·āđˆāļ­āļžāļĨāļīāļ

hook
āļŪāļļāļ
A script that runs at a specific event
event
āđ€āļŦāļ•āļļāļāļēāļĢāļ“āđŒ
Something that happens in the system
automate
āļ—āļģāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļī
To make something run by itself
block
āļšāļĨāđ‡āļ­āļ / āļ›āđ‰āļ­āļ‡āļāļąāļ™
To prevent an action from happening
format
āļˆāļąāļ”āļĢāļđāļ›āđāļšāļš
To arrange code in a standard style
validate
āļ•āļĢāļ§āļˆāļŠāļ­āļš
To check if something is correct
notify
āđāļˆāđ‰āļ‡āđ€āļ•āļ·āļ­āļ™
To send an alert or message
matcher
āļ•āļąāļ§āļˆāļąāļšāļ„āļđāđˆ
A pattern to match specific tool names
exit code
āļĢāļŦāļąāļŠāļ­āļ­āļ
A number showing if a command succeeded
stdin
āļŠāđāļ•āļ™āļ”āļēāļĢāđŒāļ”āļ­āļīāļ™āļžāļļāļ•
Standard input — data fed to a program
stdout
āļŠāđāļ•āļ™āļ”āļēāļĢāđŒāļ”āđ€āļ­āļēāļ•āđŒāļžāļļāļ•
Standard output — data from a program
lifecycle
āļ§āļ‡āļˆāļĢāļŠāļĩāļ§āļīāļ•
The stages from start to end

3. Grammar Focus / āđ„āļ§āļĒāļēāļāļĢāļ“āđŒ

Conditional Sentences (If... then..., When... then...) / āļ›āļĢāļ°āđ‚āļĒāļ„āđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚ (If... then..., When... then...)

Thai:
āļ›āļĢāļ°āđ‚āļĒāļ„āđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚āļ­āļ˜āļīāļšāļēāļĒāļŠāļīāđˆāļ‡āļ—āļĩāđˆāđ€āļāļīāļ”āļ‚āļķāđ‰āļ™āļ āļēāļĒāđƒāļ•āđ‰āđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚ 'If + present, present/future' āļŠāļģāļŦāļĢāļąāļšāđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚āļˆāļĢāļīāļ‡ 'When + present, present' āļŠāļģāļŦāļĢāļąāļšāļŠāļīāđˆāļ‡āļ—āļĩāđˆāđ€āļāļīāļ”āļ‚āļķāđ‰āļ™āđ€āļŠāļĄāļ­ āļ•āļąāļ§āļ­āļĒāđˆāļēāļ‡: 'If you edit a file, the hook runs.' 'When Claude finishes, it sends a notification.'
English:
Conditional sentences describe what happens under certain conditions. 'If + present, present/future' for real conditions. 'When + present, present' for things that always happen. Example: 'If you edit a file, the hook runs.' 'When Claude finishes, it sends a notification.'
āļ–āđ‰āļē exit code āđ€āļ›āđ‡āļ™ 2 Claude āļˆāļ°āļšāļĨāđ‡āļ­āļāļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ
If the exit code is 2, Claude blocks the action.
āđ€āļĄāļ·āđˆāļ­āļ„āļļāļ“āļšāļąāļ™āļ—āļķāļāđ„āļŸāļĨāđŒ āļ•āļąāļ§āļˆāļąāļ”āļĢāļđāļ›āđāļšāļšāļˆāļ°āļ—āļģāļ‡āļēāļ™
When you save a file, the formatter runs.
āļ–āđ‰āļēāđ‚āļ—āđ€āļ„āđ‡āļ™āđ„āļĄāđˆāļ–āļđāļāļ•āđ‰āļ­āļ‡ āļāļēāļĢāļĒāļ·āļ™āļĒāļąāļ™āļ•āļąāļ§āļ•āļ™āļˆāļ°āļĨāđ‰āļĄāđ€āļŦāļĨāļ§
If the token is invalid, authentication fails.

Section Check 1

What exit code blocks an action?

Exit code āļ­āļ°āđ„āļĢāļšāļĨāđ‡āļ­āļāļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ?

Section Check 2

Where are hooks configured?

Hooks āļ•āļąāđ‰āļ‡āļ„āđˆāļēāļ—āļĩāđˆāđ„āļŦāļ™?

Section Check 3

Complete: ___ you save, the hook runs.

āđ€āļ•āļīāļĄ: ___ you save, the hook runs.

4. Tutorial Steps / āļ‚āļąāđ‰āļ™āļ•āļ­āļ™āļāļēāļĢāđ€āļĢāļĩāļĒāļ™āļĢāļđāđ‰

1
View existing hooks / āļ”āļđ hooks āļ—āļĩāđˆāļĄāļĩāļ­āļĒāļđāđˆ
Type /hooks to see all configured hooks and their events.
2
Add a hook in settings / āđ€āļžāļīāđˆāļĄ hook āđƒāļ™āļāļēāļĢāļ•āļąāđ‰āļ‡āļ„āđˆāļē
Open ~/.claude/settings.json and add a hook under the 'hooks' key.
3
Create a PreToolUse hook / āļŠāļĢāđ‰āļēāļ‡ PreToolUse hook
Block dangerous commands by checking stdin before Bash runs.
4
Create a PostToolUse hook / āļŠāļĢāđ‰āļēāļ‡ PostToolUse hook
Auto-format files after Edit or Write operations.
5
Test your hooks / āļ—āļ”āļŠāļ­āļš hooks
Trigger the event and check if your hook runs correctly.

5. Hands-On Activity / āļāļīāļˆāļāļĢāļĢāļĄāļĨāļ‡āļĄāļ·āļ­āļ—āļģ

āđāļšāļšāļāļķāļāļŦāļąāļ”: āļŠāļĢāđ‰āļēāļ‡ PostToolUse hook āļ—āļĩāđˆāļšāļąāļ™āļ—āļķāļāļ—āļļāļāļāļēāļĢāđāļāđ‰āđ„āļ‚āđ„āļŸāļĨāđŒāļĨāļ‡āđƒāļ™āđ„āļŸāļĨāđŒ changes.log āļ—āļ”āļŠāļ­āļšāđ‚āļ”āļĒāļ‚āļ­āđƒāļŦāđ‰ Claude āđāļāđ‰āđ„āļ‚āđ„āļŸāļĨāđŒ
Practice: Create a PostToolUse hook that logs every file edit to a changes.log file. Test it by asking Claude to edit a file.

6. Reference Table / āļ•āļēāļĢāļēāļ‡āļ­āđ‰āļēāļ‡āļ­āļīāļ‡

Command / ToolEnglishāļ āļēāļĐāļēāđ„āļ—āļĒ
PreToolUseBefore a tool runsāļāđˆāļ­āļ™āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ—āļģāļ‡āļēāļ™
PostToolUseAfter a tool succeedsāļŦāļĨāļąāļ‡āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­āļ—āļģāļ‡āļēāļ™āļŠāļģāđ€āļĢāđ‡āļˆ
SessionStartWhen a session beginsāđ€āļĄāļ·āđˆāļ­āđ€āļ‹āļŠāļŠāļąāļ™āđ€āļĢāļīāđˆāļĄāļ•āđ‰āļ™
NotificationClaude needs attentionClaude āļ•āđ‰āļ­āļ‡āļāļēāļĢāļ„āļ§āļēāļĄāļŠāļ™āđƒāļˆ
StopClaude finishes respondingClaude āļ•āļ­āļšāđ€āļŠāļĢāđ‡āļˆ

7. Summary / āļŠāļĢāļļāļ›

Hooks āļ—āļģāđƒāļŦāđ‰āļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļīāļ•āļēāļĄāđ€āļŦāļ•āļļāļāļēāļĢāļ“āđŒ PreToolUse āļšāļĨāđ‡āļ­āļāļāļēāļĢāļ”āļģāđ€āļ™āļīāļ™āļāļēāļĢ PostToolUse āļ—āļģāļ‡āļēāļ™āļŦāļĨāļąāļ‡āđ€āļ„āļĢāļ·āđˆāļ­āļ‡āļĄāļ·āļ­ āļāļģāļŦāļ™āļ”āļ„āđˆāļēāđƒāļ™ settings.json āļ›āļĢāļ°āđ‚āļĒāļ„āđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚ (if/when) āļ­āļ˜āļīāļšāļēāļĒāļ•āļĢāļĢāļāļ° hook
Hooks automate actions at lifecycle events. PreToolUse blocks actions, PostToolUse runs after tools. Configure them in settings.json. Conditional sentences (if/when) describe hook logic.

Homework / āļāļēāļĢāļšāđ‰āļēāļ™

1. āļŠāļĢāđ‰āļēāļ‡ notification hook āđ€āļĄāļ·āđˆāļ­ Claude āļ—āļģāđ€āļŠāļĢāđ‡āļˆ
2. āđ€āļ‚āļĩāļĒāļ™ PreToolUse hook āļ—āļĩāđˆāļšāļĨāđ‡āļ­āļ 'rm -rf'
3. āđ€āļ‚āļĩāļĒāļ™ 5 āļ›āļĢāļ°āđ‚āļĒāļ„āđ€āļ‡āļ·āđˆāļ­āļ™āđ„āļ‚āđ€āļāļĩāđˆāļĒāļ§āļāļąāļšāļĢāļ°āļšāļšāļ­āļąāļ•āđ‚āļ™āļĄāļąāļ•āļī
1. Create a notification hook for when Claude finishes.
2. Write a PreToolUse hook that blocks 'rm -rf'.
3. Write 5 conditional sentences about automation.