# What are Claude Code hooks, and how do you set one up?

A hook is an action Claude Code runs by itself at a set moment, every time. Inside: a first hook with no code, the main events, and what to do when one fails.

Tool: Claude. By Michel Lieben. Updated 2026-11-30. Source: https://coldiq.com/university/claude/claude-code-hooks

## In short

- A hook is an action that Claude Code runs at a precise moment, such as before an edit or when it finishes. Most hooks are commands. Claude does not decide, so it is never forgotten.
- Hooks live in a settings file: ~/.claude/settings.json for all your projects, or .claude/settings.json for one project.
- A hook that runs before an action can block it. A hook that runs after cannot undo it.
- You can ask Claude Code to write the hook for you, then check it with the /hooks command.
- Hooks run with your own rights on your computer, so read a hook before you add it, above all if it comes from someone else.

## Steps

1. Decide the moment and the action, such as "when you need me, notify me".
2. Ask Claude to write the hook, then read the command it proposes.
3. Type /hooks to check that it is there, and test it.

## What is a Claude Code hook?

A hook is a small rule: when this happens, run that. Claude Code watches for the moment (called an event) and runs your action: most often a command, or a prompt or an agent that asks a Claude model.

The difference with an instruction is certainty. If you write "always format the file" in your request or in CLAUDE.md, Claude does it nearly every time. If you write it as a hook, it happens every time, because Claude Code itself runs it.

Anthropic’s advice is simple: when you keep repeating a line and Claude still misses it, turn it into a hook. If it is only a preference, leave it as an instruction.


## What can you do with Claude Code hooks?

These examples are useful even if you do not write code.

- Get a notification when Claude waits for you.
- Stop any edit to a file that must never change, such as a price list or a file with keys.
- Format a file after each edit, so the style stays the same.
- Add a reminder at the start of each session, such as today’s priorities.
- Keep a log of the settings that change.
- Refuse to finish until a check passes.


## Which moments can a hook react to?

There are more than 30 events. These are the ones you will use.

- SessionStart: a session begins or resumes.
- UserPromptSubmit: you send a message, before Claude reads it.
- PreToolUse: before an action. This one can block it.
- PostToolUse: after an action that worked.
- Notification: Claude Code sends you a notification.
- Stop: Claude finishes its answer.
- SessionEnd: the session closes.

Tip: Stop fires each time Claude finishes an answer, not only when the whole job is done. A sound on Stop can get tiring fast. Notification is often the better choice.


## How do you create your first Claude Code hook?

Let Claude write it. You check it.

1. Open Claude Code and paste the request at the top of this page.
2. Read what it wants to add. A hook has an event, a matcher (a filter, such as "only edits") and a command.
3. Accept the change to your settings file.
4. Type /hooks and find it under its event.
5. Test it: ask Claude for something that needs your permission, then switch to another window.

Tip: On a Mac the notification goes through Script Editor. If you see nothing, open System Settings, then Notifications, and allow it for Script Editor.


## How do you use a hook to block something?

Use PreToolUse with a small script. The script looks at what Claude is about to do. If it ends with code 2, the action is blocked and Claude reads your reason. If it ends with code 0, nothing changes and the normal permission rules apply.

A block from a hook holds in every permission mode, even the one that skips all checks. That makes it a good safety net. It cannot do the reverse: a hook that says yes does not override a deny rule in your settings.

A hook on the Edit and Write tools does not cover everything. Claude can still change a file with a shell command, so add a deny rule and the sandbox for files that matter.

> Create a hook for this project that blocks any edit to files inside the "approved" folder, and tells you why. Then try to edit a file there so we can see it blocked. Tell me what this hook does not cover.


## Where do you save a hook, and can you share it?

The place decides who gets it.

- ~/.claude/settings.json: you, in all your projects.
- .claude/settings.json: this project, shared with the team.
- .claude/settings.local.json: you, in this project only.
- Managed settings: the whole company, set by an admin.
- A plugin, a skill or a subagent can carry its own hooks.


## What do you do when a hook does not work?

Check these in order.

- Type /hooks. If it is not listed, the file is in the wrong place or has a typo.
- Check the matcher. Upper and lower case count: Edit is not edit.
- Check the event: PreToolUse runs before, PostToolUse runs after.
- If you see "command not found", use the full path of the script.
- On Mac and Linux, make the script executable with chmod +x and its path.
- If you see "jq: command not found", install jq, a small tool many examples use.

Tip: Set "disableAllHooks": true in your settings to turn off your hooks at once while you look for the problem.


## What to say

> Add a hook to my user settings that shows a desktop notification each time you need my attention or my permission. Use the right command for my system. Show me the exact text you will add and explain it in plain words before you save.

## Sources

- [Get started with Claude Code hooks](https://code.claude.com/docs/en/hooks-guide)
- [Hooks reference](https://code.claude.com/docs/en/hooks)
- [Best practices for Claude Code](https://code.claude.com/docs/en/best-practices)

## Questions

**What is the difference between a hook and a skill?** A skill is a written method that Claude chooses to follow. A hook runs at a set moment, whether Claude wants it or not.

**Do hooks use my Claude limit?** A command hook runs on your computer and calls no model. Prompt and agent hooks do ask a Claude model.

**Can a hook undo an edit?** No. A PostToolUse hook runs after the action. To prevent an edit, use PreToolUse.

**How long can a hook run?** The default limit is 10 minutes for most command hooks, 30 seconds for prompt hooks and 60 seconds for agent hooks. You can change it with the timeout field.

**Are Claude Code hooks safe?** They run with your own rights, so a bad one can do harm. Read the command first, and ask Claude to explain it.


## Related

- [Claude Code security: is it safe, and what can it check?](https://coldiq.com/university/claude/claude-code-security)
- [What are Claude skills?](https://coldiq.com/university/claude/what-are-claude-skills)

## Try ColdIQ free

ColdIQ gives new accounts up to 1,450 free credits as they get started, no credit card required.
One subscription covers 40+ B2B data providers for finding, enriching and verifying
contacts and companies, through the web app, the API or the MCP server for AI agents.

1. Create an account: https://coldiq.com/marketplace/sign-up?utm_source=ai-assistant&utm_medium=markdown&utm_content=university
2. Run a first search on your own market with the free credits.
3. Connect the MCP server to Claude, ChatGPT or Cursor: https://coldiq.com/mcp
