Flags SDK and Vercel Flags expert guidance. Use when declaring feature flags with flag(), wiring provider adapters (Vercel, Statsig, LaunchDarkly, PostHog, and others), managing flags with the vercel flags CLI, implementing the precompute pattern for static A/B tests, or integrating the Flags Explorer.
Installer avec Codex ou Claude Copiez ce prompt, collez-le dans Codex, Claude ou un autre assistant, puis laissez-le vérifier la page du skill et l'installer pour vous.
Une commande directe contourne le prompt de vérification. Examinez la source avant de l'exécuter.
La commande reste sur une seule ligne. Faites défiler horizontalement pour la vérifier avant de la copier.
Vous préférez une copie locale ? Téléchargez les fichiers actuellement disponibles dans SkillsMP.
Explorateur de fichiers
11 fichiers
Affichage de SKILL.md
SKILL.md
Instructions source · Aperçu en lecture seule
name
flags-sdk
description
Flags SDK and Vercel Flags expert guidance. Use when declaring feature flags with flag(), wiring provider adapters (Vercel, Statsig, LaunchDarkly, PostHog, and others), managing flags with the vercel flags CLI, implementing the precompute pattern for static A/B tests, or integrating the Flags Explorer.
summary
Flags SDK guidance — declare flags with flag(), connect provider adapters, manage Vercel Flags via the vercel flags CLI, precompute static variants, and set up the Flags Explorer.
{"aliases":["feature flags","feature toggles","vercel flags","flags sdk","a/b testing"],"intents":["add a feature flag","run an a/b test","gate a feature","roll out gradually","manage flags from cli"],"entities":["Flags SDK","Vercel Flags","Flags Explorer","vercelAdapter","precompute","FLAGS_SECRET"]}
chainTo
[{"pattern":"precompute\\s*\\(|generatePermutations|flags/next.*precompute","targetSkill":"routing-middleware","message":"Precompute pattern detected — loading Routing Middleware guidance for the middleware rewrites that serve static flag variants."},{"pattern":"@flags-sdk/(edge|global)-config|create(Edge|Global)ConfigAdapter","targetSkill":"vercel-storage","message":"Global Config flag adapter detected — loading Vercel storage guidance for Global Config setup and limits."}]
Flags SDK
The Flags SDK (flags npm package) is a feature flags toolkit for Next.js and SvelteKit. It turns each feature flag into a callable function, works with any flag provider via adapters, and keeps pages static using the precompute pattern. Vercel Flags is the first-party provider, letting you manage flags from the Vercel dashboard or the vercel flags CLI.
Each flag is declared as a function. No string keys at call sites:
import { flag } from'flags/next';
exportconst exampleFlag = flag({
key: 'example-flag',
decide() { returnfalse; },
});
const value = awaitexampleFlag();
Server-side evaluation
Flags evaluate server-side to avoid layout shift, keep pages static, and maintain confidentiality. Combine routing middleware with the precompute pattern to serve static variants from CDN.
Adapter pattern
Adapters replace decide and origin on a flag declaration, connecting your flags to a provider. Vercel Flags (@flags-sdk/vercel) is the first-party adapter. Third-party adapters are available for Statsig, LaunchDarkly, PostHog, and others.
Version note: The SDK is published as flags (renamed from @vercel/flags; that old name still appears in changelog history). flags 4.2.0+ accepts the adapter factory by reference (adapter: vercelAdapter) and resolves it once per declaration. Older versions require calling it (adapter: vercelAdapter()). The called form still works on new versions, so prefer the shorthand unless you're targeting flags < 4.2.0.
Agent workflow: Creating a new flag
When a user asks you to create or add a feature flag, follow these steps in order. Do not leave CLI steps as "next steps" for the user — execute them yourself.
Before you start
Check the project state to adapt commands and decide which steps you can skip:
Which lockfile is present (pnpm-lock.yaml, package-lock.json, yarn.lock, bun.lockb)? → Adapt all package manager commands accordingly (pnpm add, npm install, yarn add, bun add).
Is flags in package.json? → Skip install (step 1)
Does .vercel/ directory exist? → Project is linked, skip vercel link in step 2
Does .env.local contain FLAGS=? → Env vars already pulled, skip step 3
Does flags.ts (or lib/flags.ts, src/flags.ts) exist? → Add to it rather than creating from scratch (step 4)
Is @vercel/toolbar in package.json? → Skip toolbar setup (step 6)
Does app/.well-known/vercel/flags/route.ts exist? → Flags Explorer already set up, skip step 7
Steps
Install packages (if not already in package.json):
pnpm i flags @flags-sdk/vercel
Register the flag with Vercel: Run vercel flags add <flag-key> --kind boolean --description "<description>".
Prerequisite: The vercel flags commands require the Vercel CLI to be installed and authenticated. If the Vercel CLI is not installed, run pnpm i -g vercel. For authentication or linking issues, follow the vercel-cli skill if it's already available. If it isn't, suggest the user install it with npx skills add vercel/vercel@vercel-cli (don't auto-install it) and rely on vercel <command> --help in the meantime.
Before running vercel flags add, verify the project is linked to Vercel. Check for a .vercel directory in the project root. If it doesn't exist, run vercel link first.
Pull environment variables: Run vercel env pull to write FLAGS and FLAGS_SECRET to .env.local. Without these environment variables, vercelAdapter will not be able to evaluate flags. This step is mandatory after creating a flag.
Declare the flag in code: Add it to flags.ts (or create the file if it doesn't exist) using vercelAdapter:
Set up Flags Explorer (if not already present): Create app/.well-known/vercel/flags/route.ts — see the Flags Explorer setup section below.
Vercel Flags
Vercel Flags is Vercel's feature flags platform. You create and manage flags from the Vercel dashboard or the vercel flags CLI, then connect them to your code with the @flags-sdk/vercel adapter. When you create a flag in Vercel, the FLAGS and FLAGS_SECRET environment variables are configured automatically.
To create a flag end-to-end, follow the Agent workflow above.
For the full Vercel provider reference — user targeting, vercel flags CLI subcommands, custom adapter configuration, and Flags Explorer setup — see references/providers.md.
Declaring flags
When using Vercel Flags, declare flags with vercelAdapter as shown in the Agent workflow. For other providers, see references/providers.md. Below are the general flag() patterns.
To evaluate multiple flags at once, call evaluate() (from flags/next) instead of awaiting flags one at a time or using Promise.all(). To evaluate a single flag, just call it: await myFlag().
import { evaluate } from'flags/next';
import { flagA, flagB } from'../flags';
// avoid: each await blocks the next, so the flags resolve sequentiallyconst a = awaitflagA();
const b = awaitflagB();
// avoid: parallel, but each flag is evaluated in isolationconst [a, b] = awaitPromise.all([flagA(), flagB()]);
// prefer: shares work across the batchconst [a, b] = awaitevaluate([flagA, flagB]);
evaluate() is faster than both approaches. Awaiting flags one at a time makes total latency the sum of every flag's evaluation instead of the slowest single flag, while Promise.all() runs them in parallel but evaluates each in isolation. evaluate() pre-reads headers, cookies, and overrides once for the whole batch and lets adapters resolve a group in a single call, which reduces the number of parallel promises the runtime manages and leaves less room for the async work to be interrupted by other microtasks.
It accepts either an array (positional results) or an object (keyed results):
const [a, b] = awaitevaluate([flagA, flagB]);
const { a, b } = awaitevaluate({ a: flagA, b: flagB });
Outside App Router (Pages Router getServerSideProps/API routes, or routing middleware), pass the request as the second argument: await evaluate([flagA, flagB], request).
evaluate() always evaluates flags at request time. It is not for reading precomputed (static) values — for those, use getPrecomputed (or call the flag with the code, await myFlag(code, flagGroup)).
Adapters can opt into batching by implementing the optional bulkDecide hook. The Vercel adapter (@flags-sdk/vercel) implements it — roughly a 10x reduction in evaluation time when resolving hundreds of flags. See references/providers.md — Custom Adapters for implementing bulkDecide, and references/api.md — evaluate for the full signature.
When using a third-party provider alongside Vercel Flags, combine their data with mergeProviderData. Each provider adapter exports its own getProviderData — see the provider-specific examples in references/providers.md.
Use a separate FLAGS_SECRET value for each environment (Development, Preview, Production), and mark the Preview and Production values as Sensitive. Run the generator once per environment to produce distinct values, then store each on Vercel:
Use precompute to keep pages static while using feature flags. Middleware evaluates flags and encodes results into the URL via rewrite. The page reads precomputed values instead of re-evaluating.
High-level flow:
Declare flags and group them in an array
Call precompute(flagGroup) in middleware, get a code string
Rewrite request to /${code}/original-path
Page reads flag values from code: await myFlag(code, flagGroup)
For full implementation details, see framework-specific references:
Next.js: See references/nextjs.md — covers proxy middleware, precompute setup, ISR, generatePermutations, multiple groups
Create an adapter factory that returns an object with origin and decide. For the full pattern (including default adapter and singleton client examples), see references/providers.md.
Encryption functions
For keeping flag data confidential in the browser (used by Flags Explorer):
import { FlagValues, FlagDefinitions } from'flags/react';
// Renders script tag with flag values for Flags Explorer<FlagValuesvalues={{myFlag:true }} />// Renders script tag with flag definitions for Flags Explorer<FlagDefinitionsdefinitions={{myFlag: { options: [...], description: '...' } }} />
References
Detailed framework and provider guides are in separate files to keep context lean: