What Are Hooks?
Module: Hooks | Lesson: 1 of 4 | Time: ~10 minutes
What You Will Learn
- What hooks are in Claude Code
- The lifecycle concept — when hooks fire
- Why hooks are useful for automation
Prerequisites
- Completed Module 08: MCP Servers
What Is a Hook?
A hook in Claude Code is a shell command that runs automatically at a specific moment during Claude's operation. When a particular event happens — like Claude starting a session, using a tool, or finishing a task — your hook fires and runs whatever command you have configured.
Imagine a security camera that automatically starts recording when it detects motion. You do not press a button every time — the camera is triggered by the event. Hooks work the same way. You define the event (motion) and the action (start recording), and it happens automatically every time.
Hooks vs Skills
You already learned about skills in Module 07. Skills and hooks are different things that serve different purposes:
| Feature | Skills | Hooks |
|---|---|---|
| What they are | Markdown instructions for Claude | Shell commands that run automatically |
| Who executes them | Claude (the AI) interprets and follows them | Your operating system runs them directly |
| Deterministic? | No — Claude decides how to follow instructions | Yes — the exact same command runs every time |
| When they run | When Claude decides the skill is relevant | At specific lifecycle moments, every time |
The key distinction is that hooks are deterministic — they are not AI-driven. When the event fires, the command runs. There is no AI decision-making involved. This makes hooks reliable and predictable.
Why Hooks Are Useful
Hooks let you automate tasks that should happen consistently, without relying on Claude to remember to do them. Common use cases include:
- Auto-formatting code after Claude edits a file
- Running linters after code changes
- Sending notifications when Claude finishes a long task
- Blocking changes to protected files (like production configs)
- Logging everything Claude does for review
- Reloading environment variables when a session starts
How Hooks Work
Here is the basic flow:
- You define a hook in your settings: "When this event happens, run this command"
- Claude Code monitors for that event during your session
- When the event occurs, Claude Code pauses and runs your command
- The command's output can influence what happens next (for example, blocking an action)
- Claude Code resumes
Event occurs --> Hook fires --> Command runs --> Claude continues
Hooks run as shell commands on your machine. They have the same permissions as your user account. This means they can do anything a terminal command can do — run scripts, call APIs, write files, and more.
A Simple Example
Here is a preview of what a hook configuration looks like (we will cover the full syntax in Lesson 3):
{
"hooks": {
"PostToolUse": [
{
"matcher": "Edit",
"command": "echo 'A file was just edited!'"
}
]
}
}
This hook says: "After Claude uses the Edit tool, run the command echo 'A file was just edited!'." Every time Claude edits a file, that message prints automatically.
Try It Yourself
Hooks require configuration in a settings file, which we will cover in detail in Lesson 3. For now, let us just verify you have access to the settings file where hooks will live:
- Open your terminal and navigate to any project
- Check if a project-level settings file exists:
dir .claude\settings.json
- If it does not exist, that is fine — you will create it later
- Also check the global settings:
dir %USERPROFILE%\.claude\settings.json
Use Linux paths instead:
ls .claude/settings.json
ls ~/.claude/settings.json
These are the files where you will add hook configurations in the upcoming lessons.
What You Learned
- Hooks are shell commands that run automatically at specific moments in Claude's lifecycle
- Unlike skills, hooks are deterministic — the same command runs every time, with no AI decision-making
- Hooks are useful for auto-formatting, notifications, blocking protected files, logging, and more
- Hooks are defined in your settings.json file (project or global)
- When an event fires, the hook command runs and can influence Claude's behavior
How was this lesson? Take 2 minutes to share your feedback — it helps us make the tutorials better for everyone.