← Blog

How Do You Teach an AI Coding Agent Your Engineering Standards?

By Jeet Patel7 min read

An AI coding agent follows your standards only if they’re written down. Ours runs to about 2,000 lines, with 60 examples of bad and good code, and a workflow where nothing gets built without approval.

In short: Our backend coding agent is defined by a single, version-controlled instruction file of about 2,000 lines. It has two halves: a gated workflow (read context, ask questions, check for a simpler approach, check for reusable code, decide where each piece of logic belongs, present an ordered plan, wait for approval, then build one item at a time) and a rulebook (about 30 “always do” rules, 27 “never do” rules and 60 bad-versus-good code examples). The result is code that looks like our senior engineers wrote it, because it follows the same rules they do.

Why “write clean code” doesn’t work

Ask an AI agent to “follow our coding standards” and it will follow a set of standards: the average of everything it has seen. That average is usually reasonable and rarely yours.

Your codebase has opinions. Maybe every handler is synchronous. Maybe the handler owns the database commit, never the service. Maybe money is always stored as integer cents. Maybe you’ve banned a pattern because it caused an outage. None of that is visible from a single file, and an agent working on one task at a time won’t infer it reliably.

So we wrote it all down. Not as a style guide for humans to skim, but as an operating manual for an agent: precise, ordered and full of examples.

Part 1: the gated workflow

The agent follows the same sequence for every task. Each step is a gate: the agent can’t move to the next step until the current one is complete, and several steps require explicit human approval.

01 · The coding agent’s workflow
The coding agent’s workflowThree phases: Understand, Plan and Build. Understand checks environment and branch, reads context, asks questions one at a time and confirms understanding, returning to questions if not confirmed. Plan checks simplicity, reuse and logic placement, presents an ordered plan by layer and returns to planning if not approved. Build verifies one item at a time, repeats for the next item and finishes with a summary and updated API specification.1 · Understand2 · Plan3 · BuildCheck environmentand branchRead contextAsk questionsone at a timeUnderstandingconfirmed?noSimplicity checkReuse checkLogic placementOrdered planby layerPlanapproved?noBuild one itemItemverified?nextitemall doneSummary andupdate API spec
Figure 1. The coding agent’s workflow. Diamonds are human approval gates.

Here’s what the less familiar steps do.

The simplicity check

Before planning anything, the agent must ask itself whether a simpler approach exists: a single query instead of several, an existing relationship instead of a new lookup, a smaller data model. If it finds one, it must stop and say so:

“I see a simpler way to implement this: ... This avoids ... Would you like to go with this instead?”

The instruction is explicit: do not silently implement the complex version. AI agents tend to over-build. This step pushes back.

The reuse check

Next, the agent searches the context repository and the codebase for anything that already exists, then reports its findings in a fixed format:

Reuse check:
- validate_staff_access_to_patient: exists in dependencies.py, reusing
- create_subscription_credit: exists in wallet/transactions.py, reusing
- compute_next_renewal_date: does not exist, writing new in _helpers.py

This one step prevents most duplicated logic, which is one of the most common problems with AI-written code.

Logic placement

For every piece of logic, the agent must decide, and state, where it belongs:

CategoryUse it forExample
Model propertyA value or state intrinsic to the entityis_expired, is_active
DependencyAn access rule reused across routes“staff must have access to this patient”
Service validationA rule specific to one flow“start date must be today or later”

Putting logic in the right layer the first time is what keeps a codebase maintainable as it grows.

The ordered plan

The work is broken into items in a fixed layer order: permissions, migrations, models, supporting CRUD, dependencies, shared utilities, services, handlers and schemas, then tests. Each item is independently verifiable. The agent presents the list and waits for approval before starting item one.

Build one, verify, then move on

The agent builds a single item, checks its own work (imports, stale references, leftover conflict markers), reports what it wrote and waits for confirmation. It never batches items, and never starts item N+1 while item N is unverified.

Close the loop

When everything is done, the agent summarizes what changed and offers to update the API specification in the context repository. That’s how the context stays current: every feature leaves the specification more accurate than it found it.

