Skip to main content

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

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.

Think of Hooks as Automatic Triggers

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:

FeatureSkillsHooks
What they areMarkdown instructions for ClaudeShell commands that run automatically
Who executes themClaude (the AI) interprets and follows themYour operating system runs them directly
Deterministic?No — Claude decides how to follow instructionsYes — the exact same command runs every time
When they runWhen Claude decides the skill is relevantAt 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:

  1. You define a hook in your settings: "When this event happens, run this command"
  2. Claude Code monitors for that event during your session
  3. When the event occurs, Claude Code pauses and runs your command
  4. The command's output can influence what happens next (for example, blocking an action)
  5. Claude Code resumes
Event occurs  -->  Hook fires  -->  Command runs  -->  Claude continues
note

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:

  1. Open your terminal and navigate to any project
  2. Check if a project-level settings file exists:
dir .claude\settings.json
  1. If it does not exist, that is fine — you will create it later
  2. Also check the global settings:
dir %USERPROFILE%\.claude\settings.json
For WSL Users

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

Help Us Improve

How was this lesson? Take 2 minutes to share your feedback — it helps us make the tutorials better for everyone.

Give Feedback →

Next Up

Next: Lifecycle Events