CLAUDE.md Hierarchy & Configuration
Understand the CLAUDE.md configuration hierarchy and how project, user, and directory-level settings interact.
CLAUDE.md configuration layers:
User-level: ~/.claude/CLAUDE.md (personal preferences across all projects)
Project-level: .claude/CLAUDE.md (shared team configuration)
Directory-level: CLAUDE.md in any directory (scoped to that directory and below)
@import syntax: include external markdown files for modular configuration
.claude/rules/ directory: topic-specific rule files for organized configuration
Anti-Patterns to Avoid
Putting all configuration in one massive CLAUDE.md instead of using modular rules
Not understanding the precedence of user vs project vs directory configs
Claude Code uses a hierarchical configuration system with three layers that merge together. Understanding the precedence and purpose of each layer is essential.
The three configuration layers:
Precedence: More specific configs override more general ones. Directory-level > Project-level > User-level.
Modular configuration with @import and .claude/rules/:
@import ./rules/typescript.md pulls in TypeScript-specific rulespaths glob patternsThis modular approach makes rules easier to find, maintain, and allows different team members to own different rule files.
1~/.claude/2 CLAUDE.md # USER LEVEL (personal, not shared)3 "Use vim keybindings"4 "Prefer dark theme output"56project/7 .claude/8 CLAUDE.md # PROJECT LEVEL (shared via git)9 "Use TypeScript with strict mode"10 "Follow ESLint airbnb config"11 "@import ./rules/api-design.md"12 rules/13 typescript.md # Auto-loaded rule file14 testing.md # Auto-loaded rule file15 api-design.md # Imported by CLAUDE.md16 commands/17 review.md # /review slash command18 deploy.md # /deploy slash command19 skills/20 refactor/21 SKILL.md # Refactoring skill (forked context)2223 src/24 api/25 CLAUDE.md # DIRECTORY LEVEL (scoped rules)26 "All endpoints must validate auth tokens"27 "Use Zod schemas for request validation"# One massive CLAUDE.md with everything mixed # .claude/CLAUDE.md (800 lines) Use TypeScript strict mode. Follow ESLint airbnb. Use vim keybindings. # Personal pref in project config! REST endpoints follow... All functions need tests... API auth validation... # Should be directory-scoped ...800 more lines
# Modular: split by concern and scope # .claude/CLAUDE.md (project) Use TypeScript strict mode. @import ./rules/testing.md # ~/.claude/CLAUDE.md (personal) Use vim keybindings. # src/api/CLAUDE.md (directory-scoped) All endpoints must validate auth tokens. # .claude/rules/testing.md (topic-specific) Every function needs unit tests.
Custom Commands & Skills
Create custom slash commands and skills to extend Claude Code's capabilities for your team.
Commands and skills system:
Custom slash commands: .claude/commands/ directory for team-shareable shortcuts
Skills: .claude/skills/ directory with SKILL.md for complex, reusable behaviors
SKILL.md frontmatter: context: fork, allowed-tools, argument-hint
Path-specific rules: YAML frontmatter with paths glob patterns for targeted configuration
Anti-Patterns to Avoid
Using commands when skills (with forked context) would be more appropriate
Not specifying allowed-tools in skills, leaving overly broad tool access
Claude Code supports two extension mechanisms: custom commands and skills. Understanding when to use each is critical.
Custom Commands (.claude/commands/):
Skills (.claude/skills/):
SKILL.md frontmatter fields:
context: fork — Run in isolated contextallowed-tools — Restrict which tools the skill can use (e.g., [Read, Edit, Grep])argument-hint — Describe the expected argumentWhen to use which:
1---2context: fork3allowed-tools:4 - Read5 - Edit6 - Grep7argument-hint: "file or directory to refactor"8---910# Refactoring Skill1112When asked to refactor code, follow these steps:13141. **Analyze** the current code structure using Read and Grep152. **Identify** patterns that violate SOLID principles163. **Plan** the refactoring approach before making changes174. **Apply** changes incrementally using Edit (never Write)185. **Verify** each change maintains existing behavior1920## Rules21- Never delete existing tests22- Preserve all public API signatures23- Add JSDoc comments to refactored functions# Using a command for complex exploration # .claude/commands/refactor.md Refactor the given code to use dependency injection. Look through all files and restructure the codebase. # Runs in main context, pollutes it with exploration # noise. No tool restrictions.
# Using a skill with forked context # .claude/skills/refactor/SKILL.md --- context: fork # Isolated from main context allowed-tools: # Restricted tool access - Read - Edit - Grep --- Refactor the given code to use DI. # Exploration stays in the fork. Restricted tools.
Plan Mode & Iterative Refinement
Use plan mode for complex tasks and iterative refinement patterns to improve output quality progressively.
Planning and iteration strategies:
Plan mode: think before acting — useful for complex multi-step tasks
Direct execution: appropriate for well-defined, simple tasks
Iterative refinement: concrete examples, TDD iteration, interview pattern
TDD iteration: write tests first, then implement, then refine until tests pass
Anti-Patterns to Avoid
Using plan mode for simple, well-defined tasks (unnecessary overhead)
Skipping planning for complex tasks that need architectural thinking first
Plan mode tells Claude to think and outline an approach before executing. Critical for complex tasks, wasteful for simple ones.
When to use plan mode:
When to use direct execution:
Iterative refinement patterns:
The TDD iteration cycle gives Claude a concrete, verifiable goal at each step:
This dramatically improves output quality compared to "implement this feature."
1# Step 1: Write the test first (defines the goal)2You: "Write a test for getUserById that:3 - Returns user with id, name, email4 - Throws NotFoundError if user doesn't exist5 - Validates that id is a positive integer"67# Step 2: Run the test (should fail)8You: "Run the test"9Claude: "Test fails: getUserById is not defined"1011# Step 3: Implement to pass the test12You: "Implement getUserById to pass all tests"13Claude: [implements the function]1415# Step 4: Run tests again (should pass)16You: "Run the tests"17Claude: "All 3 tests pass"1819# Step 5: Refine the implementation20You: "Add input sanitization and connection pooling,21 keeping all tests green"CI/CD Integration & Batch Processing
Integrate Claude Code into CI/CD pipelines using the -p flag and structured output. Leverage batch processing for cost optimization.
CI/CD and batch processing patterns:
-p flag: run Claude Code in non-interactive mode for CI/CD pipelines
--output-format json: get structured output for automated processing
--json-schema: enforce specific output schemas
Session context isolation in CI: separate generator and reviewer contexts
Message Batches API: 50% cost savings with 24-hour processing window
custom_id: track individual requests in batch processing
Anti-Patterns to Avoid
Using interactive mode in CI/CD pipelines
Same-session self-review (retains reasoning context bias)
Not isolating generator and reviewer sessions in code review pipelines
Claude Code integrates into CI/CD pipelines using the -p flag for non-interactive execution and structured output flags for automated processing.
Key CI/CD flags:
Session isolation for code review:
The generator session (that wrote the code) must be completely separate from the reviewer session. Why? If the reviewer runs in the same session, it retains the generator's reasoning, creating confirmation bias.
Batch processing with the Message Batches API:
1name: Claude Code Review2on: [pull_request]34jobs:5 review:6 runs-on: ubuntu-latest7 steps:8 - uses: actions/checkout@v4910 # Use -p flag for non-interactive mode11 # Use a SEPARATE session from code generation12 - name: Run Claude Code Review13 run: |14 claude -p "Review this PR diff for:15 1. Functions exceeding 50 lines16 2. Missing error handling on async ops17 3. Hardcoded credentials or API keys18 4. Missing unit tests for new functions19 Provide results as structured JSON." \20 --output-format json \21 --json-schema '{22 "type": "object",23 "properties": {24 "issues": {25 "type": "array",26 "items": {27 "type": "object",28 "properties": {29 "file": {"type": "string"},30 "severity": {"type": "string"},31 "description": {"type": "string"}32 }33 }34 }35 }36 }'# Same-session self-review claude -p "Write a new auth module" # Session A claude --resume -p "Review your code" # SAME session! # Reviewer retains reasoning context = confirmation bias
# Separate session for review claude -p "Write a new auth module" # Session A claude -p "Review this diff: ..." # Session B (fresh) # Reviewer has no context from generation
Exam Tips for Domain 3
Know the CLAUDE.md hierarchy: user > project > directory
Understand when to use plan mode vs direct execution
CI/CD uses -p flag with --output-format json for automation
Batch API offers 50% savings — know when to use synchronous vs batch
Related Exam Scenarios
Code Generation with Claude Code
Configure Claude Code for a development team workflow. Tests CLAUDE.md configuration, plan mode, slash commands, and iterative refinement strategies.
Developer Productivity with Claude
Build developer tools using the Claude Agent SDK with built-in tools and MCP servers. Tests tool selection, codebase exploration, and code generation workflows.
Claude Code for CI/CD
Integrate Claude Code into continuous integration and delivery pipelines. Tests -p flag usage, structured output, batch API, and multi-pass code review.
Test Your Knowledge of Claude Code Config
Practice with scenario-based questions covering this domain.