Tips & Tricks (अपडेट: 2/6/2026)

Claude Code से Feature Flags सुरक्षित तरीके से लागू करें: rollout, experiment और kill switch

Claude Code के साथ feature flags की व्यावहारिक गाइड: rollout, experiment, kill switch, targeting, metrics और cleanup.

Claude Code से Feature Flags सुरक्षित तरीके से लागू करें: rollout, experiment और kill switch

Toggle से पहले operating rule तय करें

Feature flag runtime switch है: code deploy हो सकता है, लेकिन feature बाद में चालू, बंद या धीरे-धीरे rollout किया जा सकता है। शुरुआती गलती if (flag) लिखने में नहीं होती। असली गलती release flag, experiment flag और kill switch को एक जैसा समझने में होती है। इनकी उम्र, owner, metric और cleanup rule अलग होते हैं।

Claude Code UI branch जल्दी बना देता है, पर production implementation को safe default, targeting context, server/client boundary, exposure log, guardrail metrics, rollback steps और temporary flags की removal date चाहिए। Masa के content site और छोटे SaaS workflows में सबसे उपयोगी prompt यही रहा: “कौन सा हिस्सा fail हो सकता है, क्या बंद करना है, और rollout healthy है या नहीं यह कैसे मापेंगे?”

Design को primary docs से मिलाकर रखें। OpenFeature application-facing evaluation API और पीछे के provider को अलग रखता है, और evaluation context से user, app और environment data पास करता है। LaunchDarkly release flags, experiment flags और kill switches जैसे use cases समझाता है। Unleash lifecycle को Define, Develop, Production, Cleanup और Archived states में रखता है ताकि stale flags code में न पड़े रहें। Claude Code docs भी clear verification path देने की सलाह देते हैं।

Primary references:

Release, experiment और kill switch अलग रखें

Code लिखने से पहले flag की lifetime तय करें। Release flag अधूरी capability छिपाती है, पहले छोटी audience तक जाती है, फिर 100% rollout के बाद हट जाती है। Experiment flag hypothesis test करती है, इसलिए exposure और outcome tracking जरूरी है। Kill switch लंबी उम्र वाला safety control है, जैसे vendor outage, cost spike, slow recommendation service या risky automation को तुरंत बंद करना।

Product use caseTypeSuccess metricFailure action
नया checkout Pro accounts के 25% तक rolloutReleaseCheckout completion, payment errorscheckout_v2_release off
Pricing page CTA copy compare करनाExperimentSignup start, paid-intent clicksExperiment stop, control serve
Blog affiliate block को article body में लानाExperimentProduct clicks, read completionBlock footer में वापस
Vendor incident में recommendations बंद करनाKill switchp95 latency, 5xx raterecommendations_enabled off

Measurement के बिना flag सिर्फ अनुमान है। Related reading के लिए Claude Code A/B testing और Claude Code analytics implementation देखें। Monetized content site में CTA केवल click नहीं है; AdSense quality, read completion, affiliate revenue और consultation intent भी बचाने हैं। अकेले शुरू करने के लिए free resource लें; team rollout के लिए English consultation page उपयोगी है।

Minimal config और evaluator pattern

शुरुआत में business logic को किसी एक vendor से tightly bind न करें। App को key, default value और context से flag evaluate करनी चाहिए; बाद में provider LaunchDarkly, Unleash, OpenFeature, JSON या internal service हो सकता है। नीचे का example flag-demo.ts में paste करके npx tsx flag-demo.ts से चलाया जा सकता है।

type FlagValue = boolean | string | number;
type FlagKind = "release" | "experiment" | "kill_switch";
type Plan = "free" | "pro" | "enterprise";
type Role = "user" | "admin";
type Operator = "equals" | "in";

type FlagContext = {
  targetingKey: string;
  plan: Plan;
  country: string;
  role: Role;
  appVersion: string;
};

type FlagRule = {
  attribute: keyof Omit<FlagContext, "targetingKey">;
  operator: Operator;
  values: string[];
  value: FlagValue;
  percentage?: number;
};

type FlagConfig = {
  key: string;
  kind: FlagKind;
  enabled: boolean;
  defaultValue: FlagValue;
  offValue: FlagValue;
  owner: string;
  removeAfter?: string;
  rules: FlagRule[];
};

