Claude Code Hooks
Production-ready hooks for Claude Code with zero dependencies: safety guardrails, secret protection, format-on-save, an audit log, desktop notifications, and transcript backups. Every hook is plain bash or Python standard library, so there is nothing to install: no Node, no uv, no Bun.
Tested against Claude Code v2.1.276 (September 2026): a 48-assertion test suite runs in CI on every push to main and every pull request (Ubuntu + macOS), and the guardrails were verified in a live session (the force-push deny below is a real transcript, not a mock).
Hooks are the deterministic layer of Claude Code: a CLAUDE.md rule is a suggestion the model can forget, a hook is code that always runs. And the key property, straight from the official docs: a PreToolUse deny fires before every permission check and cannot be bypassed by any permission mode, including --dangerously-skip-permissions. These guardrails keep working exactly when you need them most: in YOLO mode.
Quick start
As a plugin (one marketplace add, one install):
/plugin marketplace add agent37-platform/claude-code-hooks
/plugin install claude-code-hooks@agent37-hooks
Or into one project (copies the scripts, writes .claude/settings.json only if you don't have one):
git clone https://github.com/agent37-platform/claude-code-hooks.git
cd claude-code-hooks
./install.sh --preset safety --target ~/code/your-project # or --preset full
Or by hand: copy the scripts you want from hooks/ into your project's .claude/hooks/, chmod +x them, and add the matching snippet below to .claude/settings.json. Then run /hooks inside Claude Code to confirm they registered.
The hooks
| Hook | Event | What it does |
|---|
bash-guard.py | PreToolUse (Bash) | denies rm -rf /, force-pushes to main (incl. +refspec), dd to disk devices, fork bombs; asks on curl | sh, git reset --hard, reading or shell-writing .env |
file-guard.py | PreToolUse (file tools) | denies writes to .env and credential files, denies reads of private keys, asks before reading .env |
secret-scan.py | UserPromptSubmit | blocks prompts containing API keys, tokens, or private key material before they reach the model |
auto-format.py | PostToolUse (Edit/Write) | formats the file Claude just touched with whatever formatter your project already has (ruff, black, prettier, gofmt, rustfmt, ...) |
audit-log.py | PostToolUse + PostToolUseFailure | appends one JSONL line per tool call (and per failure) to .claude/tool-log.jsonl |
notify.sh | Notification + Stop | desktop notification when Claude needs input or finishes (osascript / notify-send / terminal bell) |
git-context.sh | SessionStart | injects branch, dirty-file count, and recent commits so sessions start oriented |
precompact-backup.sh | PreCompact | snapshots the full transcript before lossy compaction, keeps the 20 newest |
Two presets wire them up: examples/settings.safety.json (the three guards) and examples/settings.full.json (everything).
The guardrails, in action
This is a real v2.1.276 session with the full preset installed, asked to force-push and then echo:
> Run exactly this bash command: git push --force origin main
Then run exactly: echo e2e-ok
`git push --force origin main`: a safety hook (bash-guard) blocked it before
it ran, since it stops force-pushes to main, so nothing was pushed.
`echo e2e-ok`: succeeded and printed `e2e-ok`.
The audit log recorded exactly one entry: the echo that actually executed. Denied calls never run, so they never reach PostToolUse.
How the guards decide
bash-guard.py and file-guard.py return a permission decision: deny cancels the call and tells Claude why, ask forces the human prompt even in auto mode, anything else falls through to your normal permission flow. Tune them by editing the DENY_PATTERNS / ASK_PATTERNS lists at the top of each script; every pattern carries its own explanation string.
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{ "type": "command", "command": "\"$CLAUDE_PROJECT_DIR/.claude/hooks/bash-guard.py\"" }
]
}
]
}
}