Experiments

How to safely ship AI features that can hallucinate

A graphic of a bar chart with an arrow pointing upward.

Every LLM you ship will hallucinate.

Not occasionally, not in edge cases — structurally, as a property of how probabilistic text generation works). The question isn't whether your AI feature will produce a confidently wrong output. It's whether your deployment architecture will catch it before it reaches users at scale.

This article is for engineers, PMs, and data teams who are actively building or shipping AI features and need a concrete safety architecture — not model theory. It walks through four layers of protection, in the order you should build them:

  • Guardrail metrics: how to define the signals that detect hallucination damage before it shows up in business KPIs
  • Staged rollouts: how to use feature flags and percentage ramps to limit how many users a bad output can reach
  • Human review and deterministic fallbacks: how to build review gates that prevent high-stakes outputs from reaching users unverified
  • Continuous monitoring: how to detect hallucination drift weeks after launch, when the model hasn't changed but the outputs have

Each section covers the specific implementation decisions — thresholds, ramp schedules, flag configurations, metric types — that separate teams who ship AI features safely from teams who find out something went wrong from a customer complaint.

The article is structured sequentially: pre-launch instrumentation first, rollout controls second, review architecture third, and post-launch monitoring last.

Why hallucinations are a deployment problem, not just a model problem

There's a comfortable assumption embedded in how most teams approach AI feature development: that hallucinations are a model quality issue, and that the next version of the model will be better. This assumption is wrong, and building a deployment strategy around it is how teams end up with fabricated information reaching users at scale.

The more accurate framing — and the one that should drive every architectural decision you make before shipping an AI feature — is that hallucinations are a systemic, expected failure mode of large language models. Not a defect. Not an edge case. An expected output of a system that generates probabilistic text rather than retrieving verified facts.

Hallucinations in production don't look broken — that's what makes them dangerous

The working definition matters here. A hallucination is not a generic AI error or a system failure — it's a response that contains false or misleading information presented as fact, delivered with the same confidence and fluency as accurate information.

IBM describes it as a phenomenon where an LLM "perceives patterns or objects that are nonexistent or imperceptible to human observers, creating outputs that are nonsensical or altogether inaccurate."

That last part is what makes hallucinations particularly dangerous in production: they don't look broken. A hallucinated shipping address, a fabricated policy citation, a confidently wrong medical recommendation — none of these trigger an error state. They pass through your system looking like valid output, and they reach users before any downstream signal tells you something went wrong.

IBM traces the structural causes to overfitting, training data bias, and model complexity. These are not defects in a specific model version that a patch can fix. They are properties of how LLMs are built. Symbolic AI systems generally do not produce hallucinations — but the moment you choose an LLM, you are choosing a system that structurally generates them.

Why this is an expected failure mode, not an edge case

Hallucinations have been a documented failure mode in probabilistic language systems since the 2000s, first appearing in statistical machine translation). The phenomenon predates the current generation of LLMs by decades, which should tell you something about how likely it is to be solved by the next model release.

There's an active debate in practitioner communities about whether "hallucination" is even a meaningful term — one framing holds that all LLM output is technically a hallucination, and we simply find some of it useful. That's a philosophical position worth understanding, but it's not operationally useful.

For the purposes of shipping AI features, hallucination means the specific case of confident fabrication: the model asserting something false as though it were true. That's the failure mode you need to engineer around.

Wikipedia's framing is instructive here: detecting and mitigating hallucinations poses significant challenges for) "practical deployment and reliability" of LLMs — not for model research. The research community has already located this problem in the deployment layer.

When deployment architecture is missing, the model takes all the blame

The evidence from organizations that have shipped AI features without adequate safeguards is consistent. When Google's Bard incorrectly claimed that the James Webb Space Telescope had captured the first images of an exoplanet, the model wasn't broken — it generated a plausible-sounding response to a query.

When Meta pulled its Galactica LLM demo after it produced inaccurate and sometimes prejudiced outputs, the model was doing what LLMs do. When Microsoft's Sydney chatbot produced deeply strange outputs about its own emotional states, the underlying system was functioning as designed.

In each case, the model was not the failure point. The missing layer was the deployment architecture that should have caught, contained, or flagged those outputs before they reached users.

IBM's healthcare example makes the stakes concrete: an AI model that misidentifies a benign skin lesion as malignant doesn't fail loudly — it produces a confident, well-formatted output that triggers unnecessary medical intervention.

The question isn't whether it will hallucinate — it's whether your architecture can contain it

If hallucinations are structural and expected, then the question changes. It's no longer "is this model accurate enough to ship?" It's "have we built the infrastructure to contain the damage when it hallucinates?" — and it will.

The causes IBM identifies (overfitting, training data bias, model complexity) are not things a product team controls. What you do control is the deployment architecture: the guardrail metrics that detect quality degradation, the staged rollout strategy that limits blast radius, the human review checkpoints that catch high-stakes outputs before delivery, and the monitoring systems that surface drift over time.

Those aren't optional safety theater. They're the engineering response to a known, structural property of the systems you're building on.

Every section that follows in this article is a specific implementation of that response.

Guardrail metrics must be defined before launch, not after the first incident

Most teams shipping AI features already have dashboards. They're tracking engagement, session length, conversion rates, support ticket volume. The problem is that none of those metrics are designed to catch a hallucination spike before it becomes a customer complaint.

By the time aggregate conversion drops, the damage is already distributed across thousands of sessions. You needed a signal three days ago.

Guardrail metrics are the instrumentation layer that closes that gap — but only if you define them before launch, not after something goes wrong.

Guardrail metrics hold a floor; standard KPIs raise a ceiling — they are not the same signal

The distinction matters. Standard goal metrics measure whether a feature is working as intended — whether it's moving the needle on the outcome you care about. Guardrail metrics measure whether the feature is not breaking something you can't afford to break. You're not trying to improve them. You're trying to ensure they don't regress.

GrowthBook's documentation frames it this way: guardrail metrics are ones "you want to keep an eye on, but aren't trying to specifically improve." Their job is to hold a floor, not raise a ceiling. If you're shipping an AI-powered product recommendation feature, your goal metric might be add-to-cart rate. Your guardrail metrics are the signals that would tell you the feature is actively causing harm — even if add-to-cart hasn't moved yet.

