Spec-driven development, for real
Spec-driven development, for realPart 3
Principles and scoped rules
On this page
New to the short codes like REQ, ADR or AC? See the vocabulary in Part 1, or hover over any code.
In short: A small set of numbered principles (P1 to P12) binds the code; breaking one needs a decision record, not a judgement call in a pull request. A short root CLAUDE.md holds what every session needs: commands, working rules and what “done” means. Each module has its own CLAUDE.md with the rules true only there, which Claude reads when it works in that folder. And every rule gets teeth: a test, a hook or a reviewer that notices when it breaks.
Part 2 gave every requirement and decision an ID. This part covers block 2, the rules Claude follows while it builds. The team and product are invented.
Monday, 10:15
Maya has found REQ-041. Before she asks Claude for a plan, she opens the folder the fee will live in: src/billing/. There is a CLAUDE.md in it, eighteen lines long. One line says money is always integer minor units. Another says an open question, OQ-07, decides which ledger account fees post to, and that nobody may guess it.
She realises Claude will read that file too, the moment it opens anything in billing/. The rule will be on its desk before it writes a single line.
Think of it like this: A country has a constitution: a few rules that hold everywhere and change only with a formal process. Each department pins its own rules on the wall of its room: the lab has goggles, the kitchen has allergens. Nobody carries every department’s rules in their head. They read the wall of the room they walk into.
Rules, from broad to specific
The principles
Principles are the constitution. They are few, numbered and binding. Each one is written so a reviewer could tell from the code whether it was broken.
Company-specific behaviour is data. Checked by: review; no if company ==.
Domain code never imports a provider SDK. Checked by: an edit warning on vendor imports.
A hidden field is absent from the response. Checked by: field-security tests.
Invariants do not depend on step order. Checked by: tests with reordered steps.
Types come from OpenAPI. Checked by: the CI drift gate.
Integer minor units end to end. Checked by: types and tests.
Rule: going against a principle needs an ADR (Part 4). Not a comment, not “just this once” in a pull request. That sounds heavy. In practice it means exceptions are rare, deliberate, and findable.
What Claude has read when it edits
Claude Code loads the root CLAUDE.md at the start of every session. A CLAUDE.md in a subfolder loads when Claude reads files in that folder, so the billing rules arrive exactly when they are needed and cost nothing the rest of the time.
Here is the one Maya found:
# billing: rules for this module ## What this module owns Charges, refunds and fees for a rental. Emits ChargeCreated. ## Rules here - Money is integer minor units. Never a float, never a Decimal on the wire (P11). - Late fees are computed only by terms.late_fee_minor (DEF-late-fee). Never inline the formula. - OQ-07 is open: which ledger account late fees post to. accounts.late_fee_account returns None until finance answers. Do not pick a default. ## Tests that must exist - Isolation: company A cannot read company B's charges. - Field security: a customer's response has no cost_minor. ## Handoff docs/modules/billing.md
Under the hood: how Claude Code loads these files
- At launch, Claude Code walks up from the working folder and loads every
CLAUDE.mdit finds, plus~/.claude/CLAUDE.mdfor personal rules. - A
CLAUDE.mdin a subfolder loads on demand, when Claude reads a file in that folder. - Rules in
.claude/rules/*.mdwith apathslist load only when Claude reads a matching file; withoutpathsthey load at the start. - The docs advise keeping each
CLAUDE.mdunder about 200 lines: longer files use more context and are followed less reliably. The real repositories this series comes from had root files over 600 lines. That worked, but it is the first thing worth trimming. /memorylists which files are loaded right now. See the memory docs.
Giving a principle teeth
A principle nobody checks drifts within a month. Each one should have at least one mechanism that notices:
- 1 · Write it as a rule you can check
"Domain code never imports a payment SDK" can be checked. "Keep things decoupled" cannot.
- 2 · Number it
P2. Reviews, ADRs and code comments now cite it in two characters.
- 3 · Teach the alarm
An edit warning in
spec-kit.jsontells Claude the moment an edit breaks it, with the reason.{ "files": "src/{rentals,billing}/**/*.py", "pattern": "^(from|import)\\s+(stripe|paymentsdk)", "message": "vendor import in domain code (P2): depend on the port." } - 4 · Write the test
Where a rule can be tested, test it. Field security, isolation and money all have tests.
- 5 · Exceptions need an ADR
A pull request that goes against P2 links the decision record that allows it (Part 4). No record, no merge.
The edit warning in step 3 is a real hook from the kit, edit-warnings. After every edit it checks the file against your patterns and tells Claude, with the rule’s reason, while Claude still has the context to fix it. Part 10 covers it in full.
What belongs in the root file
- Commands to build, test and export the contract.
- The working rules: plan first, read the handoff, cite IDs.
- What "done" means here.
- Pointers: where principles, ADRs and handoffs live.
- Long explanations: link the doc instead.
- Rules for one folder: put them in that folder’s CLAUDE.md.
- Procedures: make them skills (Part 9).
- History: that is what ADRs and context drops are for.
Which one fits?
- It must hold everywhere, always: A principleNumbered in principles.md, checked by a test, hook or review, and changed only by an ADR.
- Every session in this repo needs it: Root CLAUDE.mdCommands, working rules, the definition of done. Under about 200 lines.
- It is only true in one module: That module’s CLAUDE.mdLoaded when Claude works in that folder. Write it to stand on its own.
- It applies to one kind of file anywhere: A path rule.claude/rules/ with a paths list, for example every serializers file.
- It is a procedure with steps: A skillLoaded only when used (Part 9).
In the kit
templates/api/CLAUDE.mdandtemplates/web/CLAUDE.md: root files under 100 lines.templates/api/src/_module/CLAUDE.md: the module template.docs/architecture/principles.md: twelve example principles, each with how it is checked. Rewrite them with your team.
Three rules of thumb
If nobody could tell from the code whether a rule was broken, it is advice, not a rule.
Repo-wide rules at the root, module rules in the module. Never repeat one in the other.
A long CLAUDE.md is followed less reliably. Link out; do not paste in.
QuizThe billing team wants every fee rounded down to whole minor units. Where does that rule go?
Show the answer
Answer: src/billing/CLAUDE.md, and a test in billing
It is true only in billing, so it belongs in billing’s own rules, where Claude reads it exactly when it works there. The test makes sure it stays true.
Words in this post:
- Principle: a numbered, binding rule for the whole codebase, like
P2. - Root CLAUDE.md: the repo-wide rules file, loaded at every session start.
- Scoped CLAUDE.md: a rules file in a subfolder, loaded when Claude works there.
- Path rule: a rule file that loads only for matching file paths.
- Teeth: the test, hook or review that notices a broken rule.
Next, Part 4: Decision records handles the exceptions: one file per decision, with the option you rejected and why.
Setting up Claude Code or AI agents for your team?
I help teams build this: agents, RAG search and Claude Code setups that hold up in production.
Series
Spec-driven development, for real
- Spec-driven development, for realThe building blocks
- Spec-driven development, for realThe paper trail
- Spec-driven development, for realPrinciples and scoped rulesyou are here
- Spec-driven development, for realDecision records
- Spec-driven development, for realDefinitions and open questions
- Spec-driven development, for realFeature specs and plans
- Spec-driven development, for realThe contract
- Spec-driven development, for realGates that make Claude show its work
- Spec-driven development, for realSkills and reviewers
- Spec-driven development, for realGuardrail hooks
- Spec-driven development, for realProof of done
- Spec-driven development, for realMemory and handoffs
- Spec-driven development, for realOne ticket, end to end