Claude Code Hooks: The Feature That Makes Agents Safe to Deploy

Your agent just deleted your codebase. Not because it was told to. Because it reasoned that the best code is no code at all — and started running rm -rf. If you had hooks configured, that call never executed. If you didn't, today is your day to learn why.

3:09 video3 min readWatch on YouTube

Claude Code Hooks: The Feature That Makes Agents Safe to Deploy

Your agent just deleted your codebase. Not because it was told to. Because it reasoned that the best code is no code at all — and started running rm -rf. If you had hooks configured, that call never executed. If you didn't, today is your day to learn why.

Topic: CLAUDE · CLI


Chapters

0:00 Introduction 2:14 Verdict 2:40 Your Turn


YOUR TURN

Your turn. Open your .claude/settings.json right now and add one hook. Start with pre-tool-use, empty matcher, a Python script that logs every tool name and input to a file. Run any Claude Code session. Read the log. You'll immediately see what your agent is doing that you didn't know it was doing. That's the first step from vibe coding to agentic


@NikBearBrown


Sources and citations: see the reel's SOURCES.md in the open textbook.

▶ The full series playlist — Brutalist https://www.youtube.com/playlist?list=PLG9H-C6rp5RU

What this video covers

  • 0:00 — Introduction
  • 2:14 — Verdict
  • 2:40 — Your Turn

Transcript

A lightly formatted transcript of the video, with timestamps so you can jump to any section on YouTube.

Introduction

[0:00] Your agent just deleted your codebase, not because it was told to, because it reasoned that the best code is no code at all and started running rm -rf. If you had hooks configured, that call never executed. If you didn't, today is your day to learn why. The problem with prompt-level safety instructions is that they're advisory. You tell Claude don't delete files and it generally won't until it decides deletion is the correct solution. Hooks are not advisory. They're enforced at the shell level before any tool executes. The five hooks available: pre-tool use, post-tool use, notification, stop, and sub-agent stop. Here's the configuration in Claude/settings.json. Add a hooks block. Each hook is an array of matchers and commands. The matcher is the tool pattern to intercept. An empty matcher means catch everything. The

Command is a shell script or Python file. Anthropic recommends single-file UV scripts because they carry their own dependencies. Nothing outside the Claude/hooks directory is required. The hook script reads JSON from stdin. Claude passes the full tool name and tool input as structured data. Check the tool name, inspect the arguments, and exit with a non-zero status to block execution. Here's the complete pre-tool use script that blocks rm commands and end file access. Zero external dependencies. Works in any codebase. What you get in the terminal when the hook fires? The rm command is submitted, the hook intercepts it, Claude receives a block response with the reason, and the tool call never executes. Meanwhile, the ls command goes through normally. Same hook, different tool, different decision. The hook logged both attempts to pre-tool use JSON

In the Claude/hooks/logs directory. The stop hook unlocks the second major use case, observability. Every time Claude finishes responding, the stop hook copies the full chat transcript to a log file. You now have a complete record of every tool call, every decision, every sub-agent spawn. If you're running overnight agentic sessions, this is how you audit what happened while you were away. The result is four distinct observability streams from a single Claude session. Pre-tool use log with every blocked and allowed call, post-tool-use log with execution results, stop log with the full transcript, and sub-agent stop log for each parallel subtask. That's the control surface you need to debug, improve, and iterate on any agentic system. If you don't measure it, you can't improve it, and now you can measure it. Let's close the loop.

Verdict

[2:14] Pre-tool-use's gate logic non-zero exit in the tool call never runs, full stop. Post-tool-use is your logging layer, every executed call with its result. Stop gives you the transcript. Together, these are four observability streams from a single session built on scripts that carry zero external dependencies. The difference between Claude and Claude with hooks, one is agentic capability. The other is agentic capability plus a safety net you wrote and control.

Your Turn

[2:40] Your turn. Open your Claude/settings.json right now and add one hook. Start with pre-tool-use empty matcher, a Python script that logs every tool name and input to a file. Run any Claude code session. Read the log. You'll immediately see what your agent is doing that you didn't know was doing. That's the first step from vibe coding to agentic engineering.

More from Claude Basics

Nik Bear Brown Poet and Songwriter