Updated Nov 25, 20269 min read

Microservices vs Monolith for AI Coding Agents

Which architecture lets a coding agent read less, run everything and break fewer contracts: monolith, modular monolith, microservices in a monorepo, microservices across repositories, or services in one Encore app.

Illustration for Microservices vs Monolith for AI Coding Agents

For AI coding agents, start with a monolith or modular monolith, and use services only when they share one repository, call each other through typed clients and run together locally. Encore is built around that setup.

Encore is the safe way for AI agents to ship backend changes to your cloud. Services with typed calls live in one app, encore run starts them all with their infrastructure, and each environment deploys them as one process or as separate services.

Which architecture should you pick for AI coding agents?

RankArchitectureWhat the agent can read and runBest fit
1EncoreEvery service in one app with typed calls, all started locally by encore run with their databases and topicsTeams that want service boundaries without losing local runs, traces and a reviewed path to production
2Modular monolithOne process with module boundaries checked in CIGrowing apps where teams own areas of the code
3MonolithOne codebase and one processNew and mid-sized apps
4Microservices in a monorepoAll services in one repository, usually started with Docker ComposeTeams that need independent deploys and accept maintaining a Compose file
5Microservices across repositoriesOne service per repository, the rest only through their APIsLarge organizations with a team per service

Verdict: an agent does its best work when it can read the code it changes, get a type error when it breaks a contract and run the whole system before anything ships. A plain or modular monolith gives you that for free at small and medium size. Encore keeps it once you have services, and it adds per-pull-request previews and infrastructure approval on the way to your own cloud. Separate repositories give agents the least to work with.

Compare the architectures in detail
CapabilityEncoreModular monolithMonolithMicroservices in a monorepoMicroservices across repositories
Code the agent can searchThe whole appThe whole appThe whole appAll servicesOne service
Boundary contractsTyped service clients generated from the API definitionsModule interfaces, enforced by a linterFunction callsHTTP or gRPC, typed if you generate clientsHTTP or gRPC, often documented separately
Run the whole system locallyencore run starts every service and its infrastructureOne processOne processA Compose file kept in sync by handUsually not possible
Cross-service tracing locallyBuilt inNot neededNot neededRequires OpenTelemetry setupRequires a shared environment
Deploy shapeOne process or separate processes, chosen per environmentOne deployOne deployIndependent deploysIndependent deploys
Agent instructions per areaPer-service CLAUDE.md or AGENTS.md filesPer-module filesOne file, split by directory as it growsPer-service filesPer-repository files

Why does the architecture matter more when an agent writes the code?

A coding agent reads the files it thinks are relevant, edits them, runs a check and reads the result, all inside a context window of fixed size. Anthropic's engineering team calls context a finite resource with diminishing returns, and Chroma's context rot report found that models get less reliable as input grows, even on simple tasks.

Architecture decides how much the agent has to read for a change and whether it can run what it changed. The general trade-offs are in monolith vs microservices. For agents, four questions decide most of it:

  • How much code the agent has to read to make a typical change.
  • Whether boundaries are typed, so a broken contract shows up as a compile or type error.
  • Whether all the code sits in one repository the agent can search.
  • Whether the agent can run the whole system, including databases and queues, on one machine.

How does Encore give agents service boundaries they can run locally?

Boundaries (Typed calls between them) built with the Encore SDK, deployed by the Encore Platform into AWS or GCP: One deploy per push

An Encore app is one repository where every directory with a service definition is a service. Each service declares its own databases, Pub/Sub topics and buckets in code, and calls between services look like function calls that Encore turns into network calls. When an agent changes the orders service and the billing service in one task, it can start both with encore run, call the endpoint and watch the request cross the boundary.

In TypeScript, a service is a directory with an encore.service.ts file:

// billing/encore.service.ts import { Service } from "encore.dev/service"; export default new Service("billing");

The billing endpoint has typed request and response shapes, and that type is the contract other services compile against:

// billing/billing.ts import { api } from "encore.dev/api"; interface ChargeRequest { orderId: string; amountCents: number; } interface ChargeResponse { chargeId: string; } export const charge = api( { method: "POST", path: "/billing/charge" }, async (req: ChargeRequest): Promise<ChargeResponse> => { return { chargeId: `ch_${req.orderId}` }; }, );

The orders service calls it through the generated client in ~encore/clients. If the agent renames a field in ChargeRequest, the orders service stops type-checking, so the mistake surfaces in the editor and not as a failed request in staging:

// orders/orders.ts import { api } from "encore.dev/api"; import { billing } from "~encore/clients"; export const checkout = api( { expose: true, method: "POST", path: "/orders/:id/checkout" }, async ({ id }: { id: string }): Promise<{ chargeId: string }> => { const { chargeId } = await billing.charge({ orderId: id, amountCents: 4200 }); return { chargeId }; }, );

Encore learns every service and every call between them from static analysis. The local development dashboard shows a service catalog and distributed traces, and the local MCP server hands the same metadata and traces to the agent, so it can check which services a request touched.

Whether that ships as one process or many is an environment setting. Each environment runs the services either in a single process, which is simpler and cheaper, or as separate processes that scale on their own, and you can pick differently for production and development without changing code. The same page lets you require that infrastructure changes are approved before a deploy goes ahead, which is a useful brake when an agent adds a database or a topic. Each pull request also gets a preview environment hosted by Encore, so the change runs on real infrastructure before review.

