We had the web app in one repository, the API in a second and the shared validation rules copied into both. When we asked an AI coding agent to add a field to the checkout form, it edited the frontend, guessed the request shape and shipped a payload the API rejected with a 422. The agent was not careless. It simply could not see the other half of the contract, so it produced the most plausible guess.

Moving to a monorepo fixed most of that, but only after we changed the layout twice. This post is the layout we ended up with: what goes where, how the shared types are produced, where package-level instructions live, and which tools stop the agent (and us) from crossing boundaries that should not be crossed. Our earlier post on repository setup for coding agents covers commands and onboarding docs; this one is only about structure.

Why layout matters more for an agent than for a person

A person who joins the team asks a colleague where the order schema lives. An agent searches. Whatever it finds first, it treats as truth. That gives three practical rules:

  • One source per contract. If the order type exists in two places, the agent will find the stale one half the time.

  • Predictable paths. packages/<name>/src everywhere beats a mix of lib/, src/, common/ and shared/.

  • Machine-checked boundaries. Instructions in prose are advisory. A lint error in CI is not.

The layout

repo/
  apps/
    web/                 # Next.js storefront
    admin/               # internal dashboard
  services/
    orders-api/          # Go service
    billing-worker/      # Go worker
  packages/
    contracts/           # OpenAPI + generated TS types (single source)
    ui/                  # shared React components
    config-eslint/       # shared lint config
  go/
    pkg/auth/            # shared Go module
  tools/
    rules/               # project rule scripts run in pre-commit + CI
  CLAUDE.md              # repo-wide rules, short
  pnpm-workspace.yaml
  turbo.json
  go.work

Apps are deployable frontends, services are deployable backends, packages are libraries that never deploy on their own. An agent asked to "add a field to orders" now has an obvious search order: packages/contracts, then the service, then the app.

JavaScript workspaces with pnpm

pnpm reads the workspace list from pnpm-workspace.yaml:

packages:
  - "apps/*"
  - "packages/*"

Internal dependencies use the workspace: protocol so a package always resolves to the local copy, never to a registry version with the same name:

{
  "name": "@acme/web",
  "dependencies": {
    "@acme/contracts": "workspace:*",
    "@acme/ui": "workspace:*"
  }
}

This matters for agents more than it looks. Without workspace:*, a typo in a version range can make the install pull an old published copy, and the agent then reads type definitions from node_modules that no longer match the source next to it.

Task orchestration: Turborepo or Nx

Either tool gives you one command that builds, tests and lints only what changed. Turborepo 2 uses a tasks key in turbo.json:

{
  "$schema": "https://turborepo.com/schema.json",
  "tasks": {
    "build": { "dependsOn": ["^build"], "outputs": ["dist/**", ".next/**"] },
    "typecheck": { "dependsOn": ["^build"] },
    "test": { "dependsOn": ["^build"] },
    "lint": {}
  }
}

^build means "build my dependencies first", so turbo run typecheck --filter=@acme/web... checks the web app against freshly built contracts. We put that exact command in the app's instructions file, so the agent runs the check that proves its change against the real contract rather than a full-repo test run that takes twelve minutes.

Nx does the same through its project graph and adds something Turborepo does not ship: module boundary rules (covered below). Pick one; running both just doubles the configuration.

Go workspaces

Go services usually live as separate modules so each can pin its own dependencies. A go.work file at the root lets the toolchain and editors see them together:

go work init ./services/orders-api ./services/billing-worker ./go/pkg/auth
go work use -r ./services
go 1.23

use (
    ./go/pkg/auth
    ./services/billing-worker
    ./services/orders-api
)

With the workspace, go build ./... from a service resolves go/pkg/auth to the local directory, so an agent that changes the auth package sees compile errors in every service that uses it in the same session. Decide as a team whether go.work is committed. We commit it, and CI builds each module with GOWORK=off as well, to prove every go.mod is complete on its own.

Shared types: generate, do not copy

The contract lives once, in packages/contracts/openapi.yaml. TypeScript types are generated from it and committed, and the Go service implements the same spec:

