# AI coding tools write features faster than you can finish them: ship them dark behind a feature flag

> Source: <https://dev.to/attilaalbert/ai-coding-tools-write-features-faster-than-you-can-finish-them-ship-them-dark-behind-a-feature-flag-4bge>
> Published: 2026-12-06 13:06:42+00:00

**Recommendation:** When AI tools leave features half-built, merge them daily behind a single typed flag check and add a CI test that fails if any other file calls isEnabled for it.
More in the playbook "Working with AI coding agents": <https://wpnews.pro/playbooks/coding-agents>

*By Tomás Herrera, NBT*

With AI coding tools, half of a feature can exist by lunchtime. The new checkout page renders, the export runs for small files, the redesign covers three screens out of eight. The rest needs another day, a review, a test against real data.

The usual choices are both bad. Keep the work on a long-lived branch and it drifts from main until merging is painful. Merge it unfinished and users find it. A feature flag is the third option: merge small pieces into main every day, with the new code switched off until it's ready. It's also an off switch if the finished feature misbehaves in production.

You don't need a flag service to start. Here's a step-by-step setup that fits in one file, and the rules that keep flags from turning into a mess.

Martin Fowler's article on feature toggles separates flags by how long they live and who changes them. Two kinds cover most small teams:

Write the kind next to each flag. A release flag that's still there six months later is a bug.

Every check should go through one function. Ask for it like this:

```
Add src/flags.ts with:
- a typed list of flags, each with a name, kind (release | ops), owner,
  default value and a "remove by" date for release flags;
- isEnabled(name, user?) that reads overrides from env vars
  (FLAG_NEW_CHECKOUT=on|off) and falls back to the default;
- a TypeScript error if code asks for a flag that isn't in the list.
No external service. Don't use the flag anywhere yet.
```

The result looks roughly like this:

``` js
export const FLAGS = {
  newCheckout: {
    kind: "release", owner: "payments", default: false,
    removeBy: "2027-01-15",
  },
  pdfExport: { kind: "ops", owner: "reports", default: true },
} as const;

type FlagName = keyof typeof FLAGS;

export function isEnabled(name: FlagName): boolean {
  const env = process.env[`FLAG_${toSnake(name).toUpperCase()}`];
  if (env === "on") return true;
  if (env === "off") return false;
  return FLAGS[name].default;
}
```

The typed list matters more than it looks: a misspelled flag name becomes a compile error instead of a feature that's silently never on.

The most common mistake in AI-written flag code is sprinkling `if (isEnabled(...))` across twenty files. Then turning the flag off leaves half the feature visible, and deleting the flag later is a long hunt.

Tell your AI coding tools where the switch goes:

```
Gate the new checkout behind isEnabled("newCheckout") in exactly one
place: the route that chooses which checkout page to render. Old and
new code paths stay separate files. No other file may check this flag.
```

For a backend change, the single place might be where a service is chosen. For a UI change, it's usually where a route or a top-level component is picked.

Env vars are fine for on/off per environment: off in production, on in staging. To try the feature in production yourself before anyone else, add a simple user rule:

```
export function isEnabled(name: FlagName, user?: { email: string }) {
  if (user && process.env.FLAG_TESTERS?.split(",").includes(user.email)) {
    return true;
  }
  // ...env and default as before
}
```

That's enough for "staff only". If you need a percentage rollout, hash the user id (so the same person always gets the same answer) rather than using `Math.random()` on each request, which flips people between versions.

When you outgrow env vars, because you want to change a flag without redeploying, move the values to a database table or a flag service. Keep the same `isEnabled` function, so no calling code changes.

A flag doubles the paths through that part of the code. Ask for tests that run both:

```
For every test of checkout, run it twice: newCheckout on and off.
Add one test that fails if any file other than src/routes/checkout.ts
calls isEnabled("newCheckout").
```

The second test is a cheap guard for Step 3. A grep in CI does the same job.

An ops flag is only useful if flipping it is quick. Write down, in the README or runbook, exactly how to turn each ops flag off in production: which setting, where, and how long it takes to apply. If changing an env var needs a redeploy that takes ten minutes, your kill switch takes ten minutes. That may be fine. Know it before the incident.

Also decide what "off" means. For `pdfExport`, off should show a friendly "PDF export is paused" message, not a 500 error.

Every release flag got a "remove by" date in Step 2. Use it:

```
List release flags whose removeBy date has passed. For each fully
rolled-out flag, delete the flag, the old code path and its tests,
in a separate commit per flag.
```

Removing a flag is a good task for AI coding tools: it's mechanical, the typed list shows every use, and the tests from Step 5 tell you whether anything broke. Do one flag per commit so a mistake is easy to revert.

A CI check that fails when a `removeBy` date is more than two weeks past keeps this from slipping.

```
Feature flags:
- Add flags only in src/flags.ts, with kind, owner, removeBy.
- Check a flag in one place per feature.
- New code paths go in new files; don't interleave old and new.
- Tests run with the flag on and off.
```

With these in place, you can merge AI-written work every day without waiting for the whole feature, and turn it off again in a minute if it misbehaves. That's also what makes it easier to [review code from AI coding tools](https://www.ninjabuilder.app/blog/review-ai-generated-code) in small, safe pieces instead of one giant diff.
