Feature Flags
Toggle AI features and prompt variants without redeploying code.
In plain words
A feature flag is an on-off switch for a feature that you can flip while the app is running, so you can test with a few people and turn things off instantly.
Think of it like…
Circuit breakers and light switches in a building: you can turn individual circuits on, dim them or cut them off at the panel without rewiring, but a panel with hundreds of unlabelled switches becomes dangerous.
Where you’ve seen it
A team ships a new reranker behind the flag rag.reranker_v2. Percentage rollout uses stable bucketing: bucket = hash(flag_key + user_id) mod 10,000, and the user is in the rollout if bucket < percentage × 100, so 5% means bucket < 500; including the flag key in the hash keeps different flags' cohorts independent, and the same user always gets the same result. Rules target internal staff first, then 5% of free-plan users, then 25%, then everyone, and a kill switch flag llm.disable_tools can instantly turn off tool calling during a provider incident. A prompt experiment prompt.support_v15 assigns users 50/50 and logs an exposure event (user, flag, variant, time) to analytics. The SDK caches rules locally and streams updates, so evaluation adds microseconds; if the flag service is unreachable, each flag falls back to a documented safe default (new features off, kill switches off) so an outage doesn't change behaviour unexpectedly. With 20 boolean flags there are 2²⁰ = 1,048,576 possible combinations, so the team tests the default state plus each flag flipped individually (21 runs) and only the known-interacting pairs. Flags have owners and expiry dates, a quarterly cleanup removes flags fully rolled out for 30 days and every change is audit-logged.
Words you’ll hear
- Stable bucketing
- Deterministic hashing so a user always lands in the same rollout cohort.
- Kill switch
- A flag that disables a feature or dependency immediately.
- Exposure event
- A logged record that a user saw a variant, needed for experiment analysis.
- Flag debt
- Accumulated stale flags that increase complexity and risk.
Visual diagram
Theory & deep dive
Flag types, evaluation and targeting
Common types: release flags (gradual rollout of a feature, short-lived), experiment flags (variants assigned for A/B tests, with exposure logging), ops flags and kill switches (disable a dependency or feature during incidents: long-lived), permission or entitlement flags (plan-based access, longer-lived) and config-style flags (multivariate values such as a model ID or top-k). Evaluate flags locally in the SDK from a cached ruleset (updated by streaming or polling) rather than calling a remote service on each request, so latency is microseconds and the flag service isn't on the critical path. Provide the context (user id, tenant, plan, region, app version) and apply targeting rules in order: explicit overrides, segments, percentage rollouts, default.
Percentage rollouts must be stable and independent: compute bucket = hash(flag_key + user_id) mod 10,000 and enable when bucket < percentage × 100 (5% → bucket < 500). The same user always gets the same answer, increasing the percentage only adds users (the first 5% stay in at 25%) and including the flag key means two flags' 5% cohorts aren't the same people. For anonymous traffic use a persistent anonymous id; for tenant-level features hash the tenant id. Log an exposure event when a variant is actually used (not when evaluated) so analysis compares users who really saw each variant, and tag AI traces with the active variants for debugging.
user_id = 8841, flag = rag.reranker_v2 → hash mod 10,000 = 312 → 312 < 500, so in the 5% rollout. Raise to 25% (bucket < 2,500): still on. For flag prompt.support_v15 the same user hashes to 7,206: a different cohort.
Operating flags safely in AI systems
Safe defaults and failure modes: decide what each flag does when the service is unreachable or the SDK has no ruleset (new features off, kill switches off, entitlements per policy) and test that behaviour. Keep a kill switch for each risky dependency or capability (tools, a provider, a model route) with an owner and a runbook, and make sure flipping it needs no deploy. Testing: n boolean flags create 2ⁿ states (20 flags: 1,048,576), so test the default configuration, each flag flipped alone (21 runs) and explicitly known interactions; avoid flags that depend on each other, and keep the number of active flags small. For AI variants, run the golden set against each variant in CI so a flag can't enable an untested prompt.
Lifecycle and governance: every flag has an owner, a purpose, a creation date and planned removal date; short-lived flags are deleted once at 100% for a set period (30 days), after which the code path is simplified (flag debt makes code hard to reason about and can resurrect old behaviour). Restrict who can change production flags, require approval for sensitive ones, keep an audit log and notify on changes. Don't use flags as a secrets store or put sensitive data in flag payloads. Separate flags (runtime toggles) from configuration (deploy-time settings) and from experiments (statistical analysis requires randomisation, sample size and exposure data; see canary and A/B lessons). Monitor flag evaluation errors and cache staleness, and include flag state in incident timelines.
A provider starts returning malformed tool calls. The on-call engineer sets llm.disable_tools = true for all tenants; within seconds the assistant falls back to answering without tools and posting a notice. After the provider recovers, the switch is turned off and a post-incident review adds a canary check.
How it works, step by step
Step 1 of 41. Define flags with owners
Name each flag by area and purpose, give it a type, owner, default and expiry or removal plan.
- Types
- release, ops, experiment
When to use it & trade-offs
What it is
Feature flags (toggles) are runtime switches that turn behaviour on or off, or select among variants, without redeploying code. For AI products they control new AI features, prompt variants, model routes and fallbacks, retrieval settings, safety thresholds, tool availability and kill switches that disable a misbehaving capability instantly. A flag platform stores rules (percentage rollouts, targeting by user, tenant, plan or region), SDKs evaluate them locally from cached rules for low latency and the system records exposure events so experiments can be analysed. Good flag hygiene covers safe defaults, naming and ownership, testing, audit logs and removing flags once they've served their purpose.
Why it matters: Flags decouple deploy from release: you can ship code dark, enable it for 1% of users, expand gradually and switch it off in seconds if the model starts producing bad output. AI behaviour is non-deterministic and provider incidents are common, so instant, targeted control is a practical safety net. The cost is complexity: every flag multiplies the states to test and stale flags become tech debt and risk, so discipline matters.
When to reach for it
- Gradual rollout of AI features and prompt variants
- Kill switches for tools, models and providers
- Plan- or tenant-based entitlements
When to avoid or be careful
- Leaving flags in the code long after rollout
- Evaluating flags with a network call on every request
- Putting secrets or PII in flag payloads
Flags versus related mechanisms
Strengths: No redeploy; targeted; instant control.
Watch out: State explosion and flag debt.
Strengths: Simple; versioned with the release.
Watch out: Changes require redeploys.
Strengths: No runtime branching.
Watch out: Slow; no gradual exposure.
Interview signals & numbers to know
When this topic comes up
- “Turn it off without a deploy”
- Percentage rollout
- Kill switch
- Flag debt
- Exposure events
Benchmarks & rules of thumb
- bucket < 500
- 5% rollout in a 10,000-bucket scheme
- 2²⁰ = 1,048,576
- states for 20 boolean flags
- 21
- test runs: default plus each flag flipped alone
Common mistakes & follow-ups
Common interview mistakes
- Using the same hash for every flag, making cohorts identical.
- Defaulting new features on when the flag service is down.
- Never removing fully rolled-out flags.
Likely interviewer follow-ups
- How would you roll out a new prompt to 5% of users?
- What should happen when the flag service is unavailable?
- How do you avoid flag debt?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Feature Flags with generative AI interview Q&A and hands-on practice.