← ~/pillars / context-engineering / standards-in-monorepos
Standards in monorepos
The two-level CLAUDE.md model: a root file that is a map, per-module files that load on demand, and claudeMdExcludes for what should not load at all.
updated 2026-06-10 ~3 min read pattern 04/05 Context Engineering
on this page
Context. Ten Java services and a handful of shared libraries in one repository. Standards exist at two genuinely different levels, repo-wide (commit format, Java version, secrets policy) and module-specific (this service’s stack, commands, and judgment calls), and the agent might be working anywhere in the tree.
Forces. A single root CLAUDE.md that inlines all ten services’ contexts blows straight through the 200-line discipline, and every session pays for nine services’ worth of irrelevant instructions. But fragmenting everything into module files loses the cross-cutting rules. The resolver is a load mechanic (memory docs): CLAUDE.md files above the working directory load in full at launch, while files in subdirectories below it load on demand, when Claude reads files there. Standards distribution in a monorepo is really context-budget routing.
Resolution. Two levels, strictly separated: a root file that is a map and nothing else, and per-module files that own everything stack-specific. The documented layout (large codebases):
monorepo/ CLAUDE.md # root: repo-wide rules, module index, commit conventions .claude/ settings.json # Read deny rules for build output; baseline claudeMdExcludes rules/ security.md # global rule — no paths field, loads every session services/ user-service/ CLAUDE.md # stack, commands, module judgment calls .claude/skills/ db-migrations/SKILL.md order-service/ CLAUDE.md libs/ common-aws/ CLAUDE.mdThe root file answers four questions only: what is this repo, how are modules laid out, what commit conventions apply, what cross-cutting constraints exist. It duplicates nothing from a module file: the monorepo root template is the complete artifact. One deliberate choice in it: the module index lists per-module CLAUDE.md paths rather than @importing them. Imports expand in full at launch (memory docs), so @importing module files from the root would reload exactly the per-module content the two-level model exists to keep out of context.
Per-module files own the stack: build and test commands, architecture notes, and the judgment calls specific to that service. They are committed, so the whole team shares them, and they should be maintained by the module’s owning team, the same review boundary as the module’s code. The Java service template is the shape of each one.
Two refinements for scale:
-
claudeMdExcludeskeeps irrelevant module files out entirely. A developer working from the repo root onuser-servicedoes not wantorder-service’s context loading when the agent happens to read a file there. The organisation ships a baseline in committed.claude/settings.jsonfor commonly-irrelevant directories; individuals extend it in gitignoredsettings.local.json. Arrays merge across scopes (settings docs):.claude/settings.local.json {"claudeMdExcludes": ["**/services/order-service/**","**/services/payment-service/**"]} -
Per-directory skills carry module procedures (DB migration steps, fixture generation) in
services/<module>/.claude/skills/, loading only when working in that module. The skill-versus-CLAUDE.md boundary is skills versus prompts.
Two operational caveats. After /compact, only the project-root CLAUDE.md is
re-read and re-injected: a long session inside one module can lose that
module’s file, so keep module files short enough that re-reading them is
cheap, and expect the agent to re-encounter them as it touches files. And
path-scoped .claude/rules/ files cut across the two levels: a rule with
paths: "**/*Test.java" lives once at the root and activates in every
module, which beats repeating a testing convention in ten module files.
Worked example. The Acme platform: three services, two libs. The root
CLAUDE.md is 38 lines: structure, commit format
(<type>(<module>): <summary>), Java 21 on Corretto, the secrets rule, and a
five-line module index pointing at per-module files. user-service/CLAUDE.md
is 32 lines:
# user-service
Spring Boot 3.3. Java 21. Gradle. PostgreSQL via Flyway.
## Commands- Build: `./gradlew build`- Test: `./gradlew test` / `./gradlew integrationTest` (needs Docker)- Format: `./gradlew spotlessApply`
## Architecture- Hexagonal: `domain/` must not import Spring or AWS- Adapters in `infra/`; ports in `domain/ports/`- ArchUnit test enforces the layering — do not suppress violations
## Judgment calls- Prefer constructor injection; never `@Autowired` on fields- Flyway migrations are append-only- New AWS calls go through `AwsClients` in `libs/common-aws`A session launched in services/user-service loads the root map (above the
cwd) plus this file, about 70 lines of context, and nothing from
order-service at all. A session at the repo root loads only the map until
the agent actually enters a module. The enforcement that backs the
architecture lines is, as always, not the prose: the ArchUnit test from
linters as ground truth
and the hooks wired in
settings-standards-enforcement
hold the rules the file merely explains.