More

Free lesson preview

Lesson 1

How Claude Code Hooks Intercept Agent Actions

Prompt instructions are advisory. Hooks are enforced.

When you write "never edit .env" in your instructions or CLAUDE.md, you're asking the agent to remember a rule. Most of the time it will. But the agent can misread the rule, forget it in a long session, or decide that this one case is an exception. Nothing stops it.

A hook is different. It's a script that Claude Code itself runs, outside the model, every single time a matching action is about to happen. The agent doesn't get a vote. If your script says no, the action doesn't happen.

Where the hook sits

A PreToolUse hook runs in the gap between the agent deciding to do something and Claude Code actually doing it:

  1. The agent decides to use a tool, for example Edit to change a file or Bash to run a command.

  2. Before running it, Claude Code pauses and starts your hook script.

  3. Claude Code sends your script a description of the action it's about to run.

  4. Your script inspects it and exits with a code that means "allow" or "block".

  5. Claude Code either runs the action or cancels it, based on that code.

Because this happens before the action, a blocked action never touches your files or runs your command.

What the hook receives

Claude Code sends the action to your script as JSON through stdin. Here is a simplified example for a file edit:

json

json
{  "hook_event_name": "PreToolUse",  "cwd": "C:\\Projects\\my-app",  "tool_name": "Edit",  "tool_input": {    "file_path": "C:\\Projects\\my-app\\.env",    "old_string": "API_KEY=old",    "new_string": "API_KEY=new"  }}
  • hook_event_name: which kind of hook is running. Here it's PreToolUse.

  • cwd: the folder the agent is working in.

  • tool_name: the tool the agent wants to use.

  • tool_input: the details of this specific action. Its fields depend on the tool.

The payload also includes a few more fields, such as the session ID. You can ignore them for now.

That last point matters. Here is the same idea for a shell command:

json

json
{  "hook_event_name": "PreToolUse",  "cwd": "C:\\Projects\\my-app",  "tool_name": "Bash",  "tool_input": {    "command": "del .env"  }}

A Bash action has no file_path at all, only a command. A hook that only checks file_path will never see a file being changed through a shell command. Keep this in mind for Lesson 2.

stdin, stdout, stderr and exit codes

If you haven't worked with these before, here's all you need:

  • stdin is where your script reads input. Claude Code writes the JSON there.

  • stdout is your script's normal output.

  • stderr is your script's error output. This is where you explain a block.

  • The exit code is a number your script ends with. It's how your script gives Claude Code its answer.

How your hook allows or blocks the action

Claude Code reads your script's exit code:

  • Exit 0: allow. The action runs normally.

  • Exit 2: block. The action is cancelled, and whatever your script printed to stderr is sent back to Claude, so the agent understands why it was blocked and can change its approach.

  • Any other exit code (for example 1, or your script crashing): this is treated as an error in the hook, not a block. The action still runs.

That third case catches a lot of people. If your script has a bug and crashes, it doesn't fail safe. It fails open, and the action goes through. Lesson 3 covers how to test for this.

Write block messages for the agent

Your stderr message is the only thing the agent learns about the block, so make it useful. Say what was blocked, why, and what to do instead.

Weak:

text

text
Blocked.

Useful:

text

text
Blocked: .env holds secrets and is protected. Do not edit it. If a setting needs to change, tell the user which value and why, and let them change it.

The first one leaves the agent guessing, and it may try again a different way. The second tells it exactly how to move on.

Recap

Every hook in this course follows one loop:

  1. Read the JSON from stdin.

  2. Decide.

  3. Exit 0 to allow, or print a clear reason to stderr and exit 2 to block.

In Lesson 2 you'll build your first one: a PowerShell script that protects sensitive files, and the settings that tell Claude Code to run it.

Check Your Understanding: How Hooks Intercept Actions

Question 1

Your hook wants to stop an action. Which exit code does it use?

Question 2

Your hook blocks an action. Where does the agent learn why?

Question 3

The agent runs del .env through the Bash tool. What does your hook find in tool_input?

Question 4

Your hook script has a bug and crashes with exit code 1. What happens to the action?


Free

4 lessons