A/B Testing and Regression Testing for RAG

Compare configurations fairly and stop quality regressions in CI.

Last Updated: 27, Nov 2026

Search Tutorials

--Ads--

Module 16 · Lesson 8 of 8 · Intermediate

A/B Testing and Regression Testing for RAG

Compare configurations fairly and stop quality regressions in CI.

With a golden test set and metrics in place, you can answer the question every change raises: is the new version better than the old one? This lesson covers comparing two configurations fairly offline, telling real improvements from noise, blocking regressions automatically in CI, and running A/B tests with real users.

Offline comparison: same questions, two configurations

Run both configurations on the same test set and compare them per question, not just by averages. A paired comparison shows which questions improved, which got worse, and whether the change is mostly noise.

compare_runs.py
import json

def load(path: str) -> dict[str, dict]:
    return {r["id"]: r for r in map(json.loads, open(path, encoding="utf-8"))}

def compare(old_path: str, new_path: str, metric: str = "correct") -> None:
    old, new = load(old_path), load(new_path)
    common = sorted(old.keys() & new.keys())
    better = [i for i in common if new[i][metric] > old[i][metric]]
    worse = [i for i in common if new[i][metric] < old[i][metric]]
    print(f"{len(common)} questions | improved {len(better)} | regressed {len(worse)} | "
          f"old {sum(old[i][metric] for i in common) / len(common):.3f} -> "
          f"new {sum(new[i][metric] for i in common) / len(common):.3f}")
    for i in worse:
        print(f"  REGRESSED {i}: {new[i]['question']}")

Always read the regressed cases. A change that improves the average by 3% but breaks an important category (for example, all unanswerable questions) may not be worth shipping.

Is the difference real?

With 100 test questions, a difference of two or three points can easily be noise, especially when LLM generation and judging add randomness. A paired bootstrap estimates how confident you can be: resample the questions many times and see how often the new version wins.

import random

def paired_bootstrap(old_scores: list[float], new_scores: list[float], rounds: int = 10_000) -> dict:
    diffs = [n - o for o, n in zip(old_scores, new_scores)]
    n = len(diffs)
    means = sorted(sum(random.choices(diffs, k=n)) / n for _ in range(rounds))
    return {
        "mean_diff": sum(diffs) / n,
        "ci95": (means[int(0.025 * rounds)], means[int(0.975 * rounds)]),
        "p_new_better": sum(m > 0 for m in means) / rounds,
    }

If the 95% interval includes zero, you have not shown an improvement. Either the change does not matter much, or you need more test questions. Running the generation and judging twice and averaging also reduces noise.

Regression tests in CI

Turn your evaluation into tests that run on every pull request that touches prompts, retrieval settings or models. Use thresholds slightly below current performance, so normal noise does not fail the build but real regressions do.

tests/test_rag_quality.py
import json
import pytest
from my_rag import retrieve, answer           # your pipeline
from evaluate_lib import hit_rate, judge_correct, declines

CASES = [json.loads(l) for l in open("golden_set.jsonl", encoding="utf-8")]
ANSWERABLE = [c for c in CASES if c["type"] != "unanswerable"]
UNANSWERABLE = [c for c in CASES if c["type"] == "unanswerable"]

def test_retrieval_hit_rate():
    rate = hit_rate([retrieve(c["question"], 5) for c in ANSWERABLE], ANSWERABLE)
    assert rate >= 0.90, f"hit@5 dropped to {rate:.2f}"

@pytest.mark.slow                                   # LLM calls: run on PRs, not on every save
def test_answer_correctness():
    correct = sum(judge_correct(c, answer(c["question"])) for c in ANSWERABLE)
    assert correct / len(ANSWERABLE) >= 0.85

@pytest.mark.slow
def test_declines_unanswerable():
    declined = sum(declines(answer(c["question"])) for c in UNANSWERABLE)
    assert declined / len(UNANSWERABLE) >= 0.90

Keep the fast, deterministic retrieval tests in the default run and the LLM-judged tests in a slower suite. Also track latency and cost per question in the same run, so a change that doubles cost is noticed before release.

Online A/B testing

Offline tests use a fixed set of questions; real traffic is more varied. For significant changes, run an A/B test:

  1. Randomly assign users (not individual requests, so each user has a consistent experience) to version A or B.
  2. Choose the success metrics in advance: thumbs-up rate, follow-up question rate, escalations to human support, task completion, plus latency and cost.
  3. Run until you have enough data for a confident decision; small differences need many users.
  4. Watch guardrail metrics (error rates, complaints) from the first day and stop early if they get worse.
Cheaper than A/B: shadow testing

Run the new version on a copy of real traffic without showing its answers to users, then judge both versions' answers offline with the pairwise judge from LLM-as-Judge. You get realistic questions without any user risk.

Key takeaways

  • Compare configurations per question on the same test set, and always read the regressions.
  • Use a paired bootstrap to check whether a difference is larger than noise.
  • Turn evaluations into CI tests with thresholds, separating fast retrieval tests from slower LLM-judged ones.
  • Validate important changes online with A/B or shadow tests and pre-defined metrics.