The Infrastructure page in the Encore dashboard with a database's configuration panel open, showing fields for vCPUs, memory, storage, availability type, max connections and backups.
Each resource Encore provisions in your own AWS or GCP account can be reviewed and configured per environment.

Moving from a monolith to services in production is a path Encore customers have taken, for example Bookshop.org's move from Rails monolith to Go microservices with Encore.

Key features

  • One repository, one service per directory, infrastructure declared per service.
  • Typed service-to-service calls checked at compile time.
  • encore run starts every service locally with its databases, topics and buckets.
  • Traces across services locally and in every environment, readable by the agent through MCP.
  • Single-process or separate-process deploys per environment, plus optional infrastructure approval.

Good to know

Splitting a service means moving code into a new directory with its own service definition, and the typed clients point out every call site that breaks. Service clients cannot be passed around freely, because Encore finds the calls through static analysis. The .ref() method on a client covers those cases.

Try Encore →

When is a monolith the best choice for an agent?

One deploy: app (One process), db (Postgres)

For a new or mid-sized app, a single codebase running as one process is the easiest place for an agent to work. Every caller of a function is one search away, and one build checks a refactor.

It gets harder as the codebase grows: the agent has more to search, and unrelated code competes for its context. Claude Code's documentation notes that specific and concise instructions are followed more consistently, and CLAUDE.md files in subdirectories load only when the agent works there, so splitting instructions per directory buys a lot of headroom.

When does a modular monolith help an agent?

One deploy: orders (Module), billing (Module), users (Module), db (Postgres)

A modular monolith keeps one process and adds boundaries the build checks. The agent gets smaller areas to reason about, with no network in between. In Python, import-linter turns a boundary into a contract that fails CI when one module reaches into another's internals:

[importlinter] root_package = shop [importlinter:contract:billing-boundary] name = Orders only uses the billing public API type = forbidden source_modules = shop.orders forbidden_modules = shop.billing.internal

Good to know

The boundaries only hold if something checks them, so put the contract file and the command that runs it in AGENTS.md. Microservices vs modular monolith covers how to keep the boundaries from eroding.

When do microservices in a monorepo work for agents?

One repo: orders (Service), events (Pub/Sub), billing (Service), users (Service)

With every service in one repository, the agent can search the whole system, and a Docker Compose file starts the services and their dependencies together:

# compose.yaml services: orders: build: ./orders environment: BILLING_URL: http://billing:8080 DATABASE_URL: postgres://postgres:postgres@orders-db:5432/orders depends_on: [billing, orders-db] billing: build: ./billing orders-db: image: postgres:17 environment: POSTGRES_PASSWORD: postgres POSTGRES_DB: orders

Good to know

The Compose file is a second description of the system, and it drifts from the code. Every new service or database means editing it, and the HTTP contract between services is only typed if the team generates clients from a schema. Tracing a request across services locally needs OpenTelemetry wired into each one.

Why are microservices across repositories hardest for agents?

One repository per service gives teams full independence, and it is the hardest setup for an agent. The other services are visible only through their API documentation, a contract change means coordinated pull requests in several repositories, and running the full system usually means deploying to a shared environment.

Claude Code can run subagents, each in its own context window, which helps split the work. Each one still needs access to the code it changes and a way to run it.

How do you choose an architecture for AI coding agents?

  • Encore when you want service boundaries with typed calls, all services in one repository, the whole system running locally, and deploys to your own AWS or GCP account as one process or many.
  • A modular monolith when teams own areas of the code and the boundaries need checking in CI.
  • A monolith for most new and mid-sized apps, with per-directory agent instructions as it grows.
  • Microservices in a monorepo when services must deploy independently and the team accepts maintaining a Compose file.
  • Microservices across repositories only when organizational boundaries require it, accepting that agents will work on one service at a time.

Frequently asked questions

Should I use microservices or a monolith when building with AI coding agents?

Start with a monolith or a modular monolith. The agent can search all the code, a refactor is checked by one build, and the app runs with one command. Move to services when parts of the system need to deploy or scale on their own, and keep them in one repository with typed calls and a way to run all of them locally, so the agent can still test a change that crosses a service boundary.

Can I start with a monolith and split it into microservices later?

Yes. Keep the modules behind small typed interfaces from the start and enforce the boundaries in CI, so the code that moves into a service already talks to the rest through one interface. Extract one module at a time, keep it in the same repository, and replace the in-process calls with typed client calls.

Do AI coding agents struggle with large codebases?

Yes. Past a certain size, the agent has more to read than it can hold well: agents work inside a finite context window, and Chroma's 2025 context rot report found that model output becomes less reliable as input length grows. Clear module or service boundaries help, because the agent can change one part while reading only the typed interfaces of the others.

How does Encore make services safer for an AI coding agent to change?

Every service lives in one Encore app, and calls between services go through generated typed clients, so a broken contract is a type error before anything runs. encore run starts all services locally with their databases and Pub/Sub topics, and the local MCP server gives the agent the traces. Each pull request gets a preview environment hosted by Encore, an environment can require approval of infrastructure changes before a deploy, and each environment runs the services as one process or as separate processes in your own AWS or GCP account.

Give your AI coding agent real local and cloud infrastructure

Encore lets developers and agents declare databases, queues, and APIs in type-safe code—then auto-provisions real local environments, MCP introspection, and AWS/GCP deploys.

~/orders — claude
Claude Code
Claude Code v2.1.180Opus 5.5~/orders