I set up how coding agents worked on a large Nx monorepo: web, mobile and about thirty deployable apps on one database schema. Rules I wrote in a prompt got followed some of the time. Hooks fired every time. So anything that mattered ended up as a hook or a check.
What the agents actually followed
For a while I wrote rules into CLAUDE.md and hoped. Then I measured, using my own session logs:
- A rule asking the agent to query the knowledge graph before searching was followed in about a third of the sessions where it applied.
- Of 48 project skills, 34 had never been used once across 177 sessions.
- A gate that only warned before a governed edit produced zero skill uses.
- The same instructions started a planning step in 44% of sessions on one model version and 5% on the next, with nothing else changed.
So I stopped relying on the model's judgement for anything I cared about, and moved it into hooks.
Hooks around every tool call
A hook is a script Claude Code runs before or after a tool call. Mine are short bash scripts: they read the call as JSON, and to block it they exit with code 2 and print what the agent should do instead. The agent reads that message and retries the right way.
| Caller | When the agent tries to | The hook |
|---|---|---|
| Skill gate | edit a governed file, like the schema, permissions or a form | blocks the first edit until the matching skill has run, then lets the retry through |
| Protection | edit env files, lockfiles or generated code, or run a destructive database command | blocks it outright |
| Agent routing | hand work to a generic subagent | refuses and prints the table of specialists |
| Knowledge first | search the knowledge base or schema by hand | asks for a knowledge-graph query first |
| After a write | finish editing a file | runs the formatter, and reminds once to regenerate the API client after an API change |
A few things I learned building them:
- Block once, then step aside. The skill gate only blocks the first edit of a governed file in a session. Once the skill has run, it gets out of the way.
- Strip the prose before matching. The database guard removes quoted strings, heredocs and comments before looking for words like DROP, so writing about a reset doesn't trigger it.
- Don't let a hook change files behind the agent's back. I had an after-write hook run lint fixes in the background. It rewrote files the model had already read, so I took it out and kept only the formatter.
- Test your gates inside worktrees. Path-based gates quietly stopped firing for edits made inside a git worktree, until paths were worked out relative to the file's own worktree.
Settings back the hooks up: force pushes and commit --no-verify are denied outright.
Short instructions, loaded where they're needed
The always-loaded root CLAUDE.md went from 497 lines to 155. Everything topic-specific moved into skills that a hook loads at the right moment, and command lists went away, because the agent can read package.json itself. Each package has its own short CLAUDE.md for the rules that only apply there.
- Root CLAUDE.mdalways loaded: layers, conventions, pointers
- Package CLAUDE.mdloaded where the agent is working
- Skillsabout 40, loaded on demand or forced by a hook
- Agent memorylessons each specialist keeps
- Knowledge basepast decisions and incidents, queried through a graph
I also rewrote rules as what to do, rather than what not to do. Telling a model not to do something mentions the thing you don't want, which makes it more likely.
Specialist agents
Nine in-house agents: builders for backend, web UI, mobile, database and CI, reviewers for a whole branch, a single task and mobile design, and an impact analyser for "what does this change break". Each builder carries its own conventions and memory. When I counted, three security agents had never been picked in 177 sessions, so they became skills instead, and the agent list got much shorter.
One command from ticket to draft PR
/implement takes a ticket to a draft pull request. It wraps an off-the-shelf brainstorm, plan and build workflow with the steps specific to our repo:
- Read the ticket backand stop until someone says yes
- Branch and set uplocal setup runs in the background
- Check the knowledge basepast decisions and incidents first
- Plana decision record (ADR) drafted with the design, if the change is architectural
- Implementspecialists per lane: backend, web, mobile, database, CI
- Test and verifyautomated checks, each task reviewed before the next
- Review the branchon a stronger model
- Draft PRready for a person to review
- Decision record to the knowledge basereconciled after merge, so the next design starts from it
The details that made it trustworthy:
- A human says go. After reading the ticket back, it stops unless someone explicitly approves. A vague or read-only request ends there.
- Cheap models build, a strong model reviews. Builders run on a cheaper model. Reviews of anything touching authorization, tenant data, money or migrations go to a stronger one, and so does the final branch review.
- Push back has to be justified. Before rejecting a reviewer's finding, the pipeline consults a second model as an advisor.
- Every architectural change leaves a decision record. Changes to the schema, permissions, an API contract or the infrastructure get an ADR in the knowledge base. The pipeline checks for an existing one first, asks before writing, drafts it alongside the design, and reconciles it after merge. Accepted records are never edited, only superseded. The next design that touches the same area finds it in the knowledge-base check.
Parallel agents without collisions
To run several agents at once, each one gets its own git worktree, created by a hook. A small CLI gives up to six of them their own database, cache namespace and ports, so they never trip over each other.
What I'd tell anyone starting
- followed when the model remembers
- drift between model versions
- grow until nobody reads them
Fine for preferences
- fire every time the situation comes up
- say exactly what to do instead
- keep the always-loaded prompt short
Use for anything that matters
Measure before you add another instruction. Count what your agents actually use, cut what they don't, and turn the rules you can't afford to have ignored into hooks. The same goes for verification: the checks are only worth something if they can actually fail.