Skip to content

Coding agents in a monorepo: keep the context focused on the package at hand

In a large repository an agent can wander. Scope the task to a package, keep rules close to the code, and give each area its own commands.

Published by Contextingo.

A monorepo holds many packages with different rules. An agent that is told “fix the bug” may read a lot of unrelated code and apply the wrong conventions.

Narrow the task

  • Name the package and the folder in the first message.
  • Start the session in the package folder when the tool allows.
  • List the paths that are out of scope.

Rules close to the code

Keep root instructions for what applies everywhere. Put package-specific commands and conventions in that package. Some tools read instruction files in subfolders when they work there; check how yours behaves before you rely on it.

Give each package its commands

# at the root
npm test -w @acme/billing
npm run typecheck -w @acme/billing

A command that runs everything is slow and noisy. A command for one package gives a fast, clear result.

Share what crosses packages

Decisions that span packages, such as an API contract, are easy to lose. Record them once against the issue, so the agent that works on the other package finds them. See decision records for agent teams.

Questions

Good to know.

Should every package have its own instruction file?

Only if its rules differ from the root. Duplicated rules drift.

How do I stop the agent editing other packages?

State the scope in the task, and review the file list before the diff.

Nothing learned is lost. Nothing unchecked is done.