const registry: Record<string, FlagConfig> = {
  checkout_v2_release: {
    key: "checkout_v2_release",
    kind: "release",
    enabled: true,
    defaultValue: false,
    offValue: false,
    owner: "growth-platform",
    removeAfter: "2026-07-15",
    rules: [
      {
        attribute: "role",
        operator: "equals",
        values: ["admin"],
        value: true,
      },
      {
        attribute: "plan",
        operator: "in",
        values: ["pro", "enterprise"],
        value: true,
        percentage: 25,
      },
    ],
  },
  pricing_copy_2026_06: {
    key: "pricing_copy_2026_06",
    kind: "experiment",
    enabled: true,
    defaultValue: "control",
    offValue: "control",
    owner: "monetization",
    removeAfter: "2026-06-30",
    rules: [
      {
        attribute: "country",
        operator: "in",
        values: ["JP", "US", "DE"],
        value: "simple",
        percentage: 50,
      },
    ],
  },
  recommendations_enabled: {
    key: "recommendations_enabled",
    kind: "kill_switch",
    enabled: true,
    defaultValue: true,
    offValue: false,
    owner: "sre",
    rules: [],
  },
};

function bucketFor(flagKey: string, targetingKey: string): number {
  const input = `${flagKey}:${targetingKey}`;
  let hash = 0;

  for (const char of input) {
    hash = (hash * 31 + char.charCodeAt(0)) >>> 0;
  }

  return hash % 100;
}

function ruleMatches(
  flagKey: string,
  rule: FlagRule,
  context: FlagContext,
): boolean {
  const actual = String(context[rule.attribute]);
  const matched =
    rule.operator === "equals"
      ? actual === rule.values[0]
      : rule.values.includes(actual);

  if (!matched) return false;
  if (rule.percentage === undefined) return true;

  return bucketFor(flagKey, context.targetingKey) < rule.percentage;
}

export function evaluateFlag<T extends FlagValue = FlagValue>(
  key: string,
  context: FlagContext,
): T {
  const flag = registry[key];
  if (!flag) return false as T;
  if (!flag.enabled) return flag.offValue as T;

  for (const rule of flag.rules) {
    if (ruleMatches(flag.key, rule, context)) {
      return rule.value as T;
    }
  }

  return flag.defaultValue as T;
}

const demoContexts: FlagContext[] = [
  {
    targetingKey: "user_001",
    plan: "pro",
    country: "JP",
    role: "user",
    appVersion: "1.8.0",
  },
  {
    targetingKey: "user_002",
    plan: "free",
    country: "BR",
    role: "admin",
    appVersion: "1.8.0",
  },
];

for (const context of demoContexts) {
  console.log(context.targetingKey, {
    checkout: evaluateFlag<boolean>("checkout_v2_release", context),
    pricingCopy: evaluateFlag<string>("pricing_copy_2026_06", context),
    recommendations: evaluateFlag<boolean>(
      "recommendations_enabled",
      context,
    ),
  });
}

Important points सरल हैं। Unknown flag safe fallback पर जाती है। Percentage rollout stable targetingKey से होता है, Math.random() से नहीं। Temporary flags में owner और removeAfter होना चाहिए। Registry बाद में control plane में जा सकती है, app contract वही रहता है।

Server evaluation और client display अलग करें

Billing, authorization, quota, inventory या backend cost से जुड़ी चीज server पर evaluate करें। Client flags केवल already-authorized UI copy, layout, onboarding hints या low-risk visual changes के लिए ठीक हैं। Secret targeting rules browser में न भेजें और hidden button को access control न मानें।

type User = {
  id: string;
  plan: "free" | "pro" | "enterprise";
  role: "user" | "admin";
};

type RequestLike = {
  headers: {
    get(name: string): string | null;
  };
};

export function buildFlagContext(
  user: User,
  request: RequestLike,
): FlagContext {
  return {
    targetingKey: user.id,
    plan: user.plan,
    role: user.role,
    country: request.headers.get("x-country") ?? "US",
    appVersion: process.env.NEXT_PUBLIC_APP_VERSION ?? "dev",
  };
}

export function getServerFlagSnapshot(context: FlagContext) {
  return {
    checkoutV2: evaluateFlag<boolean>("checkout_v2_release", context),
    pricingCopy: evaluateFlag<string>("pricing_copy_2026_06", context),
  };
}
type PricingFlags = {
  pricingCopy: string;
};

export function PricingCta({ flags }: { flags: PricingFlags }) {
  const label =
    flags.pricingCopy === "simple"
      ? "Free plan से शुरू करें"
      : "Free trial शुरू करें";

  return <a href="/signup">{label}</a>;
}

React component केवल server से मिले snapshot को दिखाता है। Claude Code को prompt में साफ लिखें कि permission और billing server-side रहें, client सिर्फ evaluated result consume करे।

