← ~/pillars / context-engineering / claude-md-architecture

CLAUDE.md architecture

The four-tier memory hierarchy, @imports, path-scoped rules, and the 200-line discipline: what the best public files share and the anti-patterns that sink the rest.

updated 2026-09-21 ~5 min read pattern 01/05 Context Engineering

on this page

Context. Your team’s coding standards need to reach the agent on every task: build commands, architecture boundaries, the judgment calls a linter cannot make. CLAUDE.md is the delivery mechanism, but it is a layered system with specific load mechanics, not a single file you keep appending to.

Forces. Every line in a loaded CLAUDE.md costs tokens on every turn, and the cost is not just financial. CLAUDE.md content is delivered as a user message after the system prompt, not as part of the system prompt itself (memory docs). It competes with the user’s actual request for attention, and there is no strict-compliance guarantee. Anthropic’s stated discipline: target under 200 lines per file, because longer files consume more context and reduce adherence. The best-practices docs are blunter still: if your CLAUDE.md is too long, Claude ignores half of it, because important rules get lost in the noise (best practices). Against that pulls the team’s understandable urge to write everything down once and be done.

Resolution. Treat CLAUDE.md as a hierarchy with a placement decision per rule, and hold every file to the 200-line discipline by moving content to the layer that loads only when needed.

The hierarchy

Four tiers, loaded broadest-first and concatenated: later files add to, never override, earlier ones (memory docs):

TierLocationShared withUse for
Managed policy/Library/Application Support/ClaudeCode/CLAUDE.md (macOS), /etc/claude-code/CLAUDE.md (Linux)Everyone on the machine; cannot be excludedOrganisation-wide style and security guidance
User~/.claude/CLAUDE.mdJust you, all projectsPersonal workflow preferences
Project./CLAUDE.md or ./.claude/CLAUDE.mdThe team, via source controlTeam standards, the main event
Local./CLAUDE.local.mdJust you, this projectSandbox URLs, local test-data paths

The load mechanics matter for monorepos: files in directories above the working directory load in full at launch; files in subdirectories below it load on demand, when Claude reads files there. After /compact, only the project-root file is re-read and re-injected. Nested files are not. And HTML comments are stripped before injection, so maintainer notes cost zero tokens.

AGENTS.md, from v2.1.277

Claude Code now reads AGENTS.md, the cross-vendor instruction file, directly. The rule is narrow and worth knowing exactly, because it is a fallback rather than a merge (memory docs):

Your repository hasClaude reads
An AGENTS.md, and no CLAUDE.md or CLAUDE.local.md in your working directory or above itYour AGENTS.md
An AGENTS.md and a CLAUDE.md or CLAUDE.local.md in your working directory or above itYour CLAUDE.md files only
A CLAUDE.md that already imports AGENTS.mdYour CLAUDE.md, with AGENTS.md included through the import

Two consequences for a team that maintains both files. First, AGENTS.md is silently ignored the moment anyone anywhere up the tree has a CLAUDE.md, including their own gitignored CLAUDE.local.md, so “we keep them in sync” is not a policy you can verify by reading a session transcript. Second, if you want both loaded, say so: the Project instructions setting takes claude-md-and-agents-md alongside the default claude-md-or-agents-md, claude-md, and managed-only.

The safer construction is the third row. @AGENTS.md from your CLAUDE.md and the behaviour stops depending on a version floor, a setting, or a feature flag, which matters because this one is gated on all three: direct reading needs v2.1.277 or later, and Claude cannot do it at all when “your session doesn’t fetch feature flags from Anthropic, for example because you use Amazon Bedrock or another third-party provider, or you disabled telemetry”. For a team on Bedrock that is not an edge case, it is the default deployment: import the file.

Three organising tools sit alongside the hierarchy:

  • @imports (@path/to/file, max depth 4, relative to the containing file) expand inline at launch. They help you organise; they do not reduce the context bill. Never @import something you only sometimes need.
  • .claude/rules/ holds markdown rule files with an optional paths: frontmatter field. Rules with paths load only when Claude works with matching files: the documented mechanism for rules that apply to a subset of the tree, such as src/test/**/*.java.
  • Auto memory (default-on since Claude Code v2.1.59) lets Claude write its own notes to ~/.claude/projects/<project>/memory/MEMORY.md; only the first 200 lines or 25 KB load at session start. Risk for standards work: a memorised preference can quietly contradict a team CLAUDE.md rule for one developer. Audit /memory periodically, or set "autoMemoryEnabled": false where consistency matters more than recall.

What the strong public files share

Across the strongest examples in the awesome-claude-code collection, five properties recur:

  1. Build and test commands first, always copy-pasteable. The single most universal element.
  2. A project structure or module map, cheaper than the agent exploring.
  3. Specific, falsifiable rules. “No wildcard imports”, not “keep imports clean”; “files under 300 lines”, not “keep files manageable”.
  4. Explicit behaviour constraints where the model’s default differs from the team’s (“never run tests unless asked”).
  5. Cross-references instead of inline documentation: pointers to files, not copies of them.

And one inverse correlation: the weakest files are the long ones. The collection’s AI-IntelliJ-Plugin file does its whole job in ~24 lines: commands, style, patterns, every line actionable. The SG-Cars-Trends-Backend root file runs ~549 lines and has grown past its usefulness: API endpoint lists, UTM tracking architecture, and inline database schema that belong in a skill or a referenced doc. Its one redeeming structural move, explicitly listing the per-component CLAUDE.md paths, is exactly what the monorepo pattern formalises.

The other anti-patterns to audit for: rules that contradict the linter (CLAUDE.md says alphabetical imports, Spotless reorders them, the agent loops); prompt rules that should be hooks (“always run spotlessApply before committing” is a PostToolUse hook, full stop; see the policy hierarchy); and aspirational vagueness (“write clean, maintainable code”, “follow SOLID”). Claude already knows SOLID, and an unfalsifiable rule changes no decision (humanlayer, techsy).

Where each kind of content goes

  • Always-relevant fact (commands, architecture, judgment calls) → project CLAUDE.md, under 200 lines.
  • Rule scoped to a file type or subtree → .claude/rules/ with paths:.
  • Module-specific context in a monorepo → that module’s own CLAUDE.md, maintained by the owning team (monorepo pattern).
  • Procedure or reference material → a skill, loaded on demand (skills versus prompts).
  • Anything a tool could verify → not prompt-space at all (linters as ground truth).

Worked example. A Spring Boot service’s CLAUDE.md has crept to ~320 lines. The audit, using the self-test “would removing this line cause a mistake?”:

  • Lines 1–25: commands and architecture facts. Keep.
  • ~60 lines of formatting and import rules. Delete entirely: Spotless owns them via the format-on-edit hook, and their presence risks the contradiction loop.
  • ~80 lines of test conventions (builders, Testcontainers setup, naming). Move to .claude/rules/testing.md with paths: "src/test/**/*.java", loaded only when test files are in play.
  • ~100 lines of release runbook. Move to a skill; CLAUDE.md keeps one line pointing at it.
  • ~30 lines of personal sandbox endpoints one developer added. Move to their CLAUDE.local.md.

What remains is ~45 lines of facts and judgment calls, the shape of the Java service template, and every remaining line is one the agent should actually weigh on every turn.

// sources