Transcription
You think you're using an AI agent, but you're not. You're a babysitter. You tell Claude to refactor your codebase, and then you sit there approving every single file edit, running prettier by hand, watching it accidentally overwrite your end file with hallucinated credentials. And when it finishes, you weren't even at your desk because nobody told you it was done.
Five hooks, five lines of config each, and all of that goes away forever. Hooks are shell scripts that Claude code runs automatically at specific moments. Think of them like GitHub actions, but for your AI assistant.
There are 22 life cycle events you can hook into. Before Claude runs a command, that's pre-tool use. After it edits a file, post tool use. When it finishes a task, stop. When its context gets too full and compacts, post compact. You define them in your settings JSON, either globally for all projects or locally per repo. Every hook gets the event data piped in as JSON on ste in. And here's the key. If your hook exits with code 2, it blocks the action entirely. That's how we build guardrails.
Let me show you. Let me show you exactly how to set this up. Open your projects.claw directory and create or edit settings.json. Here's the full structure. At the top level, you have a hooks object. Inside hooks, you pick the life cycle event like pre-tool use or post tool use. Each event holds an array of hook groups. Every group has a matcher, a rigex that filters which tools trigger it, and then your hooks array. Each one with a type set to command and the actual shell command to run. That's it. This is the skeleton. Every hook follows. The matcher is your filter. The command is your action. And the event is your trigger.
Let's fill this in with five real hooks. Starting with the one you'll use every single day. Hook number one. The one you'll use every single day. Claude writes perfect logic, but it's formatting. Crime scene tabs mixed with spaces, inconsistent quotes, trailing white space everywhere. You could add please format with prettier to every prompt or you could never think about it again.
This post tool use hook listens for edit or write events. When Claude touches any file, jq extracts the file path from the event JSON and pipes it straight into prettier. Watch this. I'm going to ask Claude to build a React component. Look at the formatting when it generates. Messy. And now, boom. Instant. Prettier ran in the background before I could even blink. Every file, every time. Zero prompts needed.
I've seen it happen. A developer asks Claude to clean up the project. Claude sees the NV file, decides it looks messy, rewrites it. Your database URL gone. Your API keys replaced with placeholders. And if you were auto approving edits, which we'll get to, you wouldn't even know until production crashes.
Hook 2 is a pre-tool use guard. It intercepts any edit, write, or bash command before it executes. The script checks the file path against a list of protected patterns. Package-lock.git directory, anything you want. If it matches, exit code two blocked. Claude gets a message explaining why and it moves on without touching the file. Here, watch. I'll tell Claude to delete all unnecessary files in the project route. It scans, finds.yenv, tries to delete it, and blocked immediately. Claude even acknowledges the block and skips it. Your secrets stay safe.
Okay, so Claude formats your code and can't touch your secrets. But here's where it gets wild. You kick off a big refactor. 20 files. Claude's grinding through them. You go grab a coffee, come back 15 minutes later, and Claude's been sitting there waiting for your permission on step three. 15 minutes wasted.
Hook 3 uses the notification event to fire a native desktop alert the moment Claude needs you. On Mac, it's OSAScript. On Linux, notify send. On Windows, PowerShell's burnt toast. Now you can walk away knowing your phone or your screen will pull you back exactly when you're needed. Not a second before, not 15 minutes after.
Here's the sneaky problem. You start a session. You give Claude detailed instructions, your architecture doc, your coding standards, your project rules. It's perfect for the first hour. Then the context window fills up. Claude compacts. It summarizes and drops older messages to make room. and your architecture rules, your coding standards silently gone. Claude starts drifting, makes decisions that contradict what you told it earlier.
Hook 4 catches the post compact event. The moment Claude compacts its context, this hook automatically rereads your critical files back into the session. Your Claude MD, your architecture doc, your style guide, whatever you need Claude to never forget. It's like giving Claude a photographic memory for the things that matter most.
Hook five, god mode. By default, Claude asks permission before every bash command, every file write, every test run, and for dangerous stuff, you want that. But for npm run lint, for pi test, for git status, you're clicking approve 50 times a session for commands that could never hurt you. A pre-tool use hook on bash can check the command against a white list. If it matches, npm test, npx prettier, git diff, anything you trust. The hook outputs a JSON object with the decision set to allow. Claude skips the permission prompt entirely.
Combine this with hook 2, the bodyguard, and you get the best of both worlds. Claude runs at full speed on safe commands, but the moment it tries to touchenv or run rm-rf, blocked instantly.
This is what a real AI agent looks like. Five hooks, five minutes of setup, and Claude goes from a chatbot you babysit to an autonomous agent you trust. Every config file I showed you is linked in the description. Copy, paste, customize.
But here's the thing. I didn't even show you the newest hook type, agent hooks. They spawn a second Claude instance to verify the first one's work before it ships. If you want to see me build a full autonomous pipeline where Claude writes code, tests it, reviews it with an agent hook, and deploys it all without a single human click, hit subscribe. That video is coming next.