Part 2: the rulebook

The second half of the file is the standards themselves. Three things make it effective.

Rules are absolute and short

About 30 “always do” rules and 27 “never do” rules, each one line. A few examples, paraphrased:

  • Always commit in the handler, never in a service.
  • Always paginate; never call .all() on an unbounded query.
  • Never run a query inside a loop.
  • Always aggregate in the database, never in a Python list.
  • Always store money as integer cents; never use floats or decimals for money.

Every important rule has a bad and a good example

There are 60 bad-versus-good code pairs in the file. Agents learn far better from contrast than from instructions alone. Here’s an illustrative pair in the same style:

# Bad: one query per patient (N+1)
for patient_id in patient_ids:
    record = session.query(Record).filter(Record.patient_id == patient_id).first()

# Good: one query with an IN clause
records = session.query(Record).filter(Record.patient_id.in_(patient_ids)).all()

And for money:

# Bad: inline arithmetic, easy to forget rounding
amount_cents = int(payload.amount * 100)

# Good: one shared helper with consistent rounding
amount_cents = to_cents(payload.amount)

Every non-trivial query explains itself

The agent must add a comment to every non-trivial query explaining why it’s written that way: which join path was chosen, which edge case a filter handles. Future readers, human or AI, shouldn’t have to reverse-engineer intent.

What this produced

On the billing module we’ve used as an example throughout this series, code written this way shares a recognizable shape:

  • one service class per business action, with a single entry point;
  • validation, fetching and mutation in separate, clearly named methods;
  • the transaction boundary owned by the handler;
  • money as integer cents throughout;
  • idempotency guards on payment operations, so a retried request can’t double-charge.

The point isn’t that the AI is clever. It’s that the AI is consistent, and consistency is what a rulebook buys you.

The trade-offs

  • The rulebook locks in your stack. Ours tells the agent to write synchronous handlers on an older framework version, because that’s what the codebase uses. That’s right for consistency, but the agent will never modernize anything unless you change the rules.
  • Long instruction files have costs. Every line is read on every task. We keep rules short and push detail into examples.
  • Verification still leans on people. Today the developer confirms each item works. We’d like the agent to run the relevant tests itself before asking.
  • Tests come last in the layer order. Writing them first would catch misunderstandings earlier.

How to start your own

  1. Write down the ten rules your senior engineers repeat most often in code review.
  2. Add one bad and one good example for each.
  3. Add a reuse check and a “wait for approval” step to your agent’s workflow.
  4. Every time review catches something the agent should have known, add a rule.

The forward deployed angle

A forward deployed team works inside codebases it didn’t design. A written rulebook is how a small team delivers consistent quality in each one: the client’s conventions are encoded once, and every agent follows them from the first task. We cover this in more depth in our article on forward deployed engineering in the age of AI agents.

Key takeaways

  • An agent follows your standards only if they’re written down precisely.
  • Separate the workflow (what to do, in what order) from the rulebook (how to do it).
  • Make the agent check for simpler approaches and existing code before writing anything.
  • Use bad-versus-good examples; they teach better than instructions.
  • Keep humans as approval gates on the plan and on each step.

Next, see how rule codes connect specifications to test cases, making AI-built software checkable.

Frequently asked questions

How long should an AI agent’s instruction file be?

As long as it needs to be to remove guesswork, and no longer. Ours is about 2,000 lines because it covers architecture, queries, money, errors, logging, tests and email. Short, absolute rules plus examples keep it usable.

Why make the agent wait for approval at each step?

Because a wrong plan is cheap to fix before any code is written and expensive to fix after. Approval gates keep the human in charge of decisions and the agent in charge of execution.

What is a reuse check?

A step where the agent searches the codebase and specifications for existing functions, dependencies and helpers before writing anything new, and reports what it will reuse and what it will create.

Can the agent update its own rules?

In our setup, no. The rulebook is changed by people, through a reviewed pull request, so the whole team gets the update and nothing changes silently.