📄 Domain 3 · Task Statement 3.1

Configure CLAUDE.md Files with Appropriate Hierarchy, Scoping, and Modular Organization

📊 Domain Weight: 20% 🎯 Difficulty: Core Concept 🔗 Scenarios: CI/CD Pipelines & Team Workflows

CLAUDE.md files are the primary mechanism for embedding persistent, scoped context into Claude Code sessions. Knowing where to place them, what to include at each level, and how they cascade is central to the exam — and to production-grade Claude Code deployments.

📋 Contents

  1. Real-World Analogy: The Employee Handbook System
  2. What is CLAUDE.md and Why It Matters
  3. The Three-Level Hierarchy
  4. Cascading Precedence Rules
  5. Diagram: CLAUDE.md Scope Resolution
  6. What Belongs at Each Level
  7. Modular Organization with @imports
  8. Code Examples: Real CLAUDE.md Files
  9. Anti-Patterns to Avoid
  10. Exam Readiness & Key Takeaways

🏭 Real-World Analogy: The Employee Handbook System

🩹 Analogy — Company, Department, and Team Handbooks

Imagine a global corporation with three tiers of documentation:

Company Handbook (global): “All employees must follow data security policy SEC-101 and use approved communication tools.” This applies everywhere, always.

Department Manual (project): “The Engineering department uses Conventional Commits format and requires all PRs to reference a Jira ticket.” Only engineers care about this; Sales ignores it.

Team Playbook (local): “The Payments team formats all currency values in USD with 2 decimal places stored as strings, not floats.” Only the Payments submodule applies this rule.

CLAUDE.md files work the same way: global rules cascade down, local rules specialize without polluting broader contexts, and every level knows only what it needs to know.

📄 What is CLAUDE.md and Why It Matters

A CLAUDE.md file is a Markdown document that Claude Code automatically reads and incorporates into its system context at the start of every session within that file’s scope. It functions as a persistent instruction layer that gives Claude awareness of:

🎯 Exam Focus — Why CLAUDE.md is Not Just a README

A README is documentation for humans. A CLAUDE.md is operational context for Claude. The key distinction: CLAUDE.md is loaded into Claude’s active context window every session, shaping its behavior. It’s a behavioral configuration file, not a reference document. On the exam, questions often test whether you know the difference between where to place human-facing docs vs. Claude-behavioral context.

Without CLAUDE.md, Claude must rediscover project context on every session through exploration, wasting tokens and risking incorrect assumptions about conventions. With a well-structured CLAUDE.md hierarchy, Claude operates like a senior team member from the first message.

🏗 The Three-Level Hierarchy

Claude Code recognizes CLAUDE.md files placed at three distinct levels in a repository. Each level has a specific location, scope, and purpose:

🌍 Global

~/.claude/CLAUDE.md

Applies across all projects on the machine. Ideal for personal coding style, preferred tools, global security constraints, and universal habits that should follow you everywhere.

📂 Project-Root

/project/CLAUDE.md

Applies to the entire repository. Contains architecture decisions, cross-cutting conventions, approved dependencies, and team-wide standards. Checked into version control and shared.

📁 Subdirectory

/project/subdir/CLAUDE.md

Applies only to files within that subdirectory. Overrides or supplements the parent-level rules for specialized modules, microservices, or domain-specific conventions.

💡 Pro Tip — Version Control Strategy

The global CLAUDE.md lives outside the repo and is never committed — it holds personal preferences. The project-root CLAUDE.md must be committed so all team members share consistent Claude behavior. Subdirectory CLAUDE.md files are committed only when that subdirectory has legitimately different conventions from the root.

Cascading Precedence Rules

When Claude Code resolves what context applies to a given file, it reads all CLAUDE.md files in the ancestor chain and combines them. The precedence order (most specific wins on conflict) is:

