Make AI actions auditable before you trust them to run unsupervised
After this, you'll be able to explain what hooks and gates are, write a simple pre-tool hook in Claude Code's settings, and use it to log or block specific agent actions.
Complete Connecting Claude to your real data with MCP first; this lesson builds on understanding how Claude calls external tools and what happens at those call boundaries.
A hook is a script that fires automatically before or after an AI tool call. A gate is a hook that can stop execution if a condition fails. Together, hooks and gates are how you make AI-driven workflows auditable and safe enough to run without watching every step.
In Claude Code, you configure hooks in your settings file: a pre-tool hook runs before the agent calls a tool such as writing or deleting a file. A post-tool hook runs after the tool completes.
Now try it: add a pre-tool hook to your Claude Code settings that logs the tool name and the target file path to /tmp/agent-log.txt every time the agent writes or edits a file. Run any small task, then open the log and confirm every file the agent touched appears there. Every gate condition you write after this depends on that log being accurate.
Paste this into Claude
I am setting up hooks in Claude Code for [describe your project or workflow, e.g., a script that automates file cleanup or an agent that edits code files]. I want to add [describe the specific hook behavior, e.g., a pre-tool hook that logs all file write and delete actions, or a gate that blocks writes outside a specific folder]. For each hook: 1) write the exact shell script or command to use, 2) show me where in the Claude Code settings file to add it and in what exact format, 3) explain which event triggers it, and 4) describe what I should see when it fires. After I add it, give me a specific test command I can run to confirm the hook executed correctly and produced the expected output.
Check yours against
Run a file-editing task, open the log file the hook was supposed to write, and confirm every file the agent touched appears in the log with a timestamp.
Pulling this out of your own head now is the part that actually moves it into long-term memory. Re-reading only feels like it does.
From memory, name one action Claude takes in your project that you want recorded, like running commands or changing files. Write the hook you would add first, and what it writes down.
One good answer: 'A pre-tool hook that logs every time Claude edits a file in my website folder, with the time and file name. I add a block only after the log shows what really happens.' If your first hook blocks something, switch it to logging first, because a gate needs real data.
Hooks fire before and after AI tool calls. Gates stop execution when a condition fails. Log first, then block; you need real data about what fires before you can write accurate conditions.