The gap between those two signal types is where hallucination damage accumulates. Standard KPIs measure aggregate outcomes. They don't surface output quality degradation until the degradation is widespread enough to move a business metric.

Research on AI safety monitoring suggests binary pass/fail checks miss the majority of safety-relevant failures — the kind of subtle, intermittent hallucinations that don't trigger hard errors but do erode user trust session by session.

The right guardrail metrics sit closest to output quality, not business outcomes

The right guardrail metrics for an AI feature are the ones that sit closest to output quality and user response. That means tracking signals like user correction rates (how often users edit or override AI-generated content), escalation rates (how often AI-handled interactions get kicked to a human), session abandonment immediately following an AI response, and downstream error rates in systems that consume AI outputs — like order management or fulfillment pipelines.

These are leading indicators. They surface hallucination-related degradation before it reaches conversion metrics or support queues. If users are abandoning sessions at an elevated rate right after your AI assistant responds, that's a quality signal, not a traffic anomaly.

At the model output level, faithfulness scores — like the 0–10 scale used in CrewAI's hallucination guardrail or the 0.0–1.0 accuracy scores in NVIDIA's NeMo framework — can feed into your rollout monitoring as upstream signals. They don't replace deployment-level guardrails, but they can serve as early warning inputs if you're logging them.

One practical caution: keep the guardrail metric set focused. Choosing too many guardrail metrics increases false positive rates and creates alert fatigue. Aim for a small number of metrics that are genuinely proximate to hallucination risk for your specific feature.

Breach thresholds should default to zero: any statistically certain harm is a failing rollout

This is where most teams overthink it. The right default threshold is zero: as soon as there is statistical certainty that a metric is being harmed at all — even by a small amount — the rollout should be marked as failing. There's no manual calibration of "how much harm is acceptable" for a regression direction.

The statistical method behind this matters. Safe Rollouts in GrowthBook use one-sided sequential testing. The practical benefit: you can check the data at any point during the rollout without the results becoming unreliable.

With a standard A/B test, checking results early and often inflates your false positive rate — you start seeing "significant" results that aren't real. Sequential testing is designed for continuous monitoring, so the guardrail analysis is valid whether you look at it on day one or day seven. You don't have to wait for the monitoring window to close before acting on a regression.

The Metric Boundary visualization shows the confidence interval bound over time. When that bound crosses zero, there is enough statistical certainty that the rollout is harming the metric. That's the trigger.

Wiring guardrails to automated rollback

Defining metrics and setting thresholds is only useful if a breach actually stops the rollout. The mechanism that closes that loop is automated rollback — and it needs to be configured before you start the rollout, not scrambled for after a guardrail fires.

GrowthBook's Safe Rollouts include an Auto Rollback toggle that automatically disables the rollout rule when a guardrail metric fails significantly. The rollout runs as a short-term comparison between the control value and the new AI feature value, with guardrail analysis running continuously throughout the monitoring period. The specific ramp mechanics — how the percentage exposure increases over time — are covered in the staged rollouts section below.

Guardrail metrics in this setup are pulled directly from your connected data warehouse. GrowthBook connects directly to your existing data warehouse — Snowflake, BigQuery, Redshift, Postgres, and more — running analysis without requiring a separate event pipeline. The guardrail metrics you're monitoring are the same data your business already uses — no instrumentation duplication, no reconciliation problem.

The status indicators to watch during a rollout are straightforward: "Guardrails Failing" means a regression has been detected and action is required; "Ready to Ship" means no regressions were found and the monitoring duration has completed; "No Data" after 24 hours means something is wrong with the setup and needs investigation before proceeding.

The point of all of this is that customer complaints are a terrible hallucination detection system. They're lagging, they're incomplete, and by the time they surface a pattern, the feature has already been live at scale for days. Pre-defined guardrail metrics with automated rollback are the mechanism that lets you ship AI features without treating your users as the monitoring layer.

Staged rollouts exist to contain hallucination blast radius before it reaches your full user base

When an AI customer service agent at an ecommerce brand directed a customer to ship three devices to a truck stop — because it had hallucinated a shipping address — the damage wasn't contained to a test environment. It happened in production, to a real customer, with real consequences.

In a separate incident at the same company, the agent told a customer it had already dispatched a replacement product. It hadn't. The customer service team only found out when the complaint escalated. The head of customer service's response was unambiguous: "I have zero confidence moving forward. I'm turning it off today."

Both incidents share a structural cause that has nothing to do with the underlying model. The AI feature was exposed to the full user population with no mechanism to limit who experienced a bad output, no automated signal that something had gone wrong, and no way to contain the damage before it scaled. That's the blast radius problem — and staged rollouts are the primary engineering response to it.

Why shipping to 100% of users is the highest-risk pattern for AI features

Traditional feature bugs tend to be deterministic. If a button is broken, it's broken for everyone, and it's reproducible. You can catch it in QA or in the first few minutes of a canary deploy. AI hallucinations don't work that way. They're probabilistic, input-dependent, and often only surface under specific conditions that don't appear in testing — but do appear at scale.

A hallucination that occurs in 0.5% of conversations is invisible in a 50-user beta. It becomes 500 incidents per day at 100,000 daily active users.

When you ship an AI feature to your entire user base simultaneously, you have no control group, no contained observation window, and no early signal before the damage is widespread. Hallucinations are "a runtime problem, a delivery problem, and a product trust problem" — meaning the harm happens in production, not in testing, and it compounds with every additional user exposed before you catch it.

A controlled exposure sequence turns a full launch into a contained observation window

The practical alternative is to treat every AI feature launch as a controlled exposure sequence. GrowthBook's Safe Rollouts implement a fixed ramp schedule: 10% → 25% → 50% → 75% → 100%, with the ramp completing within the first 25% of the configured monitoring window.

If you set a four-day monitoring duration, you reach full exposure by the end of day one, but you have three additional days of full-population monitoring with automated guardrail checks running against your defined metrics.

