Topic 4 of 6 · 5 min read
Consistency with codebase conventions
Goal: Spot where AI code breaks a codebase convention and write the rule, in AGENTS.md or a linter, that keeps the next change consistent.
A codebase is easy to change when it does the same thing the same way everywhere; AI code drifts from that unless the conventions are written down where the agent and the tools can read them. The drift is rarely a bug. It is a tax on every future reader.
Check yourself · 1 of 3
An agent uses a different HTTP library than the rest of your project. Which fix lasts longest?
Why it matters
An agent has seen millions of repositories and one of them is yours. Left alone, it chooses what is common in general, not what is common here. Your services return Result objects, and it throws exceptions. Your code uses axios, and it reaches for fetch. Your files use snake_case, and the new one uses camelCase. Each choice is defensible alone. Together they leave a codebase where a developer cannot predict what the next file does.
Where drift shows up
- Naming. Case style, prefixes, how tests and files are named.
- Error handling. Throw, return, or log; custom error types; where errors are caught.
- Layering. Database calls inside a controller when the project keeps them in a repository layer.
- Library choice. A second HTTP client, date library or test framework.
- Structure. Where files go, how modules export, how config is read.
How to prevent it
Use two kinds of guard.
- Written rules the agent reads. Put short, concrete rules in
AGENTS.mdorCLAUDE.md: "HTTP calls go throughsrc/lib/http.ts; do not add another client." Rules that include a file path work better than values like "write clean code". - Rules a machine enforces. A linter, formatter or import rule fails the build when the agent drifts. ESLint
no-restricted-imports,ruff,dependency-cruiserand architecture tests do not depend on anyone remembering.
When you catch a drift in review, do not just fix the line. Ask: can this be a rule? Add it, so the next change is right by default.
Before and after
// Drift: a second HTTP client and a thrown string
import axios from "axios";
const res = await axios.get(url);
if (!res.data) throw "no data";
// Matches the repo
import { http } from "../lib/http";
const data = await http.get<Order>(url); // throws HttpError
{ "no-restricted-imports": ["error", { "paths": ["axios"] }] }
What goes wrong
- A huge memory file. Long files are skimmed and partly ignored. Keep rules few, specific and checkable.
- Rules without enforcement. Agents drop instructions. Back the important ones with a lint rule.
- Enforcing the wrong convention. If the repo is inconsistent already, decide which style wins first.
Examples
Layer leak
// controllers/orders.ts
const rows = await db.query("SELECT * FROM orders WHERE user_id = $1", [id]);
The project keeps SQL in repositories/. Move the query there, then add a rule to AGENTS.md: "Controllers never import db; use a repository." Add an import-restriction lint rule for controllers/.
A rule that works
Weak: "Follow our coding standards." Strong: "Errors: throw AppError from src/lib/errors.ts with a code. Never throw strings or plain Error in src/services." The strong rule has a path, a name and a boundary that a tool can check.
Key points
- Left alone, an agent follows what is common in general, not what is common in your repository.
- Drift shows up in naming, error handling, layering, library choice and file structure.
- Write short, concrete rules with file paths in AGENTS.md or CLAUDE.md so the agent can read them.
- Back important rules with a linter or import restriction, because agents sometimes drop instructions.
- When review catches a drift, turn it into a rule so the next change is right by default.
Common mistakes
- Fixing the drifting line in review without writing the rule that prevents it.
- Writing vague rules such as "follow best practices" in a memory file.
- Relying on a memory file alone for rules that a linter could enforce.
- Letting a second library in because the code works.
Check yourself · 2 of 3
Which memory-file rule is most useful to a coding agent?
Check yourself · 3 of 3
Why back a memory-file rule with a linter or architecture test?
Practice items, not counted towards your mastery.
Start here
- AI Copilot Code Quality: 2025 research (GitClear) (opens in a new tab)ArticleFree
An analysis of how code churn, copy-pasted lines and moved code changed as AI assistants spread.
- Refactoring (Martin Fowler) (opens in a new tab)BookFree
The catalogue of small behaviour-preserving steps and the reasoning behind doing them one at a time.
- Is High Quality Software Worth the Cost? (martinfowler.com) (opens in a new tab)ArticleFree
Why internal quality pays back through faster feature delivery, the case for caring about maintainability.
More for this topic
- AGENTS.md (opens in a new tab)DocsFree
An open format for giving coding agents project instructions such as conventions and commands.
- How Claude remembers your project (Claude Code docs) (opens in a new tab)DocsFree
How CLAUDE.md files carry team conventions into every agent session.
- What to look for in a code review (Google Engineering Practices) (opens in a new tab)ArticleFree
Its complexity and consistency checks apply directly to agent-written changes.