promptgarten ๐ŸŒฑ
๐ŸŒ ES
Guideโ—โ—โ—5 min ยท +40 XP

Running AI Agents in Large Monorepos

In a monorepo, an AI agent easily loses focus and drifts into the wrong subproject โ€” the right CLAUDE.md structure keeps it on track.

The problem with big repos

A monorepo often holds many subprojects: a backend, a frontend, shared libraries. One single CLAUDE.md for everything either grows huge (eating up context on every session, even for tasks that don't need it) or stays so generic it barely helps.

The fix: multiple CLAUDE.md files

Instead of one file for everything, give each subproject its own CLAUDE.md. A file at the repo root holds rules that apply everywhere (commit style, overall layout). Each subproject gets its own file with its specific commands and conventions.

How loading actually works

Start the agent at the repo root, and it loads only the root CLAUDE.md at launch; files in subfolders load only once the agent actually reads files there. Start it directly inside a subproject, and it gets that folder's file plus every parent one โ€” but by default only file access to that subtree.

Forcing focus

For tasks that clearly belong to one subproject, it pays to start the agent right there instead of at the root. That keeps context and file access narrow automatically, without you having to explain on every task which parts of the repo are irrelevant right now.

EXAMPLE

A monorepo with packages/api, packages/web, and packages/shared gets a root CLAUDE.md ('Three packages: api (Node/Express), web (React/Vite), shared (TS utils). Always run commands from the relevant package folder.') plus its own packages/api/CLAUDE.md with API-specific test commands. Start the agent directly inside packages/api/, and it sees both files โ€” but nothing from packages/web/.

QUICK QUIZ

By default, what happens when you start Claude Code directly inside a monorepo subfolder (e.g. packages/api/) instead of the repo root?

SOURCES

RELATED TOPICS

CLAUDE.md / AGENTS.md: Give Your Agent Memory & Rules โ—โ—โ—‹What Are Subagents? โ—โ—โ—‹Git worktrees for parallel AI agents โ—โ—โ—‹Context strategies for agents โ—โ—โ—