Thumbnail for Agent Instruction Layers in a Multi-Team Monorepo, Step by Step

Agent Instruction Layers in a Multi-Team Monorepo, Step by Step

Published: 2026-12-05

Agent Instruction Layers in a Multi-Team Monorepo, Step by Step

In a monorepo with several teams, I'd give every piece of agent knowledge one place, chosen by two questions: can the agent read it from the code, and when does it need it.

I started in .NET, in a large system that was close to a monolith, and later moved to TypeScript microservices. In both places the question of where to keep knowledge for a coding agent came up as soon as the main instruction file started to grow. In my projects I treat CLAUDE.md as a map that points to other files, which I described in Keep CLAUDE.md Small. Below I go through how this could look in a bigger repository with four teams, using an example platform, together with the parts of this layout that I see as weaker.

The example: a wholesale platform

Companies order goods from a wholesaler through this platform. Four teams share one repository and split it in CODEOWNERS. There are two stacks, TypeScript and Kotlin, and services talk over REST and Kafka events.

AGENTS.md                 the index
CLAUDE.md                 one line: @AGENTS.md
apps/
  storefront/             Next.js, shop for business customers
  admin/                  Angular, back office of the wholesaler
  sales-mobile/           React Native, app for sales reps
services/
  orders/                 TypeScript
  pricing/                Kotlin, the only place that calculates prices
  payments/               Kotlin, payments and invoices
  inventory/  catalog/  notifications/
  billing-legacy/         Java, being phased out
packages/
  shared-kernel/          Money, CustomerId and other shared types
  api-contracts/          OpenAPI and event schemas
  ui-kit/  test-utils/
infra/                    Terraform, Helm, CI
docs/
  domain/                 orders.md, inventory.md, payments.md, pricing.md, glossary.md
  adr/                    001-pricing-in-kotlin.md, 007-money-in-minor-units.md, ...
  incidents/

The layers

Each layer answers the question of when the agent sees a given piece of knowledge. Layer 0 is what the agent sees on its screen when it opens the code, so I don't write it down anywhere.