Before the percentage ramp even begins, targeting conditions let you restrict the rollout to a specific user segment — internal employees, beta users, or a lower-stakes account tier — evaluated against SDK attributes. This gives you a first gate that's entirely separate from the percentage progression: you're not just limiting volume, you're limiting which users are exposed at all.

The rollout runs as a short-term A/B test, with a control group receiving the existing experience and the rollout group receiving the AI feature, so you have a comparison baseline rather than just absolute numbers to reason about.

Feature flags as kill switches

The staged ramp is only half the mechanism. The other half is what happens when something goes wrong mid-rollout. GrowthBook's Auto Rollback toggle, configured per Safe Rollout rule, automatically disables the rollout rule when a guardrail metric crosses the significance threshold — no manual intervention required.

When it's disabled, teams retain manual control and can disable the flag themselves when they see a signal. Either way, the flag is the kill switch.

The contrast with the incidents described above is instructive. Those fabricated responses persisted until customer complaints escalated because there was no automated detection and no kill mechanism in place. The architecture assumed the AI would behave correctly, rather than assuming it might not and building accordingly.

Connecting rollout gates to guardrail metric thresholds

Safe Rollouts monitor guardrail metrics using one-sided sequential testing, which means rollback can trigger as soon as statistical significance is reached — not at the end of a fixed monitoring window. The failure threshold is always zero: any statistically certain harm to a monitored metric triggers a "Guardrails Failing" status.

The metrics themselves are pulled from your own data warehouse (Snowflake, BigQuery, Redshift, and others), so the same signals your analytics team already tracks — escalation rates, session abandonment, downstream error rates — become the automated gates controlling rollout progression.

This is where the guardrail metrics defined before launch do their actual work. They're not dashboards to check manually; they're the conditions under which the rollout either advances or stops. The staged rollout without connected guardrails is just a slower full launch. The guardrails without the staged rollout have no lever to pull. Together, they give teams the ability to catch a hallucination pattern in a 10% cohort and stop it before it reaches the other 90%.

Industry-wide, this pattern is becoming standard practice. LaunchDarkly's equivalent — Guarded Releases, which went GA in 2025 — and Statsig's Release Pipelines both implement similar ramp-with-guardrail logic. Teams not doing some version of this for AI feature launches are operating without a safety net that the rest of the industry is actively building in.

Human review checkpoints and deterministic fallbacks are what convert model errors into caught errors

The most common framing of production AI failures treats the model as the culprit. The model hallucinated, the model was wrong, the model needs to be improved. This framing is convenient but misleading.

When a hallucinated output reaches a user and causes harm, the model is rarely the only failure point — the architecture around the model is. Specifically, the absence of a review layer between AI output generation and user delivery is what converts a probabilistic error into a production incident.

Hallucinations are, by definition, convincingly plausible. Merriam-Webster defines an AI hallucination as "a plausible but false or misleading response generated by an artificial intelligence algorithm". IBM characterizes them as outputs that are "nonsensical or altogether inaccurate" yet presented as coherent. These outputs don't self-identify as errors. They don't trigger syntax failures or return HTTP 500 codes. They arrive looking correct, which is precisely why automated detection alone is insufficient and why architectural review checkpoints exist.

When human review is non-negotiable

Not every AI output carries the same risk profile. A hallucinated product tagline is embarrassing. The consequences escalate sharply from there: fulfillment failures follow from fabricated shipping addresses, and real financial harm follows from confidently wrong transactional instructions. The cost of a wrong output reaching a user determines how mandatory the review checkpoint is.

The Amazon Q case illustrates how quickly the stakes escalate. During a public preview, Amazon's generative AI assistant experienced severe hallucinations that exposed confidential internal data — including data center locations and internal discount programs.

The harm here wasn't a false fact about an external topic; it was the uncontrolled delivery of sensitive internal information to users who shouldn't have seen it. That's a security and compliance failure, not just an accuracy failure, and it happened because no review layer existed between the model's output and the user's screen.

The categories that warrant mandatory human review before delivery include: customer-facing financial or transactional instructions, shipping and fulfillment data, medical or health-related guidance, legal summaries, and any output that creates a binding or perceived commitment on behalf of the company. For these categories, the review checkpoint is not a quality enhancement — it's a liability control.

Confidence-score gating routes uncertain outputs away from users before delivery

The practical implementation of a review gate starts with a routing decision: does this output go directly to the user, or does it go to a review queue first? Confidence scoring provides the signal for that routing decision.

Most production LLM deployments can surface some proxy for output confidence — whether through model-returned log probabilities, a secondary classifier trained to flag uncertain outputs, or a consistency check that generates multiple responses and measures agreement. The specific mechanism matters less than the architectural principle: outputs below a defined confidence threshold should not be delivered directly. They should be routed to a human reviewer or to a deterministic fallback, depending on the output category and the latency constraints of the feature.

Threshold values shouldn't be universal. A high-stakes output category — shipping instructions, financial guidance — warrants a conservative threshold that routes more outputs to review. A lower-stakes category can tolerate a more permissive threshold. The threshold is a product decision with engineering consequences, and it should be set before launch, not tuned reactively after incidents.

When confidence gates fail and review isn't available, deterministic fallbacks are the last line

When an output fails the confidence gate and human review isn't available in real time, the system needs a fallback that is guaranteed to be correct — even if it's less personalized or less useful than the AI-generated version would have been.

The pattern by feature type: an AI chatbot that can't confidently answer a shipping question should route to a scripted response and a human handoff, not deliver a low-confidence guess. An AI-generated product description that fails a quality check should serve cached, approved copy rather than a potentially fabricated alternative. An AI system handling order instructions should fall back to a rule-based lookup from a structured database — a deterministic source where the answer is known.

The researchers spot-checked papers flagged by GPTZero for AI-assisted content, they found real errors: incorrect author attributions, wrong publication venues.

One commenter noted that a citation error "would be immediately caught by a DOI checker" — pointing directly at the value of deterministic, rule-based verification for facts that can be checked programmatically. Reserve human review for judgment-dependent outputs; use automated deterministic checks for verifiable ones.

Prerequisite flags make review gates a system-level constraint, not an application convention

