Writing Feature Flag Code With LLMs in November 2026: Pick a Safe Default, Give Every Flag an Owner and an End Date, and Test Both Paths

Feature flags let you ship code that is turned off, turn it on for a few users, and turn it off again in seconds if something breaks. They are simple in concept, and that is exactly why code generated by an LLM for them often looks finished when it is not. The model will happily write if (flags.isEnabled("new_checkout")) and move on. What it usually skips: what happens when the flag service is down, who removes the flag later, how a user stays in the same bucket on every request, and whether the "off" path still works after six months of nobody running it.

This guide walks through how to get feature flag code from an LLM that holds up in production: what to put in the prompt, the specific failure modes to make it handle, and how to test the result.

Decide what kind of flag you are writing

"Feature flag" covers several different things, and they need different code. Tell the model which one you mean, because the defaults, lifetimes, and evaluation rules differ:

  • Release flag: hides unfinished or risky code until it is ready. Short-lived. Should be deleted within weeks of reaching 100 percent.
  • Kill switch: turns off an expensive or fragile feature during an incident. Long-lived. The safe default is usually "feature on" during normal operation, with a fast way to flip it off.
  • Experiment flag: splits users into variants for an A/B test. Needs stable, sticky assignment and exposure logging.
  • Permission or plan flag: enables a feature for certain customers. This is really authorization or billing logic, and it often belongs in your data model rather than a flag service.

A one-line prompt like "add a feature flag for the new search page" leaves the model to guess. A better prompt names the type, the expected lifetime, and the audience: "Add a release flag search_v2. It will roll out to internal staff first, then 10 percent of logged-in users, then everyone. It should be removed after full rollout. If flag evaluation fails, users must get the old search page."

Make the safe default explicit

The most important decision in flag code is what happens when the flag cannot be evaluated: the SDK has not initialized yet, the flag service times out, the flag was deleted, or the key has a typo. Most flag SDKs accept a default value at the call site, and LLMs often fill it in without thinking, sometimes with true.

Ask the model to state the default for every flag and to justify it. A reasonable rule of thumb:

  • Release and experiment flags default to off (the old, known-good behavior).
  • Kill switches default to the normal operating state, so a flag outage does not turn off a working feature for everyone.

Then make the code put that default in one place. A small wrapper does this well:

// flags.ts
const DEFAULTS = {
  search_v2: false,          // release flag, owner: search team, remove by 2027-01-31
  recommendations_on: true,  // kill switch, owner: platform team
} as const;

type FlagKey = keyof typeof DEFAULTS;

export async function isEnabled(key: FlagKey, user: { id: string }): Promise<boolean> {
  try {
    return await client.boolVariation(key, { key: user.id }, DEFAULTS[key]);
  } catch (err) {
    logger.warn("flag_eval_failed", { key, error: String(err) });
    return DEFAULTS[key];
  }
}

The FlagKey type means a misspelled flag name fails at compile time instead of silently returning the default forever. Ask the model for this pattern by name; it will not always offer it unprompted. The exact client method names vary by SDK, so paste the real SDK documentation or an existing call from your codebase into the prompt rather than letting the model guess the API.

A row of signal levers in a railway signal box

A signal box lever has a known resting position. Every feature flag needs one too. Image: Lever frame in the Sourbrodt train station signal box by Trougnouf (Benoit Brummer), CC BY 4.0, via Wikimedia Commons.

Keep evaluation fast and off the critical path

Flag checks often sit inside hot code: request handlers, render functions, loops. Generated code sometimes calls a remote API on every check. Most production SDKs avoid this by keeping flag rules in memory and updating them in the background, but self-written flag systems built by an LLM frequently do a database query or HTTP call per evaluation.

Tell the model your constraints directly: "Flag evaluation must not make a network call per request. Load rules at startup, refresh them in the background, and keep serving the last known rules if the refresh fails." Then review the code for these specific problems:

  • A network or database call inside isEnabled itself.
  • No timeout on the initial load, so the app hangs at startup if the flag service is slow.
  • Rules cleared on a failed refresh instead of keeping the previous version.
  • The same flag evaluated many times in a single request with possibly different answers. Evaluate once per request and pass the result down.

Make percentage rollouts sticky

When you roll out to 10 percent of users, the same user must land in the same bucket every time. Otherwise they see the new checkout, refresh, and see the old one. A common LLM mistake in hand-rolled flag code is using Math.random() < 0.1, which reassigns users on every request.

The usual fix is to hash a stable identifier together with the flag name:

