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.
Step 1: Know which kind of flag you're adding
Martin Fowler's article on feature toggles separates flags by how long they live and who changes them. Two kinds cover most small teams:
- Release flags: hide unfinished work. They should live days or weeks and then be deleted.
- Ops flags (kill switches): turn off a risky or expensive part in an emergency. They can live longer.
Write the kind next to each flag. A release flag that's still there six months later is a bug.
Step 2: One module that answers "is this on?"
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:
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.
Step 3: Put the check at one entry point
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.
Step 4: Turn it on for yourself first
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.
Step 5: Test both sides
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.
Step 6: Make the kill switch fast
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.
Step 7: Delete release flags on schedule
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.
Rules worth writing into your instructions file
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 in small, safe pieces instead of one giant diff.