The architectural challenge with human review is enforcement. Application logic that says "route this to review first" can be bypassed, misconfigured, or simply not implemented correctly under deadline pressure. Prerequisite feature flags solve this by making the review gate a system-level constraint rather than an application-level convention.

GrowthBook's prerequisite flags feature allows one flag to be conditioned on the active state of another, ensuring that a dependent feature can only activate when its parent flag is already enabled. The practical mapping: an "AI response delivery" flag can only be enabled if a "human review passed" flag is active. This means the AI output cannot reach the user through any code path unless the review condition has been explicitly satisfied. It prevents invalid flag state combinations — the exact failure mode that allows unreviewed outputs to slip through in complex systems.

When review identifies a quality problem after delivery has already begun, instant flag deactivation provides the kill switch: the AI feature can be turned off immediately without a redeployment cycle.

Combined with Safe Rollouts that auto-rollback on guardrail metric regression, this creates a layered defense — human review as the intentional gate, automated rollback as the safety net when review capacity is exceeded or a problem is detected in aggregate metrics rather than individual outputs.

The architecture here isn't about distrust of the model. It's about acknowledging that a system designed to generate plausible text will sometimes generate plausibly wrong text, and that the absence of a review layer is what makes that a production failure rather than a caught error.

AI quality doesn't break loudly — it drifts, and outcome-linked metrics are the only way to catch it

Shipping is not the finish line. For AI features that can hallucinate, launch day is when the real monitoring work begins — and most teams aren't ready for what comes next.

The failure mode here is subtle. Traditional software breaks loudly: error logs fill up, alerts fire, on-call engineers get paged. AI quality degradation doesn't work that way. As one practitioner framed it: "AI doesn't fail loudly. It drifts quietly."

A feature that passed every pre-launch check can silently become a hallucination risk weeks later, eroding user trust and downstream outcomes before any alert fires — unless monitoring is explicitly wired to user-facing results.

Why one-time post-launch review isn't enough

