Monorepo Setup: Large Codebases with Nested CLAUDE.md
A monorepo (one Git repo, many packages) amplifies Claude Code's complexity. A root CLAUDE.md controls global behavior, but each package (ui/, api/, core/) can have its own CLAUDE.md with package-specific skills, permissions, and context rules. This hierarchy lets you enforce consistency while giving teams autonomy.
The challenge: context overload. With 1M+ lines across 20 packages, indexing everything exhausts Claude's context window. The solution: sparse worktrees, package-scoped CLAUDE.md files, and path-gated rules that limit tool access to package boundaries. We'll show the 3-repo example: root rules, package-level rules, and how permissions compose.
Multiple CLAUDE.md Files (Root + Per-Package)
A monorepo can have: 1. Root CLAUDE.md: global rules (all agents must follow). 2. Package CLAUDE.md: local overrides (this package's rules).
When Claude Code starts in a package directory, it reads the local CLAUDE.md first. If a rule is missing, it inherits from the root. This lets the root enforce guardrails (no destructive operations, no credentials in code) while packages define their own tools and skills.
Example: root forbids rm -rf globally. But packages/build/ has a CLAUDE.md that permits npm run clean (which safely removes dist/). The package rule refines the root rule; it doesn't override the safety intent.
Quick reference
- Root .claude/CLAUDE.md: global rules for all packages.
- Each package can have packages/{name}/.claude/CLAUDE.md.
- Hierarchy: root rules + package overrides.
- Config merge: package-specific settings override root defaults.
- Path-gated rules: rules apply only to this package's paths.
Remember this
Root CLAUDE.md sets global rules; package CLAUDE.md refines them. Inheritance + override model.
Per-Package Skills and Scoped Permissions
A skill is a tool or routine that an agent can invoke. Skills can be scoped to a package. Example: only agents in packages/db/ can run database migrations. Agents in packages/ui/ can't.
Path-gating implements this: a rule has a paths field that restricts where it applies. An agent running in packages/ui/ sees skills defined globally + skills in packages/ui/.claude/CLAUDE.md, but not skills from packages/db/.claude/CLAUDE.md.
Quick reference
- Skills defined in root CLAUDE.md are global.
- Skills in package CLAUDE.md are local to that package.
- Path-gated tool: 'allow shell only in packages/api/src'.
- Inheritance: agent in api/ sees root skills + api skills.
- Isolation: ui/ agent cannot invoke db-migration skill.
Remember this
Skills are global or path-gated. Agents inherit skills for their package. Isolation prevents cross-package accidents.
Sparse Worktrees for Focused Work
A monorepo has 1M+ lines. Claude Code doesn't need to index all of it to work on one package. Sparse worktrees check out only the files you need, reducing disk I/O and indexing time.
The pattern: define .worktreeinclude that lists which paths to include (e.g., packages/ui/, packages/shared/). When you create a worktree with --sparse, only those paths are downloaded. Claude Code indexes only what's needed. Time to ready: 2s (sparse) vs. 20s (full).
Quick reference
- Root repo has .worktreeinclude listing paths.
- Format: one path per line. Wildcards allowed.
- Create worktree: git worktree add --sparse
- Sparse worktree sees only included paths.
- Note: subagent inherits sparse rules from parent worktree.
Remember this
Sparse worktrees check out only needed paths. Faster indexing, reduced context. Define .worktreeinclude.
Code Intelligence and Indexing
Claude Code builds an index of symbols (functions, classes, types) to enable fast navigation and dependency discovery. In a monorepo, the index can balloon. With path-gating and sparse worktrees, you control index scope.
Best practice: index only the current package. The index includes: exported types from shared/, internal implementation of the current package, and test files. It excludes other packages' internals. This keeps the index compact and focused.
Quick reference
- Claude Code builds symbol index on startup.
- Sparse checkout reduces index size (fewer files).
- Path-gated index: include current package + shared, exclude others.
- Symbol search (Cmd+K): searches indexed symbols.
- Reindex: if you add new files, reindex manually or on file change.
Remember this
Index scope = current package + shared. Sparse checkout keeps it manageable. Reindex after major changes.
Avoiding Context Overload
Claude Code reads files on demand. But if you ask an agent to 'understand the entire repo', it will try to read everything, consuming tokens and context. The boundary: be explicit about scope.
Instead of 'implement feature X', say 'in packages/ui/, implement feature X'. This scopes the agent to one package. If it needs types from shared/, the package's tsconfig.json guides the import; the agent doesn't need to read other packages.
For true monorepo understanding (which packages import which), use a separate analysis pass: a subagent that maps the dependency graph, returns a report, and other agents use that report as input.
Quick reference
- Be explicit: 'in packages/ui/, implement X' not 'implement X'.
- Let tsconfig.json and package.json guide imports.
- Use path-gated CLAUDE.md to prevent reading other packages.
- For cross-package analysis: dedicated subagent → dependency report → use report as input.
- Token budget: cap file reads per agent to avoid runaway consumption.
Remember this
Scope explicitly. Use package boundaries. Separate analysis from implementation. Cap file reads per agent.
Real Example: 3-Repo Monorepo Structure
Three repos: frontend (Next.js, 200k LOC), backend (Go, 300k LOC), shared (TypeScript types, 50k LOC). Organized as a monorepo: root CLAUDE.md sets global rules, each package has its own.
Scenario: implement a new API endpoint (backend) + UI page (frontend) + shared types (types). Traditionally: 3 developers, 3 PRs, coordinate types. With Claude Code: 1 agent orchestrates, spawns 3 subagents (one per package), passes types through state, validates, merges.
Time: 30 min (instead of 2 hours of coordination). Cost: ~$1 in tokens.
Quick reference
- Root repo/CLAUDE.md: defines shared rules (no secrets, tests required).
- Root repo/.worktreeinclude: lists packages to include.
- packages/frontend/.claude/CLAUDE.md: Next.js skills, no Go tools.
- packages/backend/.claude/CLAUDE.md: Go tools, database access, no UI.
- packages/shared/.claude/CLAUDE.md: type generation, exports.
Remember this
Hierarchy: global + package-level rules. Parallel work on features. Orchestrate with state passing.
Scaling to 1M+ LOC
At 1M+ lines, the monorepo is too large for one agent to comprehend. Instead, decompose into agents:
1. Indexer agent: maps repo structure, exports (packages, types, public APIs). 2. Planner agent: reads indexer output, breaks down the feature into subtasks. 3. Worker agents: each handles one subtask (e.g., implement API, add UI, add types). 4. Validator agent: checks correctness (tests pass, types valid, no conflicts). 5. Merger agent: coordinates merges, resolves conflicts.
This pipeline is more reliable and scalable than a single monolithic agent. Each agent is specialized, smaller context, easier to debug.
Quick reference
- Indexer: analyzes repo structure, exports as JSON.
- Planner: reads index, generates task list.
- Workers: parallel, one task per worker, scoped to package.
- Validator: runs tests, type checks, integration tests.
- Merger: merges all branches, handles conflicts.
Remember this
Scale with decomposition: indexer → planner → workers (parallel) → validator → merger.
Key takeaway
Monorepos scale Claude Code via hierarchy: root CLAUDE.md + package overrides, sparse worktrees, path-gated permissions, and decomposed agents. For orchestrating multi-agent workflows, read Dynamic Workflows. For managing hundreds of sessions, see Agent View. For agent fundamentals, see Agent SDK Quickstart.
Polo Khan
Lead Author & Systems ArchitectSoftware engineer and distributed systems architect specializing in backend scalability, cloud-native infrastructure, databases, and AI engineering workflows. Author and maintainer of Core Concept Learning.
Related Articles
Explore this topic