PrecedenceLevelFile LocationScope
Highest Subdirectory /project/src/payments/CLAUDE.md Only src/payments/**
Middle Project Root /project/CLAUDE.md All of /project/**
Base Global ~/.claude/CLAUDE.md All projects on machine

Additive by default: Instructions at all levels are combined and all apply, unless they conflict. In the case of conflict, the more specific (lower-level) file’s instruction takes precedence. For example: global says “use 4-space indent”, project says “use 2-space indent” — the project file wins for that project.

🎯 Exam Focus — Additive Not Replacement

A common exam distractor is “subdirectory CLAUDE.md replaces the project-root CLAUDE.md.” This is wrong. All levels are additive and cascading. A subdirectory CLAUDE.md supplements and can override parent instructions on specific points, but the parent’s instructions still apply for everything not overridden.

🕐 Diagram: CLAUDE.md Scope Resolution

When Claude works on a file in /project/src/payments/process.py, it reads and merges the following CLAUDE.md files in order:

Figure 1 — CLAUDE.md Cascade for a Subdirectory File
🌍 Global CLAUDE.md ~/.claude/CLAUDE.md Personal & universal rules 📂 Project-Root CLAUDE.md /project/CLAUDE.md Team-wide & architecture rules 📁 Subdirectory CLAUDE.md /project/src/payments/ Domain-specific overrides cascades into cascades into ✅ Merged Active Context Applied when working on: /project/src/payments/process.py Global rules (personal style) + Project rules (team standards) + Payments rules (domain overrides) Specific overrides take precedence Everything non-conflicting applies Key: All ancestor CLAUDE.md files are additively merged. Most-specific file wins on conflict. No level is ignored or overwritten entirely.

📝 What Belongs at Each Level

Knowing where to place content is as important as knowing what to write. Misplacing content causes unnecessary noise (global pollution) or missing context (too narrow scope).

LevelBelongs HereDoes NOT Belong Here
Global Personal coding style preferences
Default language/framework preferences
Explicit “always ask before deleting files” safety rules
Favorite tools (ripgrep, fd, jq)
Personal API key naming conventions
Project-specific package names
Database schemas or domain types
Team-specific branching strategy
Any repo-specific paths or URLs
Project Root Tech stack and architecture overview
Approved/forbidden libraries
Branching strategy and PR conventions
Test framework and coverage requirements
Environment setup instructions
Coding standards (indent size, naming)
Common commands (build, test, lint)
Subdirectory-specific data formats
Module-internal implementation details
Personal style preferences
Subdirectory Module-specific business rules
Domain model and type conventions
Integration-specific patterns for that module
Local override of parent conventions
Subservice-specific environment vars
Universal coding standards (in root)
Auth/security policies (in root or global)
Cross-cutting architectural patterns

🔗 Modular Organization with @imports

Large projects can grow CLAUDE.md files that become unwieldy — hundreds of lines covering coding standards, architecture, tooling, security rules, and more. The solution is modular organization using @-prefixed imports:

Markdown — Project Root CLAUDE.md with @imports
# Project: PayStream Platform

## Architecture Overview
Microservices on ECS Fargate. All services expose REST APIs documented via OpenAPI 3.1.
Event-driven communication via SQS. No direct DB-to-DB calls between services.

## Imported Modules
@docs/claude/coding-standards.md           # Language-specific style guides
@docs/claude/testing-conventions.md         # Test framework, coverage rules
@docs/claude/approved-libraries.md          # Allow/deny list of packages
@docs/claude/security-requirements.md       # Auth patterns, secrets handling
@docs/claude/common-commands.md             # Build, test, deploy commands

## Critical Rules (inline, not imported)
- NEVER commit secrets or API keys to version control
- Always check for existing utility functions before implementing new ones
- Every new service endpoint must have a corresponding OpenAPI spec entry
💡 Design Principle: Inline What’s Critical, Import What’s Reference

Keep the most critical, always-needed rules inline in the main CLAUDE.md so they’re unmissable. Move verbose reference material (long lists of libraries, detailed style guides) into imported modules that Claude can load on demand. This keeps the primary file scannable and avoids burying critical instructions in long scrolling content.

Benefits of Modular Organization

💻 Code Examples: Real CLAUDE.md Files

Markdown — Global CLAUDE.md (~/.claude/CLAUDE.md)
# My Global Claude Preferences

## Code Style
- Prefer explicit over implicit. Verbose variable names over cryptic abbreviations.
- Python: type hints on all function signatures, docstrings for public functions.
- TypeScript: strict mode always. No `any` type.

## Safety Rules (Personal)
- Always show me a diff before modifying more than 5 files at once.
- Never run destructive shell commands (rm -rf, DROP TABLE) without explicit confirmation.
- Prefer non-destructive alternatives and suggest them when relevant.

## Tools I Prefer
- File search: ripgrep (rg) and fd, not grep and find
- JSON processing: jq
- HTTP testing: httpie (http) not curl for readability

## Communication Style
- Be concise. Skip preambles. Start with the answer, then reasoning.
- Flag architectural concerns as separate items, not buried in implementation notes.
Markdown — Project Root CLAUDE.md (/project/CLAUDE.md)
# PayStream Platform

## Stack
- Backend: Python 3.12, FastAPI, SQLAlchemy 2.0, Alembic
- Frontend: TypeScript, React 18, Vite, TailwindCSS
- Infra: AWS ECS Fargate, RDS PostgreSQL, SQS, CloudFront

## Architecture Rules
- Services communicate via SQS events — no direct service-to-service HTTP in hot paths.
- All DB interactions go through the repository layer in `src/repositories/`.
- Never bypass the repository layer with raw SQL in handlers/services.

## Coding Standards
- Indentation: 4 spaces (Python), 2 spaces (TypeScript/JSON/YAML).
- Naming: snake_case for Python, camelCase for TypeScript variables, PascalCase for classes.
- No wildcard imports (`from module import *`).

## Testing
- Framework: pytest with pytest-asyncio for async handlers.
- Coverage minimum: 80% for new modules, 70% overall.
- Integration tests live in `tests/integration/`, unit tests in `tests/unit/`.
- Always run `make test` before committing.

## Common Commands
- Run tests: `make test`
- Run linter: `make lint`
- DB migrations: `alembic upgrade head`
- Start dev server: `make dev`

## Critical Rules
- NEVER commit `.env` files or any file containing API keys.
- Check `src/utils/` before implementing new utility functions.
- PRs must reference a Jira ticket in the format `[PAYS-123]` in the title.
Markdown — Subdirectory CLAUDE.md (/project/src/payments/CLAUDE.md)
# Payments Module

## Domain Rules (OVERRIDES project root where noted)

### Currency Handling
- ALWAYS store monetary values as integers (cents/pence), never floats.
- Use `Decimal` for intermediate calculations, convert to `int` before persistence.
- Display formatting is handled exclusively in the presentation layer, NOT here.
- Override: Unlike other modules, amounts here use `amount_cents` not `amount`.

### Payment States
Valid transitions: pending → processing → completed | failed | refunded
Never skip states. Use `src/payments/state_machine.py` for all transitions.

### PCI Compliance (CRITICAL)
- Raw card numbers must NEVER appear in logs, errors, or return values.
- Use tokenized references (pay_tok_XXXX) from the payment provider exclusively.
- Any logging in this module: run through `payments.utils.sanitize_for_log()` first.

### Testing (Supplements root rules)
- Mock the payment gateway using `tests/mocks/payment_gateway_mock.py`.
- All payment flow tests must cover the failure path, not just happy path.

## Key Files
- State machine: `src/payments/state_machine.py`
- Gateway abstraction: `src/payments/gateway/`
- Models: `src/payments/models.py`

Anti-Patterns to Avoid

⛔ Monolithic Root CLAUDE.md

Putting everything in one massive file (500+ lines) at the project root. Critical rules get buried, the file becomes hard to maintain, and context becomes irrelevant to the scope Claude is working in.

⛔ Global Pollution

Placing project-specific rules in the global ~/.claude/CLAUDE.md. These project rules bleed into every other project you work on, causing confusion and potentially incorrect behavior in unrelated codebases.

⛔ Subdirectory Without Need

Creating subdirectory CLAUDE.md files for modules that have no legitimately different conventions from the root. This adds maintenance overhead with no benefit and fragments context unnecessarily.

⛔ Stale CLAUDE.md

Not updating CLAUDE.md when the tech stack, conventions, or architecture changes. Outdated instructions mislead Claude into following deprecated patterns confidently. CLAUDE.md must evolve with the codebase.

⛔ Missing Critical Inline Rules

Burying the most critical constraints (security rules, “never do X” prohibitions) inside imported @module files, where they may not be visible at a glance. Critical rules should always be inline in the main file.

⛔ README Confusion

Putting human-facing explanatory documentation in CLAUDE.md assuming Claude will share it. CLAUDE.md is behavioral config for Claude, not documentation. It should read as imperative instructions, not narrative prose.

✓ Modularize by Domain

Use @imports to split large CLAUDE.md files into domain-focused modules. Root file stays concise; detailed standards live in imported topic files (security, testing, APIs).

✓ Scope to Where It’s Needed

Ask: “Does this rule apply to the whole project, or just to one subdirectory?” If just a subdirectory, put it there. Avoid root-level clutter from local concerns.

✓ Treat as Living Docs

Review CLAUDE.md files during sprint retros or major refactors. Make CLAUDE.md updates part of the definition of done for architectural changes.

Exam Readiness & Key Takeaways

🎓 Exam Scenario — CI/CD Monorepo Configuration

Scenario: A monorepo contains three services: auth/, payments/, and notifications/. Each has distinct conventions. The team shares common coding standards. Where should CLAUDE.md files be placed?

Correct answer: One project-root CLAUDE.md for shared team standards, plus individual CLAUDE.md files inside auth/, payments/, and notifications/ for service-specific conventions. Each inherits the root standards and adds its own.

Common distractor: “One massive root CLAUDE.md with all three services’ rules combined” — wrong, this violates the Single Responsibility principle for configuration scoping.

🎯 Exam Focus — Precedence Quick Test

If global says “use 4-space indent”, project root says “use 2-space indent”, and src/api/CLAUDE.md says “use tabs”, what applies in src/api/handler.py?

Answer: Tabs — the most specific level (subdirectory) wins for conflicting rules. All non-conflicting rules from global and project root still apply.

1
CLAUDE.md is behavioral config, not documentation. It’s loaded into Claude’s active context every session to shape its behavior. It’s imperative instructions for Claude, not narrative explanations for humans.
2
Three levels: global (~/.claude/), project root, subdirectory. Each has a specific scope. Global = personal cross-project preferences. Root = team-wide standards (committed). Subdirectory = module-specific overrides (committed only if needed).
3
Cascade is additive, not replacement. All ancestor CLAUDE.md files are merged and all apply. The most specific file wins only on conflicting rules. Parent instructions still apply for everything else.
4
Use @imports for modular organization. Split large CLAUDE.md files into topic-specific modules (security, testing, APIs). Keep critical “never do X” rules inline in the main file, not buried in imports.
5
Scope rules to their narrowest required level. If a rule only applies to the payments module, put it in payments/CLAUDE.md, not the root. Root pollution with narrow rules wastes context and reduces clarity.
6
Version control the right files. Global CLAUDE.md = never committed (personal). Project and subdirectory CLAUDE.md files = committed and shared with the team. All team members must get consistent Claude behavior from the same source of truth.
7
Treat CLAUDE.md as a living document. Outdated instructions cause confident incorrect behavior. Update CLAUDE.md as part of architectural changes, tech stack migrations, and convention updates. Make it part of your Definition of Done.