The conditions that made your AI feature safe at launch will not stay stable. There are at least five independent vectors through which post-launch quality can degrade: model drift, data drift, concept drift, bias drift, and hallucination drift (hallucination rates increase as prompts change or users start asking questions the model wasn't optimized for). Any one of these can independently degrade output quality without a single line of application code changing.

One of the most underappreciated examples is RAG configuration degradation. Vector databases go stale. Relevance rankings shift. One day your model is answering with precise, grounded context; weeks later it's confidently citing a two-year-old document because the vector index hasn't been refreshed.

The model hasn't changed. The application code hasn't changed. But the outputs have become hallucination-like — and without active monitoring, no one knows.

Outcome-linked metrics reveal when bad outputs are translating into real user impact

There's an important distinction between AI output metrics and outcome-linked metrics. Confidence scores, response latency, and token counts tell you something about model internals. They don't tell you whether hallucinations are actually harming users.

Outcome-linked metrics do: user correction rates, task completion rates, session abandonment after AI responses, escalation rates, and downstream conversion or error rates are the signals that reveal when bad outputs are translating into real user impact. The practical challenge is connecting AI output events to downstream user behavior without building a separate analytics pipeline.

GrowthBook connects directly to your existing data warehouse — Snowflake, BigQuery, Redshift, Postgres, and more — so AI output quality signals can be analyzed alongside all downstream behavioral data in one place. Critically, metrics can be added retroactively. When your team discovers a new quality signal two months after launch — say, a correlation between certain query types and elevated correction rates — you can instrument it without re-running experiments or rebuilding pipelines.

Detecting hallucination drift over time

Drift detection requires a documented baseline. Without capturing hallucination rates, correction rates, and outcome metrics at launch, teams have no reference point for distinguishing normal variance from genuine degradation.

The practical approach: define hallucination rules at launch, score responses against them, collect user feedback signals, and review error pattern spikes on a regular cadence — weekly for high-stakes features. When patterns shift, that's the signal to investigate prompts, update ground truth, and recalibrate thresholds.

GrowthBook's Insights product supports this longitudinal view — tracking cumulative metric performance over time, North Star metric trends, and experiment outcomes across the full history of a feature's deployment. Character.AI uses GrowthBook to compare modeling techniques from the perspective of user outcomes, using that signal to guide post-training research in directions that actually serve the product. That's outcome-linked AI monitoring at scale, not just point-in-time experiment analysis.

Automating responses to quality degradation

Manual review of AI quality metrics is too slow when hallucination rates spike. Automated responses — pausing rollouts, triggering alerts, routing to deterministic fallbacks — need to be wired to metric thresholds before you need them, not after.

Because the AI feature is gated behind a feature flag (as covered in earlier sections), an automated rollback can instantly revert to a safe fallback without a code deployment. This capability — instantly deactivating underperforming features without disrupting user experience — is only available if the flag infrastructure was in place before the degradation occurred.

The monitoring infrastructure described here isn't a separate system bolted on after launch. It's the same guardrail metric instrumentation and feature flag architecture from pre-launch — running continuously, connected to automated responses, and evolving as the feature does. That's the only way to keep a hallucination-prone AI feature safe over time.

The four layers only work as a system — here's what that looks like before and after launch

The through-line of this article is simple: hallucinations are not a model problem you're waiting on someone else to solve. They're a deployment problem you can engineer around right now.

The four layers covered here — guardrail metrics, staged rollouts, human review gates, and continuous monitoring — aren't independent best practices. They're a single system, and each layer depends on the others to work.

Before launch: three things that must be locked in before any AI feature ships

Before you ship anything, you need three things locked in: the guardrail metrics that will tell you something is wrong before your users do, the feature flag that gives you a kill switch, and the fallback behavior that activates when the AI output fails a confidence gate. If any of those three are missing at launch, you don't have a safety architecture — you have a slower full rollout.

For a customer service chatbot, that means: a guardrail metric on escalation rate (defined in SQL against your support data), a feature flag with Auto Rollback enabled, and a scripted fallback response that activates when confidence scoring routes an output to the review queue. Those three things take a day to set up. The ecommerce incidents described in the staged rollouts section happened because none of them were in place.

After launch: the monitoring infrastructure that catches what pre-launch testing missed

The honest tension here is that post-launch monitoring feels like overhead until the moment it isn't. RAG indexes go stale, user query patterns shift, and hallucination rates climb without a single line of code changing.

The teams that catch this early are the ones who documented a baseline at launch and wired outcome-linked metrics — correction rates, escalation rates, session abandonment — to automated responses, not manual dashboards.

The tooling question is secondary — what matters is whether your controls share the same data and kill switch

The tooling question is real but secondary. What matters more is the architectural principle: your rollout controls, your guardrail metrics, and your monitoring should all be connected to the same data and the same kill switch.

GrowthBook connects directly to your existing data warehouse — guardrail metrics run against your existing data warehouse, and feature flags provide instant rollback without redeployment, keeping your rollout controls and monitoring connected to the same data. That's the architecture that prevents your safety system from fragmenting across tools as the feature evolves.

This article was written to be genuinely useful to teams actively building AI features, not to describe an ideal state that no one has time to implement. The architecture here is the one practitioners are actually using.

The tradeoff worth sitting with: more review gates and tighter guardrails mean slower delivery and more operational overhead. That's real. The question is whether that cost is higher than the cost of a fabricated shipping address reaching a real customer, or a confidential data exposure during a public preview. For most teams, the math isn't close.

If you're early in this process and feeling uncertain about where to start, that's a reasonable place to be. This is genuinely hard to get right, and most teams are figuring it out in production.

What to do next: If you haven't shipped yet, start with guardrail metrics — pick two or three signals that sit closest to output quality for your specific feature, define them before launch, and wire them to an automated rollback. If you're already live without that infrastructure, the highest-leverage move is to put the feature behind a feature flag immediately, even if you don't change anything else yet. That single change gives you a kill switch, which is the minimum viable safety layer for any AI feature in production.

Related insights

Sign up for free

Take Growthbook for a spin, no credit card required.

Create my account

Table of Contents

Related Articles

See All Articles
Feature Flags
Experiments

Add local feature flags to a React app with TypeScript

Nov 20, 2026
x
min read

A magazine's home page shows two recent issues. The design team wants subscribers to try a shelf with four. All four covers are public, and changing editions while the page is open should update the shelf immediately. That gives you a small React feature with a useful acceptance test. Click from guest to subscriber and back, and count the covers each time.

We'll put the display limit in a typed local flag, evaluate it inside a React component, and leave the choice of edition with the app. The example runs in a client-rendered React app without a GrowthBook account, an SDK key, or a request for flag rules. For the wider workflow, free feature flags for coding agents explains what to ask an agent to implement and how to review the result.

Define what one flag controls

Start with an existing React and TypeScript app, or create a Vite React project for the example. Install both GrowthBook packages so the definitions and the hooks resolve against compatible SDK versions.

npm create vite@latest issue-shelf -- --template react-ts
cd issue-shelf
npm install
npm install @growthbook/growthbook@^2.2.0 @growthbook/growthbook-react@^2.2.0

Create src/flags.ts. Here the feature value is a number, which lets the component use the same rendering code for either edition. A future change from four covers to three is one edit to the rule.

import type { LocalFeatures } from "@growthbook/growthbook";

export type Edition = "guest" | "subscriber";
type Features = { issueLimit: number };
type Attributes = { id: string; edition: Edition };

declare module "@growthbook/growthbook" {
  interface Register {
    features: Features;
    attributes: Attributes;
  }
}

export const features: LocalFeatures = {
  issueLimit: {
    defaultValue: 2,
    rules: [
      { condition: { edition: "subscriber" }, force: 4 },
    ],
  },
};

A guest falls through to the default of two. A subscriber matches the rule and gets four. This rule applies to every subscriber, so the ID has no effect on the result here. If you later roll the shelf out to a percentage of subscribers, you already have an identity to use for repeatable assignment.

The type registration release note explains the version requirement for LocalFeatures. The registration connects the definition, the attributes, and the value returned by the hook. For the TypeScript mechanics, see Jeremy Dorn's Register pattern walkthrough.

Put the current reader above the shelf

Replace src/App.tsx with the component below. The edition switch is a local fixture so anyone reviewing the change can reproduce both states. In an existing app, take those two values from the active reader in your session layer.

import { useMemo, useState } from "react";
import { GrowthBook, GrowthBookProvider } from "@growthbook/growthbook-react";
import { features, type Edition } from "./flags";
import { IssueShelf } from "./IssueShelf";

export default function App() {
  const readerId = "reader-17";
  const [edition, setEdition] = useState<Edition>("guest");

  const gb = useMemo(() => new GrowthBook({
    attributes: { id: readerId, edition },
    defaultHashVersion: 2,
  }).initSync({ payload: { features } }), [readerId, edition]);

  return (
    <GrowthBookProvider growthbook={gb}>
      <main>
        <h1>Recent issues</h1>
        <p>Current edition: {edition}</p>
        <button onClick={() => setEdition(
          edition === "guest" ? "subscriber" : "guest"
        )}>
          Switch edition
        </button>
        <IssueShelf />
      </main>
    </GrowthBookProvider>
  );
}

The provider receives an instance initialized with the current reader and the checked-in rules. React can render the correct shelf on the first pass for that reader. If the app is still loading a session, decide what should occupy the shelf in the meantime. For this public list a two-cover guest view is reasonable. On a page where a switch would interrupt the reader, wait for the session before mounting the shelf.

Give your agent the React setup

Use the local flags guide to ask for a typed definition, a provider with the current reader, and a test that changes the edition.

Open the Agent Guide

The useMemo dependencies include the ID and the edition. If a reader subscribes while signed in, their ID can stay the same while their edition changes. Depending only on the ID would leave the old SDK instance in place. Passing a fresh object as the dependency on every render would create unnecessary instances. The example uses the two primitive values the rule setup actually needs.

Read the numeric flag where React renders the list

Create src/IssueShelf.tsx. Keep one stable ID per issue and let the component use the resolved value to select how many items to render.

import { useFeatureValue } from "@growthbook/growthbook-react";

const issues = [
  { id: "nov", title: "November · Print shops" },
  { id: "oct", title: "October · Bookbinders" },
  { id: "sep", title: "September · Type designers" },
  { id: "aug", title: "August · Paper mills" },
];

export function IssueShelf() {
  const limit = useFeatureValue("issueLimit", 2);

  return (
    <ul aria-label="Recent magazine issues">
      {issues.slice(0, limit).map(issue => (
        <li key={issue.id}>{issue.title}</li>
      ))}
    </ul>
  );
}

Keep the hook at the top of the component, inside the provider's subtree. The React SDK supplies the flag value through that provider. Calling a hook inside a click handler or inside the list loop would break the Rules of Hooks. Use the returned number in ordinary render logic.

There are two places you can see the number two in this implementation. The definition's defaultValue is what an ordinary guest gets through evaluation. The 2 passed to the hook is a fallback for a value the SDK can't supply. Giving them the same value makes the basic shelf a sensible fallback, but checking only the guest case would let a broken provider arrangement or missing definition go unnoticed. The subscriber case proves that the component actually received the rule.

The list keys identify magazine issues. Keep those keys independent of the flag. Switching from two covers to four should preserve the identity of the first two rows. There's also no need to remount the whole app with an edition-specific key. React preserves state according to component position and keys. For example, a search input outside the shelf shouldn't lose its text because someone subscribed.

Check the transition in both directions

Use the template's build script to type-check and bundle the app. Once that passes, start the dev server and open the local address it prints.

npm run build
npm run dev

On first load, the page says guest and lists November and October. Click Switch edition. The same page should say subscriber and add September and August. Click again. The list should return to the original two issues, in order. This last check catches code that appends more results on upgrade and never removes them on downgrade.

For a compile-time check, temporarily replace the numeric fallback with the string "2". Run the build again and inspect the error on the hook call. Then restore 2 and rebuild. If the string compiles, check that src/flags.ts belongs to the same TypeScript project as the component and that the package manager resolved compatible core and React versions.

npm ls @growthbook/growthbook @growthbook/growthbook-react

Look for the core package used by your app and by the wrapper. If the app has several incompatible installations, align the versions before diagnosing the JSX. A team using an existing build script should also confirm it actually runs the TypeScript checker.

Hand off a React change someone can review

Give your coding agent the one-shot local feature flags guide and a short description of the observable behavior. For this shelf, the request can be as specific as the test.

Show two public issues to guests and four to subscribers.
Keep the same reader ID when switching editions in the test.
Report the initial titles, the titles after upgrading, and the titles
after switching back. Include the type-check command you ran.

The shelf controls how many public titles to display. If your actual feature unlocks paid articles, enforce access when the server serves an article. A client-rendered flag only decides what to show in that browser. Whenever a change to the display limit can wait for a deployment, the small local file keeps the decision reviewable alongside the component. When an editor needs to change it between issues without an engineering release, compare the free GrowthBook options for managing flag rules.

Manage the shelf in GrowthBook

Start a GrowthBook workspace when your magazine team is ready to manage shelf rules between deployments.

Get Started with GrowthBook
Feature Flags
Experiments

Use local feature flags per request in Node.js

Nov 20, 2026
x
min read

Two customers download the same weekly report. One has agreed to preview a new CSV column. The other feeds the old two-column format into an importer. Both requests can land on the same Node.js process, and each needs the right file even if the other request arrives while the report is loading.

This tutorial builds that endpoint with one checked-in feature flag. The process loads the rules once. Each request supplies its own workspace attributes and receives its own evaluation. You can run the whole example on your machine without an account, a client key, or a flag server. The free feature flags for coding agents walkthrough introduces the local approach. Here we'll give the agent a server-specific test to pass.

Set up a small TypeScript service

Use Node.js 22 or newer for this example. In an empty directory, create an npm project that uses ES modules, install the core SDK, and add the TypeScript tools.

npm init -y
npm pkg set type=module
npm install @growthbook/growthbook@^2.2.0
npm install -D typescript tsx @types/node@22

Create tsconfig.json at the project root. The sample runs TypeScript directly with tsx and checks types in a separate step. Type checking with tsx explains why the two commands serve different purposes.

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "strict": true,
    "noEmit": true,
    "skipLibCheck": true,
    "types": ["node"]
  },
  "include": ["*.ts"]
}

The following files all live next to that configuration. The server imports ./flags.js even though the source file is flags.ts. That spelling follows TypeScript's Node.js module resolution for ESM and works with this runner.

Define the report contract in one flag

Create flags.ts. The public format has two columns. The sample preview workspace gets one additional column. The only targeting attribute the rule needs today is reportPreview. Carry the workspace ID too, so two people downloading a report on behalf of the same workspace can share an assignment if you introduce a percentage rollout later.

import type { LocalFeatures } from "@growthbook/growthbook";

type Features = { includeUniqueReaders: boolean };
export type Workspace = { workspaceId: string; reportPreview: boolean };

declare module "@growthbook/growthbook" {
  interface Register {
    features: Features;
    attributes: Workspace;
  }
}

export const features: LocalFeatures = {
  includeUniqueReaders: {
    defaultValue: false,
    rules: [{ condition: { reportPreview: true }, force: true }],
  },
};

The SDK type registration update covers this rule syntax and the 2.2.0 minimum. If you misspell reportPreview in a condition or use a string where the boolean flag expects a value, TypeScript can point at the definition. The Register pattern also makes the same feature name available in other modules without threading a generic type through each handler.

Before you add the endpoint, decide what belongs to the process and what belongs to the request. The feature definitions are identical for both customers. Their identity and preview enrollment can differ. If a handler changes an attribute on a single shared user instance before awaiting a database query, another handler could replace that attribute before the first handler resumes.

Give your agent a server-side task

Use the one-shot guide to add checked-in rules. Ask your agent to keep workspace attributes local to each request and demonstrate both CSV formats.

Open the Local Flags Guide

Create a scope when the request arrives

Create server.ts. The Node.js SDK provides GrowthBookClient for the process and createScopedInstance for the current request. The two fixture paths below stand in for two authenticated workspaces. The server binds to the loopback address so you can exercise it locally.

