Archive/MODELS

AI-assisted coding that survives code review: a practical workflow

Scope small, give real context, write the test first and review every diff like a stranger's. A workflow for shipping AI-written code with confidence.

AI-assisted coding that survives code review: a practical workflow

AI coding assistants can now write whole functions, refactor modules and fix failing tests from a description. Used well, they remove a lot of tedious work. Used carelessly, they produce code that compiles, passes a quick glance and fails in production in ways nobody on the team understands, because nobody on the team wrote it.

The difference is rarely the model. It is the workflow around it. This guide describes a workflow for AI-assisted coding that produces changes a careful reviewer would approve: how to scope tasks, what context to provide, why tests should come first, how to review AI-written diffs, and which bugs to look for.

Where AI coding assistants help most, and least

Assistants are strongest where the task is well specified and the surrounding conventions are visible:

They are weakest where correctness depends on knowledge that is not in the prompt: business rules, implicit invariants, performance characteristics of your infrastructure, security assumptions, and anything that requires understanding why the system is shaped the way it is. That is not a reason to avoid them there, but it is where review must be strictest.

Step 1: scope the task to one reviewable change

The single most effective habit is asking for small changes. A task that produces a 40-line diff can be reviewed properly; a 900-line diff will be skimmed. Break work into steps that each leave the codebase working:

  1. Add the new data type and its tests.
  2. Add the function that produces it.
  3. Wire it into the endpoint behind a flag.
  4. Remove the old path.

This is the same decomposition principle that helps with any long task; see decomposition prompts for long, messy tasks.

Step 2: give the assistant the context a new teammate would need

Models write code that matches what they can see. If they cannot see your conventions, they invent their own. Before asking for code, provide:

Many tools support a persistent instructions file for a repository. Treat it like a system prompt: short, specific and about things that actually go wrong. Our anatomy of a production system prompt applies directly.

Step 3: write or generate the test first

Ask for tests before implementation, review the tests yourself, then ask for an implementation that makes them pass. Tests are shorter and easier to review than code, and reviewing them forces you to decide what "correct" means before you see a plausible implementation that might anchor your judgement.

PROMPT · TESTED ON CODING ASSISTANTS
Write unit tests for a function `parse_invoice_total(text: str) -> Decimal`. Cover: currency symbols (€, $, £), thousands separators in both "1,234.50" and "1.234,50" formats, missing totals (raise ValueError), and negative credit notes. Do not implement the function yet. List any behaviour you had to assume.

The last line matters: it surfaces assumptions you can confirm or correct before they become code.

Step 4: ask for a diff, with reasons

Request changes as a diff or a clearly delimited set of edits, and ask the assistant to explain each change in one sentence. Unexplained changes outside the requested scope, such as reformatted files, renamed variables or "improved" error handling, are a common source of regressions and noisy reviews. Tell the assistant explicitly not to touch unrelated code.

Step 5: review AI-written code like a stranger's pull request

Review with more suspicion than you would for a trusted colleague, not less. The code will look confident and idiomatic, which makes errors easier to miss.

What to check in an AI-written diff
CheckWhat goes wrong
Does it call real APIs?Invented functions, options or library versions
Edge casesEmpty inputs, nulls, time zones, Unicode, very large values
Error handlingSwallowed exceptions, generic catch-alls, missing retries
SecurityUnsanitised input in queries, secrets in logs, overly broad permissions
ConcurrencyShared state, race conditions, missing locks or transactions
TestsTests that assert the implementation rather than the requirement
ScopeUnrelated edits mixed into the change

Be especially wary of tests written in the same session as the implementation. A model that misunderstands a requirement will write a test that encodes the same misunderstanding, and both will pass.

Step 6: run everything, then read the failures yourself

Run the full test suite, the linter, the type checker and, where relevant, the application. When something fails, read the error before pasting it back to the assistant. Often the fix is obvious, and understanding it keeps you in control of the codebase. Repeatedly pasting errors back without reading them is how teams end up with code nobody can maintain.

Prompts that work well for common coding jobs

A handful of request shapes cover most day-to-day work. Keep them in a shared snippets file so the whole team benefits.

Each of these keeps the human in charge of what changes and why, which is the core of the workflow.

When to let an agent run on its own

Agentic coding tools can edit files, run commands and iterate until tests pass. They are productive for well-tested codebases and tightly scoped tasks. Give them guardrails: a clean branch, a task with a clear done condition, permission limits on commands, and a human review before merge. The general trade-offs between autonomous agents and fixed workflows are covered in do you need an agent, or just a workflow?.

Team practices that keep quality high

Frequently asked questions

Is AI-generated code safe to use in production?

It can be, if it goes through the same review, testing and security checks as any other code. The risk comes from skipping review because the code looks correct, not from where it was written.

How do I get better code from an AI coding assistant?

Give it the relevant files and conventions, keep each task small, ask for tests first, request a diff with explanations, and state constraints such as allowed dependencies and runtime versions explicitly.

Should AI write the tests and the code?

It can write both, but review the tests first and separately. Tests written alongside the implementation tend to share its misunderstandings, so a human should confirm the tests describe the real requirement.

What are the most common bugs in AI-written code?

Calls to non-existent functions or options, missing edge cases, overly broad error handling, security issues with unsanitised input, and unrequested changes outside the task's scope.

RK
WRITTEN BYRafael KimAgents and tooling. Has personally been booked into a yurt by a language model.

Keep reading

Liked this? The Sunday Diff has one like it every week.