20%

3.1 CLAUDE.md Hierarchy, Scoping, and Modular Organisation

CLAUDE.md คืออะไร

CLAUDE.md คือไฟล์ instructions ที่ Claude Code อ่านอัตโนมัติเมื่อเริ่ม session — เป็นวิธีบอก Claude ว่า project นี้มี conventions อะไร, ห้ามทำอะไร, ควรทำอะไร โดยไม่ต้องพิมพ์ซ้ำทุกครั้ง เหมือน README สำหรับ AI assistant

Hierarchy (ลำดับชั้น)

CLAUDE.md มี 3 ระดับ — Claude อ่านทุกระดับที่เจอ:

  1. Global~/.claude/CLAUDE.md — ใช้ทุก project
  2. Project Root./CLAUDE.md — ใช้เฉพาะ project นี้
  3. Subdirectory./src/CLAUDE.md — ใช้เฉพาะเมื่อทำงานกับไฟล์ใน directory นั้น
~/.claude/CLAUDE.md          ← Global (ทุก project)
├── myproject/CLAUDE.md      ← Project root
│   ├── src/CLAUDE.md        ← Subdirectory
│   ├── tests/CLAUDE.md      ← Subdirectory
│   └── docs/CLAUDE.md       ← Subdirectory

Inheritance Rules

  • ทุกระดับ merge กัน (ไม่ override)
  • Subdirectory CLAUDE.md เป็น additive — เพิ่มกฎเฉพาะ directory นั้น
  • ถ้ามี conflict → specific (subdirectory) wins over general (global)

Modular Organization with @-imports

ใช้ @filename เพื่อ import CLAUDE.md ย่อยเข้ามา:

# CLAUDE.md (project root)
@conventions/coding-style.md
@conventions/testing.md
@conventions/git.md
# conventions/coding-style.md
- Use TypeScript strict mode
- Prefer functional style
- No classes unless necessary

Best Practices for Large Projects

# CLAUDE.md (root)

## Architecture
- Monorepo with apps/ and packages/
- Each app is a Next.js app
- Shared code in packages/

## Commands
- `pnpm dev` — start dev server
- `pnpm test` — run all tests
- `pnpm lint` — lint all packages

@apps/web/CLAUDE.md
@apps/api/CLAUDE.md
@packages/shared/CLAUDE.md

Key Concepts

  • Auto-loaded — Claude Code อ่าน CLAUDE.md โดยอัตโนมัติ ไม่ต้อง configure
  • Context-aware — Subdirectory CLAUDE.md load เฉพาะเมื่อ Claude ทำงานกับไฟล์ใน directory นั้น
  • Merge semantics — ทุก level merge กัน ไม่ override
  • Token cost — CLAUDE.md ทุกไฟล์กิน tokens ใน context window

Exam Tips

  • จำ 3 levels: Global > Project > Subdirectory
  • CLAUDE.md เป็น additive/merge ไม่ใช่ override
  • ข้อสอบอาจถามว่า CLAUDE.md ไหน apply เมื่อไหร่ — subdirectory apply เฉพาะเมื่อ Claude ทำงานกับไฟล์ใน dir นั้น
  • @-imports ช่วยจัด modular ไม่ต้องใส่ทุกอย่างในไฟล์เดียว
  • อย่าใส่ข้อมูลเยอะเกินใน CLAUDE.md — กิน context window ทุก turn