Feature flags

From InugamiDev/ultrathink-oss · View interactive version

Feature flags is an AI agent skill from the GitHub repository InugamiDev/ultrathink-oss. Feature flags patterns including LaunchDarkly, Statsig, Vercel Edge Config, environment-based toggles, gradual rollouts, and user targeting

How to use this skill

Skill file

Feature Flags Skill

Purpose

Feature flags decouple deployment from release. Ship code to production behind flags, then control visibility through configuration rather than code changes. This skill covers provider-based and DIY flag systems with safe rollout patterns.

Key Patterns

Strategy Comparison

| Approach | Best For | Latency | Cost |

|----------|----------|---------|------|

| Environment variables | Simple on/off per environment | Zero | Free |

| Vercel Edge Config | Edge-fast reads, small flag sets | ~1ms | Free tier available |

| LaunchDarkly | Enterprise targeting, analytics | ~20ms | Paid |

| Statsig | A/B testing + flags combined | ~15ms | Free tier available |

| Database flags | Custom rules, full ownership | Varies | Self-hosted |

Environment-Based Toggles (Simplest)

// lib/flags.ts — zero-dependency flags
const flags = {
  newCheckout: process.env.NEXT_PUBLIC_FLAG_NEW_CHECKOUT === "true",
  betaDashboard: process.env.FLAG_BETA_DASHBOARD === "true",
} as const;

export type Flag = keyof typeof flags;
export function isEnabled(flag: Flag): boolean {
  return flags[flag];
}

// Usage in component
if (isEnabled("newCheckout")) {
  return <NewCheckoutFlow />;
}

Vercel Edge Config Flags

import { get } from "@vercel/edge-config";

export async function getFlag(key: string): Promise<boolean> {
  const value = await get<boolean>(key);
  return value ?? false;
}

// In Server Component
export default async function Page() {
  const showBanner = await getFlag("promo-banner");
  return showBanner ? <PromoBanner /> : null;
}

// In Middleware (edge-fast)
import { NextRequest, NextResponse } from "next/server";
import { get } from "@vercel/edge-config";

export async function middleware(req: NextRequest) {
  const maintenanceMode = await get<boolean>("maintenance");
  if (maintenanceMode) {
    return NextResponse.rewrite(new URL("/maintenance", req.url));
  }
  return NextResponse.next();
}

Gradual Rollout with User Targeting

interface FlagRule {
  enabled: boolean;
  rolloutPercent: number;
  allowlist: string[];
  denylist: string[];
}

function evaluateFlag(rule: FlagRule, userId: string): boolean {
  if (!rule.enabled) return false;
  if (rule.denylist.includes(userId)) return false;
  if (rule.allowlist.includes(userId)) return true;

  // Deterministic hash for consistent assignment
  const hash = simpleHash(userId) % 100;
  return hash < rule.rolloutPercent;
}

function simpleHash(str: string): number {
  let hash = 0;
  for (const char of str) {
    hash = (hash * 31 + char.charCodeAt(0)) >>> 0;
  }
  return hash;
}

LaunchDarkly Integration

import { init, LDClient } from "@launchdarkly/node-server-sdk";

let ldClient: LDClient;

export async function getLDClient() {
  if (!ldClient) {
    ldClient = init(process.env.LAUNCHDARKLY_SDK_KEY!);
    await ldClient.waitForInitialization();
  }
  return ldClient;
}

export async function getFlag(key: string, user: { key: string; email?: string }) {
  const client = await getLDClient();
  return client.variation(key, user, false);
}

Best Practices

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.