Module: L1-M06 · Duration: ~180 min (estimate, including the independent exercise) · Previous: L01 · Next: L03 - Golden datasets and baselines
1. Introduction
When the output changes between runs, one pass is only one sample. The right question is not "does this test pass or fail?" but "what percentage of the time does this test pass, and how sure are we of that figure?".
This lesson teaches three tools: running repeatedly and counting, the Wilson confidence interval to know how sure a figure is, and a stability gate that decides PASS/FAIL only when the data is clear enough. You will also learn a measure of the user's experience: pass^k.
2. Learning objectives
- Compute the pass rate from N runs, keeping environment errors (timeouts) separate.
- Compute and explain the 95% Wilson confidence interval for a pass rate.
- Apply a stability gate with three outcomes: PASS, FAIL, INCONCLUSIVE.
- Estimate how many runs are needed for a threshold to be meaningful.
- Distinguish pass@k from pass^k, and choose the measure that fits the user's experience.
3. Prerequisites and tools
- L01: sources of variation;
SimulatedFAQModel. - L1-M04-L05: a rate must come with a count.
4. Concepts
4.1 A pass rate is an estimate
Run a case N times and count k passes: the rate is k/N. But 3/3 and 300/300 are both 100%, and nobody trusts those two figures equally. A confidence interval tells you which "true" rates are consistent with the data you have.
Environment errors (timeouts, network errors) are not evidence about the quality of the answer. Count them separately, and remove them from N when you compute the rate. Otherwise an infrastructure incident looks like a regression.
Everyday example. A noodle shop asks its customers for feedback. "3 out of 3 customers liked it" and "300 out of 300 customers liked it" are both 100%, but every owner trusts the second figure more. And if 8 customers leave because of a power cut before they are served, the owner does not record them as "customers who disliked the food": that is about the power cut, and it is written down separately.
Worked example. Run Q-ADDR 50 times with faq-v3@sim-2026-10, a configuration with simulated timeouts. Save
the following as try_l02.py in the lab folder, run python try_l02.py, then delete the file:
from lab import dataset, repeat
from lab.simulated_model import SimulatedFAQModel
case = next(c for c in dataset.load() if c["id"] == "Q-ADDR")
r = repeat.run_case(SimulatedFAQModel("faq-v3", "sim-2026-10"), case, 50, seed=2)
print(f"passes={r.passes} fails={r.fails} errors={r.errors} valid={r.valid_runs} rate={r.rate:.2f}")
print(f"if timeouts counted as fails: {r.passes}/{r.passes + r.fails + r.errors} = {r.passes / 50:.2f}")
Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions gave identical output):
passes=42 fails=0 errors=8 valid=42 rate=1.00
if timeouts counted as fails: 42/50 = 0.84
- 50 calls = 42 answers + 8 timeouts. All 42 answers pass.
- The pass rate: 42/42 = 100% over 42 valid runs, with a separate line: "8/50 runs timed out".
- If timeouts are counted as fails: 42/50 = 84%. The case looks as if its quality dropped, although every answer that came back is right.
4.2 The Wilson interval
The "easiest" computation (the rate ± 1.96 × the standard deviation, called the Wald interval) gives poor results when N is small or the rate is close to 0 or 1. Brown, Cai and DasGupta (2001) write that "the chaotic coverage properties of the Wald interval are far more persistent than is appreciated", and recommend "the Wilson interval or the equal-tailed Jeffreys prior interval for small n" [R1]. The NIST statistics handbook also presents the Wilson method and names one advantage: "the lower limit cannot be negative" [R2].
You do not need to memorise the formula; lab/repeat.py has the function wilson(k, n). What you need to remember
is how to read it:
| Result | 95% interval | Meaning |
|---|---|---|
| 3/3 | 44% - 100% | The true rate could be only about 50%. 3 runs say nothing yet |
| 30/30 | 89% - 100% | Fairly sure that the true rate is above 89% |
| 27/30 | 74% - 97% | A wide interval: from "quite bad" to "very good" |
Everyday example. A courier who says "your parcel arrives in 2-4 days" is more honest than one who says "exactly 3 days". A confidence interval is that kind of range. If the courier has driven this route only 2 times, even a narrow range is not worth much; after 200 trips, a narrow range is well founded.
Worked example: why not the Wald interval. Compute 27/30 by hand first:
- The rate p = 27/30 = 0.9.
- The standard deviation of the rate: the square root of p × (1 − p) / n = the square root of 0.9 × 0.1 / 30 = the square root of 0.003 ≈ 0.055.
- Half of the Wald interval: 1.96 × 0.055 ≈ 0.107. Interval: 0.9 ± 0.107 = [0.79, 1.01]. The upper bound is above 100%: impossible.
- With 3/3: p = 1, so p × (1 − p) = 0, and the Wald interval is [1, 1]. After 3 runs, Wald says "certainly 100%": even more absurd.
Check it with code (save as try_l02.py in the lab folder, run python try_l02.py, then delete the file):
import math
from lab.repeat import wilson
for k, n in ((3, 3), (27, 30), (30, 30)):
p = k / n
half = 1.96 * math.sqrt(p * (1 - p) / n)
low, high = wilson(k, n)
print(f"{k}/{n}: rate {p:.2f} | Wald [{p - half:.2f}, {p + half:.2f}] | Wilson [{low:.2f}, {high:.2f}]")
Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions gave identical output):
3/3: rate 1.00 | Wald [1.00, 1.00] | Wilson [0.44, 1.00]
27/30: rate 0.90 | Wald [0.79, 1.01] | Wilson [0.74, 0.97]
30/30: rate 1.00 | Wald [1.00, 1.00] | Wilson [0.89, 1.00]
Wilson always stays inside [0, 1], and for 3/3 it tells the truth: the true rate could be as low as about 44%.
4.3 The stability gate
A threshold such as "pass rate ≥ 80%" only means something when it is compared with the whole interval:
- PASS: the lower bound of the interval ≥ the threshold.
- FAIL: the upper bound < the threshold.
- INCONCLUSIVE: the interval straddles the threshold: run more, do not guess.
The gate in the lab also returns INCONCLUSIVE when there are too few valid runs. INCONCLUSIVE is an honest result, not a failure of the test.
Everyday example. A teacher grading papers: a mark that is certainly 5 or above passes, a mark that is certainly below 5 fails. If a paper is smudged and cannot be read, the teacher has it rewritten once, instead of guessing the mark.
Good vs bad. Q-HOTLINE after 30 runs (demo §8.1): 22/30.
| Weak decision | Good decision | |
|---|---|---|
| What you do | Compare the rate 0.73 with the threshold 0.8 and write FAIL; or rerun until you see ≥ 0.8 and write PASS | Compare the interval [0.56, 0.86] with 0.8: it straddles the threshold, so write INCONCLUSIVE and run more once, under the rule below |
| Why | A number without its interval pretends to be certain; rerunning until green picks the result you wanted | You conclude only when the data are clear, and the number of runs is decided in advance |
The rule for running more. "Run more" only makes sense with discipline. Otherwise it becomes "rerun until it is green":
- Decide the number of runs N in advance, based on the smallest change you care about and on power (L04).
- Use a new seed (with a real API: a new run, at a new time).
- Draw the conclusion from the new run only. Do not add its passes/fails to the previous run.
- Run more at most once per question. If it is still unclear, report INCONCLUSIVE and let a person decide according to the risk.
Stopping as soon as you see the result you want, or pooling the data after you have seen the results, invalidates every figure in this lesson.
4.4 How many runs are needed?
The higher the threshold, the more runs are needed. With a threshold of 0.9, a case that has never failed still needs a few dozen runs to get through the gate. You will compute the exact figure in the exercise in section 9. This is why a stable test suite for an LLM feature costs more, and why you should run repeatedly selectively: high-risk cases, cases close to the threshold.
Everyday example. To claim "more than half of the customers are happy", asking a few dozen people is enough to see it. To claim "more than 90% of the customers are happy", you must ask many more, because a few unhappy answers are enough to shake the conclusion.
Worked example. A case that has never failed, threshold 0.8 (threshold 0.9 is kept for the exercise in section
9). Save the following as try_l02.py in the lab folder, run python try_l02.py, then delete the file:
from lab import repeat
from lab.repeat import CaseResult
for n in (10, 15, 16, 30):
result = CaseResult("X", passes=n)
low, high = result.interval()
print(f"{n}/{n}: CI95 [{low:.3f}, {high:.3f}] gate(0.8) {repeat.gate(result, 0.8, min_valid=1)}")
Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-06; both versions gave identical output):
10/10: CI95 [0.722, 1.000] gate(0.8) INCONCLUSIVE
15/15: CI95 [0.796, 1.000] gate(0.8) INCONCLUSIVE
16/16: CI95 [0.806, 1.000] gate(0.8) PASS
30/30: CI95 [0.886, 1.000] gate(0.8) PASS
With 15/15 the lower bound is 0.796, just short of 0.8, so the gate is still INCONCLUSIVE. From 16/16 it passes. A perfect case still needs 16 runs to clear a threshold of 0.8; a higher threshold needs even more.
Anthropic's documentation on evals also recommends prioritising volume: "More questions with slightly lower signal automated grading is better than fewer questions with high-quality human hand-graded evals" [R3].
4.5 pass@k and pass^k
Two measures for k attempts, according to Anthropic's article on evals for agents [R4]:
- pass@k "measures the likelihood that an agent gets at least one correct solution in k attempts". It suits a tool where the user can try again and pick the good result themselves.
- pass^k "measures the probability that all k trials succeed". The article gives the example (0.75)³ ≈ 42%. It suits a product where every answer has to be right, such as a customer service chatbot. τ-bench defines pass^k as "the chance that all k i.i.d. task trials are successful, averaged across tasks" (in the paper, not on the abstract page) [R5].
With a pass rate p = 0.95, pass^10 = 0.95¹⁰ ≈ 0.60. If a customer asks 10 questions, each with a pass rate of 95% and with answers independent of each other, the customer has about a 40% chance of getting at least one wrong answer. The figure 95% looks much better than the real experience.
Worked example. With p = 0.75 and k = 3, by hand:
- pass^3: all three runs right = 0.75 × 0.75 × 0.75 = 0.5625 × 0.75 ≈ 0.42.
- pass@3: at least one run right = 1 − (all three wrong) = 1 − 0.25 × 0.25 × 0.25 = 1 − 0.015625 ≈ 0.98.
- The same case, two very different figures: 98% if the user may try again and pick, 42% if all three answers must be right.
Everyday example. A chatbot that suggests dish names for a restaurant: the customer looks at 3 suggestions and picks the best, so pass@3 is a fair measure. A table-booking chatbot: the customer asks 3 questions (date, time, number of people), and one wrong answer books the wrong table, so you must use pass^3.
Good vs bad.
| Weak measure for a customer-service chatbot | Good measure | |
|---|---|---|
| How | CI reruns up to 3 times and is green if one run passes (that is pass@3) | Run N times, report k/N and the 95% interval, then compute pass^k for the number of questions a customer usually asks |
| A 60% case | CI is green about 94% of the time (1 − 0.4³) | Report 60%, and pass^3 ≈ 22%: a customer who asks 3 questions most likely meets at least one wrong answer |
5. Technical deep dive
5.1 lab/repeat.py
| Function | What it does |
|---|---|
run_case(model, case, n, seed) |
Runs n times, returns a CaseResult with passes, fails, errors, and at most 3 failure samples |
CaseResult.rate, .interval() |
The rate and the Wilson interval over the valid runs |
gate(result, threshold, min_valid) |
PASS / FAIL / INCONCLUSIVE |
pass_hat_k(p, k) |
p^k |
The failure samples (failure_samples) record the answer and which property failed. You will need them in L05 to
decide whether a failure is a product defect or an oracle defect.
5.2 The Python you need for this lesson
@dataclasscreates a class that holds data;@propertygives a value that is computed like an attribute (result.rate, without parentheses).next(n for n in range(1, 1000) if điều_kiện)(with a condition in place ofđiều_kiện) takes the first value that satisfies the condition.
6. The QA perspective
- What to decide before running: the threshold for each risk level, the number of runs, how to handle environment errors.
- What can go wrong: rerunning until green (hides defects); counting timeouts as fails; reporting "100%" from 3 runs; using one threshold for every case even though their risks differ.
- Reporting: k/N, the 95% interval, the number of environment errors, the gate result, and the failure samples. Never report a rate without N.
7. Real-world scenarios
Scenario 1 (fictional): "rerun up to 3 times". A team configures CI to rerun LLM tests up to 3 times and to count a pass if any one run passes. That is pass@3. For a case with a pass rate of 60%, pass@3 ≈ 94%, so CI is almost always green, while 40% of customers get a wrong answer. Google describes a flaky test as a test "that exhibits both a passing and a failing result with the same code" [R6]; rerunning to hide it does not make the product better.
Scenario 2 (fictional): timeouts counted as fails. During a night of patchy network, the pass rate of every case drops by about 10%. The report shouts "regression across the board". Once the timeouts are separated out, the rate of the real answers has not changed.
8. Hands-on tutorial
8.1 Running the demo
python -m lab.demo l02
Output (copied from a real run, 2026-10-31):
n=3
Q-REFUND 3/3 rate=1.00 CI95=[0.44, 1.00] gate(0.8)=INCONCLUSIVE
Q-HOTLINE 3/3 rate=1.00 CI95=[0.44, 1.00] gate(0.8)=INCONCLUSIVE
Q-ADDR 3/3 rate=1.00 CI95=[0.44, 1.00] gate(0.8)=INCONCLUSIVE
n=30
Q-REFUND 30/30 rate=1.00 CI95=[0.89, 1.00] gate(0.8)=PASS
Q-HOTLINE 22/30 rate=0.73 CI95=[0.56, 0.86] gate(0.8)=INCONCLUSIVE
Q-ADDR 30/30 rate=1.00 CI95=[0.89, 1.00] gate(0.8)=PASS
n=200
Q-REFUND 194/200 rate=0.97 CI95=[0.94, 0.99] gate(0.8)=PASS
Q-HOTLINE 165/200 rate=0.82 CI95=[0.77, 0.87] gate(0.8)=INCONCLUSIVE
Q-ADDR 200/200 rate=1.00 CI95=[0.98, 1.00] gate(0.8)=PASS
pass^k for p=0.95: {1: 0.95, 3: 0.857, 10: 0.599}
Reading the output:
- With n = 3, all three cases are 3/3 and all are INCONCLUSIVE: 3 runs are not enough to decide.
- With n = 30, Q-HOTLINE turns out to be unstable (22/30), and its interval straddles 0.8.
- With n = 200, the interval of Q-HOTLINE narrows but still straddles 0.8. Its true rate lies close to the threshold, so it takes a very large number of runs to settle it, or a product decision about the threshold.
8.2 Running the tests
python -m pytest tests/test_l02_repeat.py -v
8.3 Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
ValueError: n must be positive |
wilson or run_case called with n = 0 |
Check the number of valid runs after removing errors |
| The gate is always INCONCLUSIVE | High threshold, few runs | Increase n for that case, or review the threshold |
9. Independent exercise
Problem: How many runs does a gate need?
Requirements: complete exercises/test_l02_how_many_runs.py:
- TODO 1: find the smallest n such that n/n passes give gate PASS at the threshold 0.9. Compute it with a loop, then assert the figure.
- TODO 2: run Q-CANCEL 200 times (
faq-v1@sim-2026-09, seed 1) and assert the gate result at the threshold 0.8. - Write 3-5 sentences: with a budget of 2,000 calls for 8 cases, how do you allocate it?
Deliverable: the test file and the budget allocation paragraph.
Acceptance criteria: the test file passes; the allocation paragraph uses risk (risk in the dataset) and
closeness to the threshold.
Hint: repeat.gate(CaseResult("X", passes=n), 0.9, min_valid=1).
9.1 Extra exercise: pass@k and an honest report line
Time: 15-30 minutes. File: exercises/test_l02_extra_report_line.py.
Problem: The team's CI "reruns up to 3 times". You need a figure that shows what that setting really measures, and a report line that never lets "100%" stand alone without its N.
Requirements:
- Run
python -m pytest exercises/test_l02_extra_report_line.py. Untouched, 9 tests fail with a message starting withFailed: TODO. - Write
pass_at_k(p, k)(§4.5, worked example). - Write
summary_line(result, threshold)in exactly the format of the docstring: k/N over valid runs only, the number of environment errors on its own (§4.1), the 95% interval and the gate result (§4.3). - Run again until you get
9 passed. Write one sentence: which configuration doestest_summary_line_on_a_real_runuse, and why does that very configuration expose the "timeouts counted in N" mistake?
Acceptance criteria: 9 tests pass; the answer to step 4 says that this configuration has timeouts.
Hint: the f-string f"{x:.2f}" rounds to 2 decimal places; result.interval() returns (low, high).
9.2 Real-world scenario: a food-delivery app's "rerun up to 3 times" CI (fictional)
The food-delivery app "Ăn Liền" (fictional) has an order-support chatbot. Today its CI runs each case once and reruns it up to 3 times if it fails. Last night the team tried running each case 30 times:
| Case | Topic | Risk | Pass | Fail | Timeout |
|---|---|---|---|---|---|
| C1 | Refund when the restaurant cancels the order | high | 29 | 1 | 0 |
| C2 | Delivery fee by distance | high | 24 | 3 | 3 |
| C3 | Restaurant opening hours | low | 30 | 0 | 0 |
| C4 | Vegetarian dishes on the menu | medium | 18 | 12 | 0 |
| C5 | An expired discount code | medium | 27 | 0 | 3 |
| C6 | Greeting | low | 30 | 0 | 0 |
Budget: 1,200 calls per night.
Your task (45-60 minutes):
- For each case, compute the pass rate over the valid runs and the 95% interval (use
repeat.wilson); record environment errors separately. - Set a threshold for each risk level, apply the gate, and write PASS, FAIL or INCONCLUSIVE for each case.
- Explain what the current CI showed the team (and what it hid) for the worst case, using pass@3 and pass^3.
- Propose how to spend the 1,200 calls per night, and a rule for running more on INCONCLUSIVE cases.
A good answer has:
- A table of the six cases with valid k/N, the 95% interval and the timeouts in a separate column
- Thresholds that differ by risk, and each gate result justified by the interval, not by a single rate
- The two figures pass@3 and pass^3 for the same case, with one sentence on which is closer to what customers see
- A budget split that is not even, justified by risk and closeness to the threshold, with a total of at most 1,200
- A rule for running more that has all four points of §4.3
10. Knowledge check
0/19 done · Results are kept in this browser only.
P02 · True or false
P03 · Short answer
Why does a gate need three outcomes instead of two?
P04 · Short answer
The pass rate is 90%. Compute pass^5 and explain what it means for a customer who asks 5 questions.
P05 · Short answer
(Scenario) The team proposes "rerun up to 3 times, passing once counts as a pass". What measure is that, and why is it dangerous for a customer service chatbot?
P06 · Put in order
P08 · Multiple choice
P09 · Spot the bug
The gate() function below is copied from lab/repeat.py (without the docstring), with one line modified. Find the bug.
def gate(result, threshold=0.9, min_valid=10):
if result.valid_runs < min_valid:
return "INCONCLUSIVE"
low, high = result.interval()
if high >= threshold:
return "PASS"
if high < threshold:
return "FAIL"
return "INCONCLUSIVE"P10 · Recall
Lesson: L1-M06-L01P11 · Select all that apply
P12 · Short answer
In the demo in §8.1, Q-HOTLINE is 22/30 (0.73) at n = 30 but 165/200 (0.82) at n = 200. Do the two figures contradict each other?
P13 · True or false
P15 · Multiple choice
P16 · True or false
P17 · Apply now
In code-labs/l1-m06, do the extra exercise of §9.1 in exercises/test_l02_extra_report_line.py, then run python -m pytest exercises/test_l02_extra_report_line.py.
P18 · Fill in the blank
P19 · True or false
11. Summary
- Concepts: a pass rate is an estimate; the Wilson interval; the stability gate PASS/FAIL/INCONCLUSIVE; environment errors counted separately; pass@k and pass^k.
- Skills: running repeatedly and reading the interval; setting a gate; estimating the number of runs.
- Pitfalls: concluding from a few runs; rerunning until green; counting timeouts as fails; reporting a rate without N.
- You can now: set a defensible stability criterion for an LLM case.
12. References
| ID | Title | Publisher | URL | Accessed | Relevance |
|---|---|---|---|---|---|
| R1 | Interval Estimation for a Binomial Proportion (Brown, Cai, DasGupta), Statistical Science 16(2):101-133, 2001 | Institute of Mathematical Statistics | https://doi.org/10.1214/ss/1009213286 | 2026-10-31 | The Wald interval performs poorly; Wilson recommended for small n (quoted from the abstract via Crossref) |
| R2 | NIST/SEMATECH e-Handbook of Statistical Methods, §7.2.4.1 Confidence intervals | NIST | https://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm | 2026-10-31 | The Wilson method; the lower bound is never negative |
| R3 | Define success criteria and build evaluations | Anthropic | https://platform.claude.com/docs/en/test-and-evaluate/develop-tests | 2026-10-31 | Prioritising the number of questions |
| R4 | Demystifying evals for AI agents (Grace, Hadfield, Olivares, De Jonghe), 2026 | Anthropic | https://www.anthropic.com/research/demystifying-evals-for-ai-agents | 2026-10-31 | pass@k, pass^k |
| R5 | τ-bench (Yao, Shinn, Razavi, Narasimhan), arXiv 2406.12045, 2024 (preprint) | arXiv | https://arxiv.org/abs/2406.12045 | 2026-10-31 | Definition of pass^k |
| R6 | Flaky Tests at Google and How We Mitigate Them (John Micco), 2016 | Google Testing Blog | https://testing.googleblog.com/2016/05/flaky-tests-at-google-and-how-we.html | 2026-10-31 | Definition of a flaky test |
Key terms (English ↔ Vietnamese)
| English | Tiếng Việt |
|---|---|
| Pass rate | Tỷ lệ pass |
| Confidence interval (Wilson) | Khoảng tin cậy (Wilson) |
| Stability gate | Cổng ổn định |
| Inconclusive | Chưa đủ bằng chứng để kết luận |
| pass@k / pass^k | Ít nhất một lần đúng trong k lần / cả k lần đều đúng |
| Environment error | Lỗi môi trường (timeout, mạng) |