Coding with AI assistants
AI assistants and coding agents can write a lot of code very fast, but you still own every line you merge. Give them clear context and small tasks, let tests be the guard, write the important tests from the spec yourself, and review the change based on its risk. Watch for made-up APIs and packages, security holes and leaked secrets.
Author: bezzad
Contents
The problem: fast code, slow trouble
A developer in our shop team asked an AI coding agent: “Add coupon codes to the checkout.” Twenty minutes later there was a pull request with 900 changed lines, and all tests were green. The developer merged it.
A week later:
- Expired coupons still worked. Nobody had told the agent that coupons expire.
- The tests were green, because the agent wrote the tests from its own code. They checked what the code did, not what it should do.
- A new package in the change had a name very close to a popular one. Nobody checked it.
- The pull request was too big to review well, so the reviewer only skimmed it.
AI did not make the mistake alone. The process around it did.
What these tools are
There are a few kinds of AI coding tools:
- Autocomplete. It suggests the next lines while you type.
- Chat assistants. You ask a question or paste code, and it answers or writes code.
- Coding agents. They read the project, edit many files, run commands and tests, and try again until the checks pass. They can work for a long time without you.
The more a tool can do alone, the more speed you get, and the more careful you must be.
Rule one: you own the code
If you merge it, it is your code. “The AI wrote it” is not an excuse in an incident review.
- You must understand every change you merge. If you cannot explain it, do not merge it.
- You decide the design. The tool suggests. You choose.
- You are responsible for security, privacy and cost. The tool does not know your rules unless you tell it.
Give good context
An AI tool only knows what it can see. Most bad results come from missing context.
- The goal and the why. “Customers want to use coupon codes from our email campaigns.”
- The rules. “Coupons expire. One coupon per order. The discount is never more than 50 euros.”
- What to follow. “Use the existing Money type. Follow the style of the shipping module.”
- What not to touch. “Do not change the payment provider code.”
- How to check. “Run the unit tests and the type checker. All must pass.”
Many projects keep a short instructions file in the repository for AI tools, with the build commands, the code style and the main rules. Write it once, and every task gets better.
Small tasks
Big tasks give big, hard to review changes. Cut the work into small steps, just like thin slices:
- Step 1. Add the Coupon model and its rules, with tests.
- Step 2. Add the endpoint that checks a coupon code.
- Step 3. Show the coupon field in the checkout, behind a feature flag.
Each step is one small pull request. Each one is easy to review and easy to undo.
Tests are the guard, so write the important ones yourself
An agent tries again until the tests are green. So the tests decide what “correct” means. If the tests are weak, the agent will happily make weak code pass.
Here is what the agent wrote:
export function applyCoupon(total: number, coupon: Coupon) {
return total - (total * coupon.percent) / 100;
}
test("applies coupon", () => {
expect(applyCoupon(100, { percent: 10 })).toBe(90);
});
The test passes. But it only repeats what the code does. Tests written from the rules find the gaps:
test("expired coupon gives no discount", () => {
const coupon = { percent: 10, expiresAt: yesterday };
expect(applyCoupon(100, coupon, today)).toBe(100);
});
test("discount is never more than 50", () => {
const coupon = { percent: 50, expiresAt: tomorrow };
expect(applyCoupon(400, coupon, today)).toBe(350);
});
- Write the key tests from the spec, before or with the code. Then let the tool make them pass.
- Read the tests the tool writes. Do they check rules, or only repeat the code?
- Watch out for tests that were changed to pass. An agent may weaken or skip a test that “was in the way”. Look at test changes in every diff.
Review based on risk
Not every change needs the same review. The risk of the change decides, not who wrote it.
What to look for in AI-written code:
- Made-up things. Functions, settings or packages that do not exist, or exist with a different meaning. The build catches some of them. Packages need a check by hand.
- New packages. Is the package real, popular and kept up to date? Is the name exactly right? Attackers publish packages with names that look like real ones, or names that AI tools often invent.
- Security. Text glued into SQL or shell commands, missing permission checks, secrets in code, too much data in logs.
- Copy and paste instead of reuse. The tool may write a new helper when the project already has one.
- Old patterns. Tools learned from old code too. Check that the code uses current versions and your project’s style.
- Too much. Extra options, extra layers, code “for later”. Remove it.
Keep data and access safe
- Use only tools your company allows. Some tools send your code to outside servers.
- Never paste secrets or customer data into a prompt: no passwords, keys or real customer records.
- Give agents only the access they need. Run them in a sandbox or a separate branch. Never give them production database passwords.
- Treat what the agent reads as untrusted. A web page, issue or file can contain hidden instructions written by someone else. An agent with too much access may follow them.
When not to use it
AI tools are great for many jobs, such as first drafts, tests, scripts, moving code to a new version and explaining old code. Be more careful when:
- You do not understand the problem yet. First think, then ask the tool. Otherwise you get confident code for the wrong problem.
- The code is security critical, and nobody on the team can review it deeply.
- You are learning. If you always let the tool do it, you will not be able to review its work later.
Common mistakes
| Mistake | Result | Right way |
|---|---|---|
| “The AI wrote it” | Nobody owns the bug. | You own every line you merge. |
| One huge task | Huge diff, weak review. | Small steps, small pull requests. |
| No rules in the prompt | Code that ignores business rules. | Goal, rules, examples, limits, how to check. |
| Only tests written by the tool | Tests repeat the code. | Write key tests from the spec. |
| Not reading test changes | Weakened or skipped tests. | Review tests in every diff. |
| New packages without a check | Fake or harmful packages. | Check the name, the owner and the activity. |
| Secrets or customer data in prompts | Data leaks. | Only approved tools, no secrets, no real data. |
| Agent with production access | One wrong step hits real users. | Sandbox, least access. |
Summary in six lines
- You own every line you merge, whoever wrote it.
- Give clear context: goal, rules, examples, limits, how to check.
- Cut work into small tasks and small pull requests.
- Write the important tests from the spec, and read every test change.
- Review based on risk, and check new packages and security by hand.
- Keep secrets and customer data out of prompts, and give agents the least access.