import { createServer } from "node:http";
import { setTimeout as delay } from "node:timers/promises";
import { GrowthBookClient } from "@growthbook/growthbook";
import { features, type Workspace } from "./flags.js";

const client = new GrowthBookClient({ defaultHashVersion: 2 })
  .initSync({ payload: { features } });

const demos = new Map<string, Workspace>([
  ["/demo/preview", { workspaceId: "press-a", reportPreview: true }],
  ["/demo/standard", { workspaceId: "press-b", reportPreview: false }],
]);

createServer(async (req, res) => {
  const workspace = demos.get(req.url ?? "");
  if (req.method !== "GET" || !workspace) {
    res.writeHead(404).end();
    return;
  }

  const gb = client.createScopedInstance({ attributes: workspace });
  await delay(10); // Stand-in for loading this workspace's report.
  const expanded = gb.isOn("includeUniqueReaders");
  const csv = expanded
    ? "period,downloads,unique_readers\nweek-1,80,55\n"
    : "period,downloads\nweek-1,80\n";

  res.writeHead(200, {
    "Content-Type": "text/csv; charset=utf-8",
    "Cache-Control": "private, no-store",
  });
  res.end(csv);
}).listen(4178, "127.0.0.1", () => {
  console.log("Report server listening on http://127.0.0.1:4178");
});

The HTTP handler looks up a workspace, creates its scope, and holds that scope across an asynchronous operation. The short promise-based timer gives a second request time to arrive. In a real export, that's where the database or storage read would happen. Keep the stable client at module scope and the scoped instance inside the handler. If a formatter needs the decision, pass it the boolean or the scoped instance.

In production, resolve the workspace and its access rights from the authenticated session before reading the report. The two selectable paths here are fixtures for public sample data. A caller's choice of a header, query string, or workspace slug can't establish permission to read another customer's report. The endpoint also prevents shared response caching. If you cache real exports, the cache needs to distinguish the workspace and the report format, along with any other inputs that change the response.

Test two requests that overlap

In the first terminal, type-check the service and start it. A successful type check exits quietly. The next command prints the loopback address and keeps the server running.

npx tsc --noEmit
npx tsx server.ts

Save the following as verify.mjs. In a second terminal, run node verify.mjs. Node's built-in fetch calls the local server. Strict assertions compare the full header and data row, including where each row ends.

import assert from "node:assert/strict";

async function report(name) {
  const response = await fetch("http://127.0.0.1:4178/demo/" + name);
  assert.equal(response.status, 200);
  return response.text();
}

const [preview, standard, previewAgain] = await Promise.all([
  report("preview"), report("standard"), report("preview"),
]);

assert.equal(preview, "period,downloads,unique_readers\nweek-1,80,55\n");
assert.equal(standard, "period,downloads\nweek-1,80\n");
assert.equal(previewAgain, preview);
console.log("Both workspaces received the expected CSV.");

You should see the single success line. Testing just the preview workspace proves that the code can add a column. Testing both responses catches the more expensive mistake of silently changing an existing customer's export. Keeping the old format assertion around is especially valuable when the downstream importer belongs to someone outside your team.

To check the compile-time boundary, temporarily change gb.isOn("includeUniqueReaders") to gb.isOn("includeUniqueReader") and rerun npx tsc --noEmit. The diagnostic should identify the misspelled key. Restore it before running the server again. A coding agent can include the before-and-after type check and the two CSV headers in its pull request.

Decide when to expand the report

When one preview customer is ready, leave everyone else on the default. If the next step is to enroll a fraction of workspaces, choose an attribute that matches the promise you're making. Hashing a workspace ID lets colleagues downloading the same report share a format. Hashing each user's ID could give two analysts in the same company different column sets. The one-shot guide shows how to express a percentage and hash attribute in a local rule.

A new rule in flags.ts reaches a process through your deployment. During a rolling deploy, the same workspace might hit old and new processes. If customers need a durable choice of export schema, keep supporting the agreed format in the API itself. When the operational requirement is to change targeting between deploys, you can keep the scoped evaluation in the handler and change how the process receives its rules. The free GrowthBook offerings overview covers Cloud Starter and self-hosting for teams that want to manage those rules.

Manage report targeting in Cloud

Create a GrowthBook workspace when the team supporting exports needs to expand the preview independently of a server release.

Get Started with GrowthBook
Feature Flags
Experiments

Use local feature flags in the Next.js App Router

Nov 20, 2026
x
min read

Your conference agenda has a new “Show room” button. Reviewers should see it on the first page load. Everyone else should get the existing agenda. If a Server Component renders the old agenda and a browser hook independently decides to show the new button, the first HTML and the browser can disagree.

In the Next.js App Router, the server can make this decision once and pass a boolean to the interactive component. We'll define the flag in the repo, select the preview audience with a local test cookie, and verify the actual HTML for both visitors. Clicking the button will still work after hydration. You can follow along without a GrowthBook account or client key. If you're assigning this work to an agent, the free feature flags for coding agents walkthrough gives the background.

Keep the flag definitions on the server

Use a TypeScript App Router project, or create a small one with create-next-app. These paths assume the project has an app directory at its root. If yours uses src/app, put both app and lib under src so the relative imports still match.

npx create-next-app@latest agenda-flags --ts --app --empty --use-npm --no-src-dir --no-tailwind --yes
cd agenda-flags
npm install @growthbook/growthbook@^2.2.0 server-only

Keep the starter's root layout. Create lib/agenda-flags.ts with the following code. The SDK type registration update explains how LocalFeatures checks the flag and its rule against the registered types. This setup requires version 2.2.0 or later.

import "server-only";
import { GrowthBookClient, type LocalFeatures } from "@growthbook/growthbook";

type Features = { agendaRoomDetails: boolean };
type Attributes = { audience: "preview" | "public" };

declare module "@growthbook/growthbook" {
  interface Register {
    features: Features;
    attributes: Attributes;
  }
}

const features: LocalFeatures = {
  agendaRoomDetails: {
    defaultValue: false,
    rules: [{ condition: { audience: "preview" }, force: true }],
  },
};

const client = new GrowthBookClient({ defaultHashVersion: 2 })
  .initSync({ payload: { features } });

