← all articles

Keeping an AI Coding Assistant Inside Your Repo's Conventions

The pattern you’ve probably seen

You ask an assistant to add a new endpoint. It comes back with working code. It also comes back with a naming scheme your team stopped using two years ago, a try/except block where the rest of the file uses a shared error wrapper, and a docstring that explains what the function does instead of why it exists. None of it is wrong. All of it is off.

This isn’t a model quality problem in the sense of “the answer is incorrect.” The code runs, the tests might even pass. The issue is that the model has no built in sense of your repo’s conventions unless you give it one, every single time, in a form it actually reads.

Why the drift happens

Large language models generate the statistically likely next token given their training data and whatever context you hand them in that call. Public training data skews toward generic, textbook style patterns because that’s what’s abundant on GitHub, in tutorials, and in documentation. Your repo’s specific choices, like using a custom Result type instead of exceptions, or a flat file structure instead of nested feature folders, are a tiny signal against a huge amount of “normal” code the model has seen. Left alone, it regresses to the mean.

The other piece is statelessness. Every call to a model API is independent. There’s no persistent memory of your last session unless the tool you’re using is built to re-inject context (a system prompt, a project file, retrieved snippets) on every request. If that context doesn’t mention your conventions, the model has no way to know they exist, no matter how many times you corrected it yesterday.

The three levers that actually work

Instruction files. Claude Code reads a CLAUDE.md file at the start of a session. Cursor reads .cursorrules. GitHub Copilot supports a copilot-instructions.md file. Codex CLI and other agent harnesses have their own equivalents, often under AGENTS.md. These are plain text files that get folded into the system prompt or early context on every call. They work because the model treats them as high priority instructions, not because the model “remembers” anything between sessions.

Existing code as few-shot examples. Models pattern match on what’s already in front of them far more reliably than on prose describing a rule. If you tell an assistant “we always validate input at the boundary, not deep in the call stack” that’s a sentence competing with millions of other sentences it’s seen. If you show it three existing files that do exactly that, it copies the pattern with much higher fidelity. Pointing an assistant at a real file (“write this the way orders/service.py handles validation”) beats describing the abstraction almost every time.

Automated guardrails. Linters, formatters, type checkers, and CI checks don’t care whether a human or a model wrote the code. A pre-commit hook that runs ruff, a type checker that fails the build on an untyped function, a CI job that blocks a merge without a matching test file: these catch convention violations regardless of source. This is the layer that actually enforces rather than suggests.

Why instruction files alone aren’t enough

A long CLAUDE.md or AGENTS.md file feels like it should solve the problem. In practice it has two limits worth naming plainly.

First, it’s advisory, not enforced. The model can read “never use bare except blocks” and still write one three turns later if the surrounding context nudges it hard enough in another direction. Instructions compete with everything else in the context window; they bias the model’s underlying tendencies rather than overriding them.

Second, every token in that file gets sent, and often billed, on every relevant call. APIs are stateless, so a 3,000 word conventions file isn’t read once and cached for free; it’s part of the context payload each time the assistant needs to reason about your code (prompt caching from providers like Anthropic and OpenAI can reduce the repeated cost of static context, but it doesn’t make it free, and it doesn’t stop a long file from crowding out other context the model needs in that turn). Teams that write exhaustive style guides into these files often find the assistant gets slower to respond and no more consistent, because the important rules are buried under paragraphs of things that could have been a linter rule instead.

The practical split I’ve settled on: put things in the instructions file that a machine can’t check (why we chose this architecture, what NOT to touch, which directories are legacy and shouldn’t be copied from) and put things a machine can check (formatting, import order, naming casing) into an actual linter config. If a rule can be expressed as a regex or an AST check, it belongs in tooling, not prose.

The multi-tool fragmentation problem

If your team uses one assistant, you write one instructions file and move on. Most teams don’t. Someone’s on Cursor, someone’s on Claude Code, someone’s using Copilot in VS Code, and now you’re maintaining CLAUDE.md, .cursorrules, and copilot-instructions.md as three separate files that drift out of sync with each other and with the codebase. There’s no single standard yet that every tool reads, and duplicating the same conventions across three files means updating conventions three times or watching them quietly diverge.

The workaround I’ve seen hold up: keep one canonical file, usually AGENTS.md since that’s the name gaining the most cross-tool adoption, and have the other tool specific files be short stubs that just say “see AGENTS.md.” It’s clumsy, but it beats three files that all claim to be the source of truth.

What actually keeps an assistant on the rails

A few things that have made a real difference, none of them exotic:

  • Keep the instructions file short enough that a human would actually reread it. If nobody on the team has looked at it in months, it’s decoration, not a control.
  • Reference real files by path instead of describing patterns abstractly. “Follow the pattern in api/handlers/users.py” gives the model something concrete to copy.
  • Push anything mechanically checkable into a linter, formatter, or type checker, and wire it into CI so a convention violation fails the build whether a human or an assistant wrote the line.
  • Review AI-generated diffs the way you’d review a new hire’s first few PRs: closely, and specifically for house style, not just correctness. That review process is doing more enforcement work than the instructions file is.
  • Update the instructions file when you catch drift, the same day, while you remember exactly what went wrong. Stale instructions are worse than none because they create false confidence.

None of this makes an assistant understand your conventions the way a teammate who’s been on the repo for two years does. What it does is close the gap between “technically correct” and “actually fits,” which is most of what code review time gets spent on anyway.

If you want more breakdowns like this on the tools and tradeoffs behind AI coding workflows, AI Tool Gazette is where we cover them without the sponsored spin. Check out more from the site.

for builders
Running agents or scrapers at scale?

AI pipelines that crawl, research, or automate the web hit rate limits and geo-blocks fast. Singapore Mobile Proxy runs real 4G/5G mobile IPs that carriers still trust.

see plans →
read on
More from the Gazette

Tool reviews, model and pricing news, and build guides for people shipping real things with AI.

browse all articles →