Skip to content
mentoor

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?

Choose one answer

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

  1. Naming. Case style, prefixes, how tests and files are named.
  2. Error handling. Throw, return, or log; custom error types; where errors are caught.
  3. Layering. Database calls inside a controller when the project keeps them in a repository layer.
  4. Library choice. A second HTTP client, date library or test framework.
  5. 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.md or CLAUDE.md: "HTTP calls go through src/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-cruiser and 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?

Choose one answer

Check yourself · 3 of 3

Why back a memory-file rule with a linter or architecture test?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic