Cloud Code Hooks for Deterministic Control
AI Summaries (evaluation playlist)go watch the original →
the gist
Hooks in Cloud Code run commands every time at lifecycle events, such as post-tool-use for autoformatting or pre-tool-use to block dangerous operations; configure in settings.json and check into repo.
The Breakthrough
Hooks provide deterministic execution of commands at specific points in Cloud Code's lifecycle, ensuring they always run unlike nondeterministic prompt instructions such as telling Claude to run Prettier after edits.
What Actually Worked
- Developers configure hooks in settings.json by specifying an event, an optional matcher for tools, and a command to run.
- Events include "user prompt submit" (before Claude processes a prompt), "pre-tool use" (before a tool call), "post tool use" (after a tool completes), "notification" (when Claude sends a notification), and "stop" (when Claude finishes responding).
- A post tool use hook with matcher "edit" or "multi-edit" checks the file extension and runs the appropriate formatter, such as Prettier for TypeScript, go format for Go, or ruff for Python.
- A pre-tool use hook receives the tool name and input as JSON on stdin; it exits with code 0 to proceed or code 2 to block the action, feeding the stderr message back to Claude as feedback.
- Commands reference project scripts using the CLAUDE_PROJECT_DIR environment variable; hooks are project-level in .settings.json and can be checked into the repository for team-wide enforcement.
Context
Cloud Code relies on Claude for tasks like file edits, but prompt-based instructions such as "run Prettier after every edit" fail intermittently. Hooks solve this by guaranteeing execution for use cases like autoformatting, logging commands, blocking dangerous operations (such as writes to production configs or bash rm -rf), and sending task completion notifications. This enforces team rules without relying on Claude's interpretation.
Notable Quotes
- "Hooks are deterministic they always run so put it this way you can tell claude in your claw.md file to run prettier after every file edit and most of the time it will do that but sometimes it won't it's not perfect but a hook makes it happen every single time with no exceptions."
- "If something needs to happen every time without fail don't put it in a prompt put it in a hook."