Layer What it is Where in the repo
0. Screen what the agent reads by itself code, manifests, CI, CODEOWNERS, packages/api-contracts/
1. Index loaded in every session /AGENTS.md, with CLAUDE.md importing it
2. Local files loaded when the agent works in that directory services/*/AGENTS.md, packages/*/AGENTS.md, apps/*/AGENTS.md
3. Domain knowledge terms and rules per context docs/domain/*.md
4. Decisions why something is the way it is docs/adr/
5. History the raw record git, pull requests, review comments, docs/incidents/

I took the idea of layers from Anthropic's article The components of agent memory, where an agent's memory goes from transcripts, through notes and wiki-like pages, up to the context injected at the start of every session.

The point of layer 2 is that the rules of the payments team load only when the agent touches payments. This depends on the tool. Claude Code loads a CLAUDE.md or AGENTS.md from a subdirectory once the agent reads a file in that subdirectory. Codex walks from the project root down to the directory where the session started and stops there, with a default limit of 32 KiB for all files together. So with Codex, a session started in the repository root sees the index, and the file in services/payments/ only when the session starts in that directory. That's one of the weaker points of this layout: the same repository gives the agent different knowledge depending on the tool and on where you start it.

Step 1: decide what stays on the screen

Before writing anything for the agent, I'd check whether a rule can be expressed in code, a type, a linter or CI. A rule there works for every tool and every session, and when the code changes, the build shows whether the rule still holds.

In the example, services don't import each other's code. They talk through packages/api-contracts/, REST and events. I'd put this rule in CI, so that it's checked on every pull request. For the TypeScript services it could be dependency-cruiser:

// .dependency-cruiser.cjs
module.exports = {
  forbidden: [
    {
      name: "no-imports-between-services",
      comment: "Services talk through packages/api-contracts, REST and events",
      severity: "error",
      from: { path: "^services/([^/]+)/" },
      to: { path: "^services/", pathNot: "^services/$1/" },
    },
  ],
};

The same goes for billing-legacy. Marking its public classes as @Deprecated puts the information right where the agent reads the code. A simple interface helps in the same way: if pricing is the only module that calculates prices and exposes one small API for it, there's less to explain in any file.

The weaker side: Kotlin services need a different tool for the same rule, and not every rule can be checked automatically. What remains goes to the layers below.

Step 2: keep the root index short

The index is read in every session, so I'd put there only what applies everywhere and what the agent can't see in the code:

# Wholesale platform

Platform where companies order goods from a wholesaler. Four teams, see CODEOWNERS.

## Where to look
- Domain terms and rules: `docs/domain/` (start with `docs/domain/glossary.md`)
- Why things are the way they are: `docs/adr/`
- Each service and package can have its own AGENTS.md with local rules

## Rules that apply everywhere
- Prices are calculated only in `services/pricing/`.
- Amounts always go through Money from `packages/shared-kernel/`.
- A change to an event schema is a new version of the contract.

## Ask before changing
`services/payments/`, `services/pricing/`, `packages/shared-kernel/`

## When documentation and code disagree
When a repository's documentation and its existing code disagree, follow the conventions
used in the existing code, and in your final reply point out each inconsistency you found
between the documentation and the code.

CLAUDE.md next to it has one line, @AGENTS.md, so tools that read either file get the same content. Grafana does exactly this: its root CLAUDE.md contains only @AGENTS.md. The last section of the index is the one-sentence fix from the Xu and Wu preprint, which I described in the post about instruction debt.

There's no directory tree, stack or endpoint list in the index, because the agent reads them from the repository. The ETH Zurich paper Evaluating AGENTS.md found that "repository overviews, although popular and recommended by model providers, are not helpful".

Big open source monorepos make a different choice here. The root AGENTS.md of Apache Airflow has about 25 KB and includes sections on commands and repository structure, next to architecture boundaries such as "Scheduler reads serialized Dags — never runs user code". Grafana's root file has a build and test section too. In a project with many external contributors, commands that aren't obvious from the code probably earn their place. So I'd treat a short index as a starting point and add commands when the agent keeps getting them wrong.

Step 3: local files for team and module rules

Local files hold rules that matter only in one area. Two examples:

# services/pricing/AGENTS.md

This service is the only source of prices and discounts. Other services ask it over REST.
Contract price from the customer's agreement wins over volume discounts. They don't add up.
Why pricing is in Kotlin: `docs/adr/001-pricing-in-kotlin.md`. Don't move logic out of it.
# apps/sales-mobile/AGENTS.md

Amounts here are still decimals. The rest of the platform uses Money in minor units
(`docs/adr/007-money-in-minor-units.md`). Convert at the API boundary.
Temporary note: remove it when the migration ticket MOB-142 is done.

The second note is temporary on purpose. It describes a state of the code that should disappear, so it names the ticket after which it can be deleted.

Both big repositories I checked go this way. At the commits I looked at, Airflow has 15 AGENTS.md files, for example in providers/ and in the UI. Grafana has 15, for example in packages/grafana-ui/ and in several features of the frontend.

The trade-off is that every local file is another place that can go out of date. A cross-cutting change, like the Money migration, touches packages/shared-kernel/ and apps/sales-mobile/, so the knowledge sits in two files and both need an update.

Step 4: domain knowledge and decisions outside the instruction files

Domain terms and the reasons behind decisions are needed less often than local rules and are longer, so they live in docs/domain/ and docs/adr/, and the index only points to them.

The word "order" is a good example. In orders it's a customer's order, in inventory it's a purchase order to a supplier, and in payments it's what gets invoiced. The glossary keeps the three meanings side by side, and each service's domain file uses one of them. An ADR explains why pricing is in Kotlin, so that the agent doesn't "simplify" a decision someone made on purpose.

The weaker side is that the agent opens docs/domain/orders.md when it decides to, and in a short task it may never do it. For a rule that must hold every time, I'd use the screen or a local file.

Step 5: keep the layers true in the same pull request

The more files, the more places for the debt I wrote about last week. The main mechanism I'd use is simple. A change in a service comes with an update of its documentation and its agent files in the same pull request, and the reviewer checks it. The team that owns the module in CODEOWNERS also owns its AGENTS.md:

/services/payments/          @wholesale/payments-team
/services/payments/AGENTS.md @wholesale/payments-team
/AGENTS.md                   @wholesale/platform-team

The first line already covers the file, so the second one is only there to make the ownership visible.

In a monorepo I'd add one cheap check to CI: every path mentioned in an agent file has to exist. After a service is renamed or removed, the build fails and points to the note that still mentions it.

// scripts/check-agent-files.mjs
// Fails CI when a path in backticks inside AGENTS.md or CLAUDE.md does not exist.
import { readFileSync, existsSync } from "node:fs";
import { execSync } from "node:child_process";
import { dirname, join } from "node:path";

const files = execSync("git ls-files '*AGENTS.md' '*CLAUDE.md'", { encoding: "utf8" })
  .split("\n")
  .filter(Boolean);

let missing = 0;
for (const file of files) {
  const text = readFileSync(file, "utf8");
  for (const [, ref] of text.matchAll(/`([\w.-]+\/[\w./-]*)`/g)) {
    if (!existsSync(ref) && !existsSync(join(dirname(file), ref))) {
      console.error(`${file}: ${ref} does not exist`);
      missing++;
    }
  }
}
process.exit(missing ? 1 : 0);

The files above write every path in backticks, which is what the script looks for. It catches a renamed directory and misses a rule whose content became wrong. That part stays with the reviewer.

What I'd take from this

For each piece of knowledge I'd first try to put it on the screen: code, a type, a linter, CI. What can't go there gets the narrowest layer that is still loaded when the agent needs it, and an owner who updates it together with the code.

Back to Blog