Observability के साथ rollout करें

Safe rollout सिर्फ “1% से शुरू करो” नहीं है। Ramp schedule, metrics और rollback threshold पहले से तय होने चाहिए। Unleash gradual rollout में percentage, stickiness और constraints मिलते हैं। LaunchDarkly guarded rollouts metrics से rollout को जोड़ते हैं और regression पर pause या rollback कर सकते हैं। छोटा internal evaluator हो तब भी यही operating model अपनाएं।

तीन layers track करें: exposure, primary metric और guardrails। Exposure बताता है किसने कौन सा flag value देखा। Primary metric बताता है desired behavior सुधरा या नहीं। Guardrails बताते हैं speed, errors, revenue quality, support load या trust खराब तो नहीं हुआ।

type FlagExposure = {
  flagKey: string;
  value: FlagValue;
  targetingKey: string;
  route: string;
  evaluatedAt: string;
};

export function trackFlagExposure(event: FlagExposure) {
  console.log(
    JSON.stringify({
      event_name: "feature_flag_exposure",
      ...event,
    }),
  );
}

Checkout में 5xx, payment failure और support tickets देखें। Monetized blog में affiliate clicks के साथ read completion, bounce, Core Web Vitals और paid-intent clicks भी देखें। AI feature में token cost, p95 latency और per-user quota देखें। Clicks बढ़े लेकिन buyer quality गिरी, तो वह जीत नहीं है।

Concrete failure modes

पहला failure है हर page load पर random assignment। User refresh करे और A से B बदल जाए तो exposure और conversion data खराब हो जाता है। Stable targeting key का bucket बनाएं।

दूसरा failure है premium feature को केवल client में छिपाना। React button गायब होना API protection नहीं है। Feature flag UX switch है, authorization नहीं।

तीसरा failure unsafe default है। Unknown release flag आम तौर पर false return करनी चाहिए। Typo से true मिला तो accidental launch हो गया।

चौथा failure temporary flags न हटाना है। कुछ महीनों बाद checkout_v2_release mystery branch बन जाती है। Decision के बाद cleanup PR खोलें।

पांचवा failure बहुत nested rules हैं। Parent flag, child flag और overlapping percentage rollout से असली audience समझाना मुश्किल हो जाता है।

Claude Code के लिए safe prompts

Claude Code files पढ़ सकता है, code बदल सकता है और tests चला सकता है। इसलिए scope और verification शुरू में दें।

इस repository में feature flag workflow जोड़ें।
पहला flag checkout_v2_release है, staged rollout के लिए।

Constraints:
- Billing और authorization flags server पर evaluate हों।
- Unknown release flags false return करें।
- Percentage rollout stable targetingKey इस्तेमाल करे।
- Registry में owner और removeAfter शामिल हों।
- Unrelated files modify न करें।

Required output:
- Minimal flag registry और evaluateFlag function
- Exposure event type
- कम से कम 3 product use cases
- Failure examples और rollback steps
- चलाए गए test commands

Merge से पहले review prompt:

इस feature flag implementation को review करें।
Defaults, server/client boundary, stable bucketing,
missing exposure events, cleanup dates और rollback behavior देखें।
Severity order में exact files के साथ findings दें।

ऐसे prompts Claude Code को केवल decorative toggle से हटाकर maintainable operating system की ओर ले जाते हैं।

Cleanup भी shipping का हिस्सा है

हर flag बनते ही maintenance cost बनती है। Release flags full rollout के बाद हटें। Experiment flags winner चुनने के बाद हटें। Kill switches रह सकते हैं, लेकिन owner, runbook और alerts चाहिए। PR template में owner, removeAfter, metrics और planned removal PR डालें।

इस article के evaluator को runnable TypeScript demo के रूप में verify किया गया: वही targetingKey वही bucket देता है, unknown flags safe fallback देती हैं, और kill switch का explicit off value है। Masa की monetized content note है कि clicks के साथ quality भी measure करें। Full platform से पहले एक release flag, एक experiment flag और एक kill switch से शुरुआत करें।

#Claude Code #Feature Flags #सुरक्षित rollout #TypeScript #Observability
मुफ़्त

मुफ़्त PDF: Claude Code cheatsheet

Email डालें और commands, review habits तथा safe workflow वाली एक-page PDF पाएँ.

हम आपका data सुरक्षित रखते हैं और spam नहीं भेजते.

Masa

लेखक के बारे में

Masa

Claude Code workflow और team adoption पर काम करने वाला engineer.