6 min readBy JJ Eaton

Why AI coding agents work better in a well-structured codebase

Domain-driven design, plus services and repositories, gives AI coding agents clear names, one home for business logic and an easy line to test against.

A lot of the talk about AI coding agents is about which model to use. In my experience, the shape of the codebase you point the agent at matters just as much. This week Matt Pocock posted that the thing blowing people's minds most in his course is not the agentic material. It is domain-driven design: building a shared language for the business, using that language with the agent, and refactoring the code to match it.

That lined up with how I already work, so I replied with why I think it lands so well. Domain-driven design is close to a perfect software pattern for agents, and it works especially well when you combine it with the services and repositories pattern.

What I mean by domain-driven design

Domain-driven design, usually shortened to DDD, is an old idea from well before AI. You organise software around the real business it serves, and you use the same words in the code that people use in the business.

If your team talks about "quotes", "jobs" and "invoices", the code has things called quotes, jobs and invoices, with rules that match how those actually work. It does not have a vague "records" table and a pile of helper functions with names only the original developer understood.

The shared vocabulary is often called the ubiquitous language. The point is that a business owner, a developer and now an AI agent can all say "approve the quote" and mean exactly the same thing.

Where services and repositories come in

The second half of the pattern is about where code lives.

Services hold the business logic. The rules for approving a quote, or for when a job counts as complete, sit in one place instead of being scattered across screens, scripts and database triggers.

Repositories handle storage. They save and load quotes, jobs and invoices, and the rest of the code does not need to care which database is underneath.

Used together, services and repositories act as the ports and adapters of the system. The core business logic sits in the middle, and the outside world (databases, payment providers, email, other APIs) plugs in at the edges.

Why this suits AI agents so well

Here are the four reasons I gave in my post, and what each one means when an agent is doing the work.

Centralised business logic. When the rules live in one place, the agent only has to find and change one place. In a messy codebase the same rule is often copied into several spots, and an agent will happily update one of them and miss the rest. That is how you end up with a bug that only shows up on some screens.

A clear testing boundary. Services are the natural place to test business rules, because they do not depend on a real database or a live payment provider. An agent can write a change, run the tests for that service and get a clear pass or fail. That feedback loop is a big part of what makes agent work reliable instead of hopeful.

Easy to understand. An agent only knows what it can read. When the structure is predictable, it spends less of its effort working out where things are and more of it doing the task you asked for. The same is true for the person reviewing its work.

Clear, simple naming. This is the one people underrate. When you ask an agent to "add a discount to approved quotes", and the code has a quote service with an approve step, the agent's job is mostly done before it starts. When the code calls the same idea three different names, the agent has to guess, and guessing is where mistakes come from.

Maybe it feels natural to me because that is how I was trained before AI. But the combination of DDD with services and repositories really does fit the way we now prompt and work with agents.

What this means for a business

If you are a CEO or CTO planning to use AI coding agents, this has a few practical consequences.

Your codebase is part of your AI strategy. Two companies can buy the same agent and get very different results. The one with a clear structure and clear names will get changes it can trust. The one with years of tangled code will get more rework, longer reviews and more surprises in production.

Speed comes from fewer wrong turns. The gains from agents are not only about how fast they type. They come from the agent making the right change the first time, in the right place, with a test that proves it.

Risk goes down when the rules have one home. Business rules around money, approvals and customer data are exactly the ones you do not want changed in one place and forgotten in another. Centralising them makes agent changes easier to review and easier to roll back.

The language is a business asset, not just a technical one. Agreeing on what your core terms mean helps your staff, your developers and your agents at the same time. It is often the cheapest improvement you can make, and you can start it with a whiteboard rather than a rewrite.

How I would start

You do not need to rebuild everything. A practical path looks like this:

  1. Write down the handful of core terms your business uses every day, and what each one means.
  2. Pick one area of the product where agents will do the most work, and make the code there use those terms.
  3. Move the business rules for that area into services, and the database code into repositories.
  4. Add tests at the service level so the agent has something solid to check its work against.
  5. Point the agent at that area first, see how it performs, then spread the pattern outward.

Where to start

If your team is starting to use AI coding agents and the results feel inconsistent, it is worth looking at how the code is organised before you blame the model. That is part of what we cover in a Build Things AI readiness audit: which workflows suit agents, how to structure the work so agents can do it well, and how to keep a person in control of what ships. I am based in Perth, Australia, and work with companies anywhere.

Sources