Interview's MateAI Engineering
Interview's Mate

AI Engineering

LLMs, RAG, agents & evals

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
AI Engineering topics
Lesson 72/ 160
04. Evals

A/B Testing Prompts

Online experiments that compare prompt variants on real user metrics.

IntermediateOnline Exp · Primary Metric3 quiz questions
Practice Generative AI Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidHands-onQuiz (3)Practice

In plain words

Show half of the users answers from the old prompt and half from the new one, then see which group gets helped more.

Think of it like…

Testing two menu layouts in a restaurant on alternate tables: you measure what people order and how long they wait, not what the chef thinks looks better.

Where you’ve seen it

A support assistant resolves 60% of conversations without escalation. The team wants to detect a 3-point gain (60% to 63%) with a 5% significance level and 80% power. The standard formula gives about 4,100 conversations per arm. With 2,000 conversations a day split 50/50, that's about 4 days; they run a full 7 days to cover the weekly cycle. Result: variant B resolves 62.4% vs 60.1%, a +2.3-point gain with a 95% interval of roughly +0.2 to +4.4, but median latency rises from 3.1s to 4.0s and cost per conversation by 18%. Because the primary metric improved and the guardrails stayed within budget, B ships to 100% with a latency watch.

Words you’ll hear

Randomisation unit
What's assigned to a variant: usually the user, so each person gets a consistent experience.
Primary and guardrail metrics
The one metric that decides, and others (latency, cost, safety) that must not degrade.
Minimum detectable effect
The smallest improvement you design the test to detect.
Sample ratio mismatch
A traffic split that differs from the planned one, signalling an assignment bug.

Visual diagram

Loading interactive diagram

Theory & deep dive

Designing the experiment

Choose the randomisation unit (user or session, not individual requests, so one person doesn't see mixed behaviour), assign deterministically with a hash of the user id and experiment name, and record the variant (and prompt version) in every trace. Pick one primary metric tied to user value (resolved without escalation, task completed, accepted suggestion) and a few guardrails (p95 latency, cost per request, safety or refusal rate, escalation to humans). Avoid optimising proxy metrics such as response length or thumbs-up alone.

Compute the sample size up front. For a binary metric with baseline p₁ and target p₂, the per-arm size is roughly (z₁₋α/₂ √(2 p̄ q̄) + z₁₋β √(p₁q₁ + p₂q₂))² / (p₂ − p₁)². For 60% → 63% at α = 0.05 and 80% power that gives about 4,100 per arm. Smaller effects need far more traffic: halving the effect to 1.5 points needs roughly four times the sample. Fix the duration (at least one full weekly cycle) and don't stop the moment the p-value dips below 0.05, since repeated peeking inflates false positives.

Sample-size arithmetic

p₁ = 0.60, p₂ = 0.63, p̄ = 0.615. Terms: 1.96 × √(2 × 0.615 × 0.385) = 1.96 × 0.688 = 1.349 and 0.84 × √(0.24 + 0.2331) = 0.84 × 0.688 = 0.578. Sum = 1.927, squared = 3.71, divided by 0.03² = 0.0009 gives about 4,125 per arm.

Key takeaway: Sticky assignment, one primary metric with guardrails, a precomputed sample size and a fixed duration.

Running, reading and pitfalls

Start with a shadow or canary release (a few percent of traffic) to catch crashes, cost spikes and safety issues before the full split. During the test check for sample ratio mismatch (if a planned 50/50 split shows 52/48 on thousands of users, assignment is broken), segment results (new versus returning users, intents, languages), and watch for novelty effects (users react to change, then settle). Report the effect with a confidence interval, not only a p-value, and weigh it against cost and latency changes: a 2-point gain that costs 18% more per conversation may or may not be worth it.

LLM-specific caveats: outputs are stochastic, so variance is higher than for deterministic UI changes; interactions with other changes (a model upgrade at the same time) confound results; and metrics based on LLM judges need validation. Keep a holdback group on the old prompt for a while after launch to confirm the gain persists. When traffic is small, rely on offline evals and sequential or Bayesian methods with proper stopping rules, and accept that you can only detect large effects.

Reading the result

B: 62.4% (n = 4,200), A: 60.1% (n = 4,200). Difference +2.3 points, standard error √(0.624 × 0.376/4200 + 0.601 × 0.399/4200) ≈ 0.0107, so the 95% interval is +2.3 ± 2.1 = [+0.2, +4.4] by this rough calculation, which the team reports with the latency and cost trade-offs.

Key takeaway: Check assignment health, segment results, report intervals and weigh guardrails; keep a holdback.

How it works, step by step

Step 1 of 4
Team

1. Pass offline gates

Only variants that clear golden-set regression and safety checks reach live traffic.

no critical-slice regressions
Key numbers at this step
Cheap
filter first

When to use it & trade-offs

What it is

A/B testing prompts compares two prompt (or model, or retrieval) variants on live traffic: users are randomly assigned to A or B, and you measure the outcomes that matter (resolution rate, task completion, thumbs-up rate, escalation, conversion) alongside guardrail metrics (latency, cost, safety flags). It's the final check after offline evals, because it measures real user behaviour rather than a proxy, and it needs proper experiment design: randomisation unit, sample size, a fixed duration and a pre-registered primary metric.

Why it matters: Offline evals show that a variant is likely better on your test set; only an online experiment shows whether users actually benefit. LLM changes also have side effects (longer answers, higher cost, different refusal rates) that offline suites may miss. Done carelessly, A/B tests produce false wins from noise, peeking or biased assignment, so statistical discipline is part of the skill.

When to reach for it

  • Deciding whether a prompt or model change helps real users
  • Checking latency, cost and safety side effects
  • Rolling out changes gradually with measurable gates

When to avoid or be careful

  • Stopping the test as soon as p < 0.05
  • Randomising per request so users see both versions
  • Optimising a proxy metric such as response length

Ways to validate a prompt change

Online A/B test
Best for: Measuring real user impact

Strengths: True outcomes and side effects.

Watch out: Needs traffic and time; exposes users to the variant.

Offline evals on a golden set
Best for: Fast screening and regression checks

Strengths: Quick, safe and repeatable.

Watch out: Proxy for real behaviour.

Shadow mode
Best for: Checking safety, cost and latency without exposure

Strengths: No user impact.

Watch out: Can't measure user reactions.

Interview signals & numbers to know

When this topic comes up

  • “How do we know the new prompt helps users?”
  • “Canary and shadow deployment for LLMs”
  • “How many users do we need?”
  • Guardrail metrics
  • Sample ratio mismatch

Benchmarks & rules of thumb

~4,100 / arm -
sample size to detect 60% → 63%
+2.3 pts -
observed gain in the example
7 days -
minimum to cover weekly seasonality

Common mistakes & follow-ups

Common interview mistakes

  • Calling a winner after two days because the dashboard turned green.
  • Ignoring cost and latency changes that eat the gain.
  • Running the test while shipping another model change in parallel.

Likely interviewer follow-ups

  • How would you A/B test a new system prompt?
  • How do you decide the sample size and duration?
  • What guardrail metrics would you set?
Interview tip: Say "offline gates, canary, then A/B with guardrails". That layered rollout shows you balance speed with safety.
Loading hands-on exercise

Knowledge check

Loading quiz

Put it into practice

Practice related interview Q&A

Apply A/B Testing Prompts with generative AI interview Q&A and hands-on practice.

Open Generative AI Q&A →Tech Learn Q&A
Previous topic
Regression Evals in CI
Next topic
Human Labeling