Build your own agentic layer, part 4: hooks, the rules the model cannot forget

Part 4 of a series on building your own agentic layer. Part 3 covers tools, MCP, and RAG.
Everything so far has been a request. A rule says "never do X", and the model will usually not do X. Usually is fine for commit message style. It's not fine for "don't delete my home folder" or "don't touch production".
That's what hooks are for.
Hooks: code, not suggestions
A hook is a script the harness runs when something happens. Claude Code has a set of events you can attach scripts to:
- SessionStart when a session opens
- UserPromptSubmit when I send a message, before the model sees it
- PreToolUse right before the agent runs a tool
- PostToolUse right after
- Stop when the agent finishes its turn
- plus a few others (Notification, PreCompact, SessionEnd, SubagentStop)
The script gets the details as JSON on stdin (which tool, what command, which file) and can answer with JSON. A PreToolUse hook can answer allow, ask, or deny, plus a reason. On a deny, that reason goes back to the model.
The difference that matters: the model is probabilistic. Same prompt, slightly different answer each time. A hook is deterministic. A bash script that blocks rm -rf ~ blocks it every time, no matter how the conversation went or how long it's been. People call these guardrails.
Trillian has 24 hooks now. Here are the ones I'd build first.

Flip the permission model
By default, Claude Code asks before running most commands. That's safe, but it means you can't walk away. The first command it isn't sure about, it stops and waits for you.
You could list every safe command in an allowlist. I went the other way. Trillian's auto-approve.sh runs before every tool call and allows everything except a denylist. The denylist has two tiers:
- Deny for things that are never OK: a recursive force delete of
/or my home folder, a fork bomb, wiping a disk. - Ask for things that are sometimes OK but I want to decide:
sudo, anyrm -rf,git push --force, throwing away uncommitted work, publishing a package, destructive SQL, piping a download straight into a shell, and reading anything that looks like a secrets file.
The answer it sends back looks like this:
{"hookSpecificOutput":{"hookEventName":"PreToolUse",
"permissionDecision":"ask",
"permissionDecisionReason":"recursive force delete"}}
It's fail-safe. If the script can't parse the request, it doesn't answer at all, and Claude Code falls back to its normal prompt. It never allows something it couldn't read.
This is the hook that lets me run agents unattended. The other half of that is getting pinged when one needs me, which I wrote about in Claude, ping me.
Every stop gets logged, so I can see what it's actually catching:

432 stops for touching a secrets file. 242 for rm -rf. Those are stops, not disasters. Each one is a moment where I got asked instead of the agent going ahead, and I didn't have to be watching for it.
Production is off limits
This one has a story. An agent once pulled a live config file off a production server through the hosting provider's admin API, then went on to query real customer data with what it found.
So now there's prod-guard.sh, with two layers:
- Anywhere, always: any command that reaches the cloud provider's admin API for a web app, or dumps connection strings, app settings, or secret values, is denied. Hard stop.
- Per repo: each repo can list its production database hosts. Once a command targets one of those, any write is denied, any
SELECT *or row-level read is denied, and even aCOUNT(*)still asks me first.
It matches on command text, so it's best-effort. It narrows the hole, it isn't a sandbox. The deny reason goes back to the model, so it knows why it was stopped and can pick a different way.
Small gates that save a lot
- Branch before work. You can't edit files while you're on
main. The deny message tells Claude the exact command to cut a fresh branch from the up-to-date remote. It's blocked 71 times so far, every one a time the agent would have started work on trunk. - No secrets in prompts. A UserPromptSubmit hook blocks a message from me that looks like it has a token or password in it. Starting the message with
!!overrides it. - Check every install. After any
npm,yarn, orpnpminstall, a PostToolUse hook runs that package manager's own audit and tells Claude the vulnerability count. It also scans new packages' install scripts for the patterns used by the 2025 self-spreading npm worms, like reading cloud credentials or pushing to GitHub during install. - Don't bury my to-do list. A Stop hook blocks the turn if Claude hands me a block of commands to run with no hand-off file written. Chat scrolls. Files don't.
When a hook goes too far
Not every hook earned its place. I had one that blocked Claude from handing me a commit message unless a code review had run first. Good idea. In practice it kept demanding a fresh review between commits of a change that had already been reviewed. It blocked 63 times before I turned it off by default.
I also added a hook that asked me to name every new session. It broke, and it came out the next day.
Two habits that made this easier:
- Almost every hook has a kill switch. An environment variable like
TRILLIAN_BRANCH_GATE=0turns it off for one session without editing anything. - Hooks fail open on surprises. If a hook can't find
gitor can't parse its input, it steps aside instead of blocking my work. The permission hook is the careful one: when it can't tell, it falls back to asking me.
Links
- Claude Code hooks reference, with every event and what each one receives
- Claude Code permissions and settings
- Claude, ping me, the notification hooks
- CISA alert on the 2025 npm supply chain compromise
- npm audit
- jq, which most of the hooks use to read their input
Drafted with an assistant, edited by @wintaru
0 comments