Not long ago, while rebuilding the Zygnus site with Claude Code, we ran a local preview connected through a tunnel to the production database. It was the quick way to see real content. It worked, and it also taught us something: every time we opened a blog post to review it, the view counter in production went up. Nothing serious, but it was a real write to the live database, made from a laptop.
With an agent that runs commands, that kind of accident can be much worse. Claude Code follows what you put in CLAUDE.md, but those instructions are guidance for the model. If a step must never be skipped, something that does not depend on the model should enforce it. That is what hooks are for.
What a hook is
A hook is a command of yours that Claude Code runs at fixed moments. The one we use to protect production is PreToolUse: it runs before Claude uses a tool, such as running a shell command or editing a file. The hook receives on standard input a JSON payload with the tool and its parameters. If your script exits with code 2, the action is blocked and whatever you print to standard error reaches Claude as the reason.
1. Register the hook
At the project root, in .claude/settings.json:
{
"permissions": {
"deny": ["Read(./.env)", "Read(./.env.*)"]
},
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": ".claude/hooks/no-production.sh" }
]
},
{
"matcher": "Edit|Write",
"hooks": [
{ "type": "command", "command": ".claude/hooks/no-env.sh" }
]
}
]
}
}
The permissions.deny rule keeps it from reading .env, where the passwords live. The two hooks cover what a permission list cannot describe well.
2. Block commands that touch production
.claude/hooks/no-production.sh (needs jq and execute permission with chmod +x):
#!/usr/bin/env bash
cmd=$(jq -r '.tool_input.command // empty')
# Adjust these patterns to your servers and commands
patterns='ssh .*@prod|rsync .*:/var/www|migrate.*--force|db:wipe|DROP (TABLE|DATABASE)|git push .*--force'
if echo "$cmd" | grep -Eiq "$patterns"; then
echo "Blocked: this command touches production. Ask a person to run it." >&2
exit 2
fi
exit 0
Do not try to guess everything dangerous. List the commands that already scared someone on your team or that nobody should run without a review: the deploy, forced migrations, server access. The list grows over time, and that is fine.
3. Protect configuration files
.claude/hooks/no-env.sh:
#!/usr/bin/env bash
path=$(jq -r '.tool_input.file_path // empty')
case "$path" in
*.env|*.env.*|*/config/database.php)
echo "Blocked: $path is edited by hand." >&2
exit 2 ;;
esac
exit 0
4. Test it before you trust it
A hook that never blocked anything may be broken. Test it from the terminal by sending it the same JSON it would receive:
echo '{"tool_input":{"command":"php artisan migrate --force"}}' \
| .claude/hooks/no-production.sh; echo "exit: $?"
If it prints the message and the exit code is 2, it works. Then, inside a session, ask Claude for something that breaks the rule and check that it stops and explains why.
What a hook does not solve
The hook is the second line of defense. The first is a local environment that cannot hurt production even if it tries: a test database with sample data, or a read-only user if you really need to see real data. In our case, the view counter is better fixed by turning it off when the app is not running in production than by any hook.
Keep .claude/settings.json and the hooks folder in the repository. That way the whole team works under the same rules, and changes to those rules go through review like any other code.
Which command would you put first on your list? Send me yours and we can compare it with ours.