export function showRoomDetails(audience: Attributes["audience"]) {
  const gb = client.createScopedInstance({ attributes: { audience } });
  return gb.isOn("agendaRoomDetails");
}

The module can reuse the client and rules across requests handled by the same process. It only holds the current visitor's audience inside the function call. Two requests can therefore use the same flag file and get different results. The Node.js SDK documents the scoped client used under the App Router here.

This particular rule has no percentage and needs no visitor ID. The two audiences are sufficient. If the team later asks for a rollout to 10% of attendees, add a stable attendee or registration ID and choose which of those units should stay together. Jeremy Dorn's Register pattern article explains how a change to an attribute type reaches the call sites that construct it.

Read the cookie for this request

Create app/agenda/page.tsx. The static heading can render for everyone. The request-specific content sits behind a Suspense boundary, so the fallback can show a neutral loading message until the server has the cookie.

import { Suspense } from "react";
import { cookies } from "next/headers";
import { showRoomDetails } from "../../lib/agenda-flags";
import { Agenda } from "./Agenda";

async function AgendaForVisitor() {
  const jar = await cookies();
  const audience = jar.get("agenda-preview")?.value === "1"
    ? "preview"
    : "public";

  return <Agenda showRoom={showRoomDetails(audience)} />;
}

export default function AgendaPage() {
  return (
    <main>
      <h1>Conference agenda</h1>
      <Suspense fallback={<p>Loading the agenda…</p>}>
        <AgendaForVisitor />
      </Suspense>
    </main>
  );
}

The App Router's cookies function reads the incoming request asynchronously. Read it inside the visitor-specific component. Keeping that read out of shared cached code lets the surrounding page reuse common content without treating one visitor's decision as another's. This placement also accommodates App Router projects using Cache Components.

Give your agent the Next.js setup

Use the local flags guide with a concrete request to evaluate one flag on the server and return the same decision to the browser.

Open the Local Flags Guide

The cookie in this demo is a convenient way to preview a public UI treatment. Anyone with that cookie can see the room label. To target paying attendees or private sessions, obtain the audience from the verified session on the server and authorize the underlying data separately.

Pass one decision to the interactive component

Create app/agenda/Agenda.tsx. It's a Client Component because a visitor can open and close the room details. Its initial branch comes directly from the boolean supplied by the page.

"use client";

import { useState } from "react";

export function Agenda({ showRoom }: { showRoom: boolean }) {
  const [open, setOpen] = useState(false);

  return (
    <section data-agenda={showRoom ? "preview" : "standard"}>
      <h2>10:00 · Designing for print</h2>
      {showRoom && (
        <>
          <button aria-expanded={open} onClick={() => setOpen(!open)}>
            {open ? "Hide room" : "Show room"}
          </button>
          {open && <p>Room 204</p>}
        </>
      )}
    </section>
  );
}

The client boundary takes a serializable prop. The component receives true or false. The SDK instance, targeting rules, and cookie handling stay in the server module. You don't need a React provider for this component because its only browser-side choice is whether the visitor has clicked the button.

Follow one page load through server rendering and hydration. For a preview visitor, the server can send a section with a “Show room” button. React's first client render gets the same showRoom value and starts with open set to false. Hydration attaches the click handler. Opening the room detail then updates component state and doesn't require another flag evaluation.

Verify the HTML and the click separately

Build the production app and start it on port 3100.

npm run build
npm run start -- -p 3100

In a second terminal, save this as verify-agenda.mjs in the project root and run node verify-agenda.mjs. The test makes two requests to the same route and looks for a marker on the rendered section. A marker in the HTML helps catch an implementation that only adds the intended layout after JavaScript runs.

import assert from "node:assert/strict";

async function agenda(cookie) {
  const response = await fetch("http://localhost:3100/agenda", {
    headers: cookie ? { Cookie: cookie } : {},
  });
  assert.equal(response.status, 200);
  return response.text();
}

const [standard, preview] = await Promise.all([
  agenda(""), agenda("agenda-preview=1"),
]);
assert.match(standard, /<section data-agenda="standard"/);
assert.doesNotMatch(standard, /<section data-agenda="preview"/);
assert.match(preview, /<section data-agenda="preview"/);
assert.doesNotMatch(preview, /<section data-agenda="standard"/);
console.log("Both agenda layouts arrived in the server HTML.");

Now open http://localhost:3100/agenda in a fresh browser profile. You should see the session title without a room button. In that local page's browser console, set the demo cookie and reload.

document.cookie = "agenda-preview=1; Path=/; SameSite=Lax";
location.reload();

Click Show room and look for Room 204. Click Hide room and the room should disappear. This second check proves the component became interactive. If the server response test passes but the click fails, inspect the browser console for a hydration error before changing the flag rules.

Remove the fixture and reload once more. The button should disappear and the agenda title should remain.

document.cookie = "agenda-preview=; Path=/; Max-Age=0";
location.reload();

The reload is deliberate. Editing the cookie in an already open document doesn't rerun its Server Component. In your app, use the normal session-change flow to request a fresh server render when a visitor signs in, signs out, or leaves a preview group.

Choose where the next flag should be evaluated

For a decision needed in the initial HTML, give the agent an observable requirement. Ask it to show the two HTTP responses, then click the interactive control in the preview. The first check catches an incorrect layout. The second catches correct HTML that never becomes interactive.

If a later feature depends on an action entirely within the browser, evaluate whether its flag belongs in the client tree. The one-shot local flags guide covers both placements. This agenda only needs one server decision. Updating its checked-in rule takes a deployment. Keep that tradeoff in view if the event organizer needs to pause a preview while attendees are already arriving. For that case, compare GrowthBook’s free options for changing flags between deploys.

Manage the agenda rollout in Cloud

Create a GrowthBook workspace when the organizer needs to adjust a preview audience while your Next.js app is serving attendees.

Get Started with GrowthBook

Ready to ship faster?

No credit card required. Start with feature flags, experimentation, and product analytics—free.

Simplified white illustration of a right angle ruler or carpenter's square tool.White checkmark symbol with a scattered pixelated effect around its edges on a transparent background.