import { createHash } from "node:crypto";

function bucket(flagKey: string, userId: string): number {
  const h = createHash("sha256").update(`${flagKey}:${userId}`).digest();
  return h.readUInt32BE(0) % 10000; // 0..9999
}

function inRollout(flagKey: string, userId: string, percent: number): boolean {
  return bucket(flagKey, userId) < percent * 100;
}

Including the flag key in the hash keeps different flags from always picking the same 10 percent of users. Ask the model to explain which identifier it hashes, and think about anonymous visitors: if there is no user ID, you need a stable device or session ID, or those visitors will flip between variants.

If you use a hosted flag service, its SDK handles bucketing for you. In that case, the thing to check is that you pass the same user key everywhere, not an email in one place and a database ID in another.

Give every flag an owner and an end date

Flags are cheap to add and easy to forget. Old release flags pile up, and each one doubles the number of code paths nobody tests. The fix is mostly process, but an LLM can help enforce it if you ask.

  • Require a comment or metadata entry for each flag with an owner and a removal date, as in the DEFAULTS map above.
  • Ask the model to write a small CI check that fails when a release flag is past its removal date.
  • When a flag reaches 100 percent, ask the model to remove it: "Remove the search_v2 flag. Keep only the enabled path, delete the old code path and its tests, and remove the entry from DEFAULTS."

Flag removal is a task LLMs do well, because it is mechanical and easy to verify in a diff. Review it anyway: models sometimes keep the wrong branch when a condition is negated, such as if (!isEnabled(...)). Read each changed branch and confirm the surviving code is the new behavior.

Close-up of toggle switches and an indicator light on a vintage control panel

Every switch you add is another path to test, and another one someone has to remove later. Image: Vintage electronic device control panel with toggle switches by Shixart1985, CC BY 2.0, via Wikimedia Commons.

Test both paths, every time

The biggest risk with flags is that the "off" path rots. Tests run with whatever the default is, so the other path goes untested until someone flips the flag during an incident and finds it broken.

Ask the model to generate tests that run the relevant behavior with the flag both on and off, using an in-memory or stubbed flag client rather than the real service:

describe.each([true, false])("checkout with search_v2=%s", (enabled) => {
  beforeEach(() => setTestFlags({ search_v2: enabled }));

  it("returns results for a known query", async () => {
    const res = await search("blue shoes");
    expect(res.items.length).toBeGreaterThan(0);
  });
});

Also ask for three failure tests that generated code often lacks:

  1. Flag service unavailable: the client throws or times out, and the code returns the documented default.
  2. Unknown flag: the flag was deleted on the service side, and the code still behaves safely.
  3. Stable bucketing: the same user ID evaluated 1,000 times gets the same answer, and roughly the right share of a large set of IDs falls into the rollout.

Log exposures, not just evaluations

For experiment flags, you need to know which users actually saw each variant. Generated code tends to log every evaluation, which inflates counts and can include users who never reached the feature. Ask the model to log an exposure event at the point where the variant changes what the user sees, once per user per session, with the flag key and variant. Keep personal data out of that event; a user ID is usually enough.

A prompt template you can reuse

Add a feature flag to this code.
- Flag key: <key>
- Type: release / kill switch / experiment
- Audience and rollout plan: <who, in what order>
- Default if evaluation fails: <on/off>, because <reason>
- Owner and removal date: <team, date>
- SDK and an existing call from our codebase: <paste>
Constraints: no network call per evaluation, evaluate once per request,
sticky bucketing by <user id field>.
Write tests that run with the flag on and off, plus tests for
service failure and unknown flag.

Checklist before you merge

  • The flag type, owner, and removal date are written down.
  • The default is explicit, lives in one place, and matches the flag type.
  • Flag names are checked at compile time or by a lint rule.
  • Evaluation does not block on the network, and startup has a timeout.
  • Rollouts hash a stable ID plus the flag key.
  • Tests cover both paths and the failure cases.
  • There is a ticket to remove the flag after rollout.

LLMs are good at the typing part of feature flags: wrappers, tests, and removal diffs. The decisions that keep flags safe, such as the default, the lifetime, and who cleans up, still have to come from you. Put those decisions in the prompt, and the generated code gets much closer to something you can trust in production.

Comments

Popular posts from this blog

Grok Bot - a step closer to AGI

Tencent Hy4 Preview: Open 770B MoE Built for Real Work

Structured Outputs and Constrained Decoding for Reliable LLM APIs in Production (September 2026)