{
  "name": "@acme/contracts",
  "scripts": {
    "generate": "openapi-typescript openapi.yaml -o src/schema.d.ts",
    "check": "openapi-typescript openapi.yaml -o /tmp/schema.d.ts && diff -q /tmp/schema.d.ts src/schema.d.ts"
  }
}

The check script runs in CI and fails if someone (or something) edited the generated file by hand. Agents are especially prone to "fixing" a type error by changing the generated file, because that is where the error points. A header comment in the generated file helps, but the diff check is what actually stops it.

If you use protobuf or a schema language instead of OpenAPI, the pattern is identical: one source file, generated outputs, a CI check that the outputs are up to date.

Per-package instructions

Claude Code reads CLAUDE.md from the directory you start it in and its parents at launch, and pulls in CLAUDE.md files from subdirectories when it reads files in them. That lets each package carry its own rules without bloating the root file that loads every session.

Root CLAUDE.md, kept short:

# Repo rules
- Contracts live in packages/contracts. Never edit src/schema.d.ts by hand; run pnpm --filter @acme/contracts generate.
- Apps may import packages. Packages never import apps. Services never import JS packages.
- Before finishing: turbo run lint typecheck test --filter=...[origin/main]

services/orders-api/CLAUDE.md:

# orders-api
- Handlers in internal/http only parse, validate and call internal/orders.
- SQL lives in internal/store. Migrations: make migrate-new name=<slug>.
- Test: go test ./... from this directory.

Keep each file to the rules that are specific to that package. Anything that applies everywhere belongs in the root file, and anything that can be enforced by a tool belongs in the tool.

Boundaries enforced by tooling

This is the part that makes the layout hold. With Nx, tag projects and let the lint rule enforce the dependency direction:

{
  "rules": {
    "@nx/enforce-module-boundaries": ["error", {
      "depConstraints": [
        { "sourceTag": "type:app", "onlyDependOnLibsWithTags": ["type:ui", "type:contracts"] },
        { "sourceTag": "type:ui", "onlyDependOnLibsWithTags": ["type:ui", "type:contracts"] },
        { "sourceTag": "type:contracts", "onlyDependOnLibsWithTags": [] }
      ]
    }]
  }
}

Without Nx, dependency-cruiser does the same for any JavaScript repository with a rule such as "no file under packages/ may import from apps/". In Go, the internal/ directory rule is enforced by the compiler: code under services/orders-api/internal/ cannot be imported by billing-worker, full stop. Use it for anything that is not meant to be shared.

Add CODEOWNERS on packages/contracts/ so contract changes always get a human review from the owning team, even when the rest of the pull request is routine.

Keeping the context useful

A monorepo can also drown an agent. Generated bundles, lockfiles and vendored code are large and rarely relevant. Three habits help:

  • Keep build output in ignored directories (dist/, .next/, .turbo/) so search tools that respect .gitignore skip them.

  • Add deny rules in .claude/settings.json for paths the agent should never read, such as fixtures with production-shaped data.

  • Start the agent from the package directory when the task is local to one package; it still sees the root rules through the parent CLAUDE.md.

Common mistakes

Mistake

Why it hurts

Do this instead

Copying types into each app

Agent edits one copy, the others drift

Generate from one contract, check in CI

Huge root CLAUDE.md

Every session pays for rules it does not need

Root rules short, package rules local

Boundaries only in docs

Prose is ignored under pressure

Nx tags, dependency-cruiser, Go internal/

Version ranges for internal packages

Install may resolve a stale registry copy

workspace:* everywhere

Full-repo test as the only check

Slow, so it gets skipped

Filtered turbo run or nx affected per package

Key takeaways

  • An agent treats whatever it finds first as truth, so keep one source per contract.

  • Use a predictable apps/, services/, packages/ split and the same internal layout in each.

  • Generate shared types and fail CI when generated files are hand-edited.

  • Put package-specific rules in package-level CLAUDE.md files and keep the root file short.

  • Enforce dependency direction with tooling, not with paragraphs.

References