Module: L3-M06 · Duration: ~120 minutes (estimate, including the independent exercise) · Previous: L02 (retries, timeouts, idempotency) · Next: L04
1. Introduction
One successful run says the agent can do the task, not that it does it every time. Task t03 (borrow B002, then notify the student) succeeds on the first run; run it 20 times and only 12 succeed, because the simulated model occasionally writes the wrong notice text.
L1-M06-L02 introduced the pass rate as an estimate, the Wilson interval, and the two measures pass@k and pass^k through the formula p^k for a given rate p. This lesson takes the next step, the way the original papers do: run each task n times, count the c successes, and estimate pass@k and pass^k from (n, c) with unbiased estimators, computed as exact fractions. Then compute the success-rate curve over k, for each agent configuration, and read it to make a decision.
Sources: pass@k following Chen et al. 2021 [R1], pass^k following τ-bench [R2], how to use them for agents following Anthropic [R3], the Wilson interval following NIST [R4]. What the lesson produces: lab/trials.py with functions tested against their definitions, the rate curves of four configurations, and in §9 a function you write yourself that computes the curve with fractions, run on a configuration you have not seen in the lesson.
2. Learning objectives
- Define an independent trial of a task, and state the conditions for trials to be independent. Read: §4.1.
- Compute pass@k = 1 − C(n−c, k)/C(n, k) and pass^k = C(c, k)/C(n, k) from n and c, as exact fractions, and explain why the version that plugs p = c/n into the formula is biased. Read: §4.2, §5.1. Practice: §8.2 (part A), §8.3, §9 (TODO 2).
- Compute and read the pass@k and pass^k curves over k, averaged over tasks. Read: §5.2. Practice: §8.2 (part C), §9 (TODO 2).
- Choose the right measure and k for a product, and show when pooling trials hides a broken task. Read: §6.1. Practice: §8.2 (part D), §9 (TODO 3).
3. Prerequisites and tools
- L1-M06-L02 (the pass rate is an estimate, the Wilson interval, p^k), L1-M06-L01 (non-determinism), L2-M07-L02 (pairing: the same items for both sides), L3-M03-L01 (tasks and end-state success criteria), and L01, L02 of this module. Code lab
code-labs/l3-m06. - Keep the meanings apart. In this lesson a trial is one complete run of a task; it differs from an attempt of a tool call in L02. A trial's success is
run["success"]: the agent says it finished and the end state is right and there is no duplicate effect. c/n is the number of successes over the number of trials of one task. - Variation between trials in the lab comes from two seeded sources: a slip of the simulated model (with a given probability, the model uses a wrong parameter at one step; t01 10%, t03 25%, t05 40%) and the probabilistic injected faults (L01). Every number is simulated.
4. Concept explanation
4.1 From one run to n independent trials
Anthropic's article says "Each attempt at a task is a trial" and notes that each trial must be "isolated", starting from a clean environment: shared state (leftover files, caches, exhausted resources) causes correlated failures, and trials that fail together because of one environment limit "are not independent" [R3]. τ-bench also defines both measures over "k i.i.d. task trials" [R2]: independent and identically distributed.
In the lab, trials.run_trials(tasks, config, n, seed) runs each task n times with trials 0 to n−1, each with a new Library, SimClock and Injector. The same (seed, trial) always gives the same run, so trial number t of every configuration meets the same slip and the same probabilistic injected faults: comparing safe with naive is a paired comparison (L2-M07-L02).
Three consequences for the tester. One: n must be chosen beforehand (L1-M06-L02). Two: with frontier models, "0% pass@100" "is most often a signal of a broken task, not an incapable agent" [R3]. Three: the clean environment must be tested (the test test_trials_are_independent_of_the_order_they_run_in).
4.2 pass@k and pass^k are estimates from (n, c)
An example before the formulas. A task runs 4 times: ✓ ✓ ✓ ✗ (c = 3). Draw 2 of the 4 trials, without replacement: there are 6 pairs. Pairs that are all successes: 3 (the pairs taken from the three ✓), so pass^2 = 3/6 = 1/2. Pairs with at least one success: all 6 (there is only one ✗, you cannot draw two ✗), so pass@2 = 1. Plugging p = 3/4 into the formula of L1-M06-L02 gives p² = 0.5625 (higher than 1/2) and 1 − (1/4)² = 0.9375 (lower than 1). The number of pairs is C(n, k) (n choose k, math.comb(n, k)): the number of ways to choose k trials out of n, ignoring order; C(4, 2) = 6, C(3, 2) = 3. The two formulas below are this counting, written for any n, c, k.
Chen et al. define pass@k for code generation: a problem is solved if at least one of k samples passes the unit tests. They generate n ≥ k samples per task (n = 200, k ≤ 100), count the c correct samples, and compute the unbiased estimator (equation 1) [R1]:
pass@k = E_task [ 1 − C(n−c, k) / C(n, k) ]
τ-bench proposes pass^k ("pass hat k") for tasks that need reliability, such as customer service: "the chance that all k i.i.d. task trials are successful, averaged across tasks", and gives the unbiased estimator [R2]:
pass^k = E_task [ C(c, k) / C(n, k) ]
How to read them: draw k trials without replacement from the n. C(n−c, k)/C(n, k) is the probability that all k draws fail; subtracting it from 1 gives "at least one success". C(c, k)/C(n, k) is the probability that all k succeed. Chen et al. write that their estimator is unbiased because it estimates (1 − pass@1)^k "as the probability of drawing k failed samples without replacement" [R1]. The lab's test enumerates every set of k trials with itertools.combinations and compares against the two formulas.
The trap is plugging the observed rate p_hat = c/n into the formula of L1-M06-L02. Chen et al. say it directly: one may be tempted to estimate pass@k as 1 − (1 − p_hat)^k, "but we show that it is biased", and in appendix A that approach "results in a consistent underestimate" [R1]. The intuitive reason: (c/n)^k is the probability of drawing k trials with replacement from the n that were run, that is, one successful trial can be drawn twice. Drawing with replacement "reuses" the successes, so (c/n)^k is higher than C(c, k)/C(n, k); for pass@k it reuses the failures too, so 1 − (1 − c/n)^k is lower. The 4-trial example above gives exactly those two directions; the test test_the_plug_in_values_differ_from_the_estimators checks them on many (n, c). At k = 1 all four are equal, and equal to c/n; τ-bench notes pass^1 = pass@1 = E[c/n] [R2]. Anthropic describes the direction of the two curves: pass@k rises with k, pass^k falls with k, and by k = 10 they "tell opposite stories" [R3].
5. Technical deep dive
5.1 lab/trials.py: exact fractions and input checks
pass_at_k(n, c, k) and pass_hat_k(n, c, k) return a fractions.Fraction, computed from math.comb (different from the same-named function pass_hat_k(p, k) = p^k of the L1-M06-L02 lab). pass_hat_k(10, 7, 3) is exactly 7/24, and two ways of computing are compared with ==. math.comb(a, k) is 0 when k > a, so pass@k = 1 when n − c < k and pass^k = 0 when c < k; that is also the first line of Chen et al.'s numpy code (if n - c < k: return 1.0) [R1]. They give a stable product form (figure 3) because computing directly produces "very large numbers and numerical instability" [R1]; with Fraction, the lab computes directly, and one test compares with the product form at n = 200, k = 100.
_check raises ValueError when n, c, k are not integers (including True and 10.0), n < 1, c is outside [0, n] or k is outside [1, n]. plugin_pass_at_k and plugin_pass_hat_k are there for comparison, not for reporting.
wilson(c, n, z=1.96) is the Wilson score interval following the formula of the NIST handbook, section 7.2.4.1 [R4]; because it has a square root, it returns floats, rounded to 4 digits for printing tables. With places=None it returns the unrounded value, and the L05 gate compares that value with the target: the lower bound of 17/21 is 0.59999…, and rounding it to 0.6 would "meet" a target of 0.6. z must be a finite number greater than 0. How to read the interval is already in L1-M06-L02 §4.2; what is new here is that it is computed per task, next to that task's pass^k.
5.2 The success-rate curve, per task
curve(counts, ks) takes {task: (n, c)} and returns, for each k, the mean over tasks of pass@k and pass^k (the expectation E_task in both papers). It raises ValueError when counts is empty or a k exceeds the n of a task: every point of the curve must use the same tasks.
The mean over tasks differs from pooling all trials. With two tasks, one 4/4 and one 0/4, the mean pass^2 is 1/2 (one task always works, one never does); pooling the 8 trials into 4/8 and computing pass^2 gives 3/14, a number that describes no task (the test test_a_curve_is_the_mean_over_tasks_not_the_pooled_rate). Part D of the demo shows the more dangerous direction: pooling the 100 runs of safe gives 82/100 with the Wilson interval (0.7333, 0.883), which looks fine, while two tasks have their own rates below 0.8.
6. QA perspective
6.1 Reading a curve to make a decision
Part C of the demo puts three curves side by side. Under the same fault plan flaky, naive has pass@1 = 0.630 and pass^4 = 0.230; safe has pass@1 = 0.820 and pass^4 = 0.548. The curve of safe under flaky coincides with its curve without faults: with that plan, every injected fault is recovered by retrying, so what is left is only the model's slips. That is a useful finding: to make safe better, you have to fix the tasks (t03, t05), not the retry policy.
Four questions when reading a curve:
- Which k is the product's k? Anthropic's article says pass@k suits tools where "one success matters", pass^k suits agents where "consistency is essential", and pass^k "especially matters for customer-facing agents" [R3]. A student who asks the assistant every day of the school week (5 days) is asking a question closer to pass^5 than to pass@1. Choose k before running, and write it into the report's targets (L05).
- What does a high pass@k say? Here pass@8 is close to 1 for all three configurations: almost every task succeeds at least once in 8 trials. For an agent with side effects, "try 8 times and pick the right one" is not how a student uses the product; a high pass@k is not evidence of reliability.
- Each task, not just the mean. The curve is a mean; part B shows that t02 and t04 always succeed (pass^8 = 1), while t03 has pass^8 = 0.004. A report needs a per-task table next to the curve, with Wilson intervals.
- Is n big enough? With n = 20, the Wilson interval of 17/20 is (0.6396, 0.9476): wide. At k close to n, pass^k rests on very few all-success combinations (C(c, k) is small), so the end of the curve is very sensitive to a single trial; do not read pass^16 at n = 20 like pass^16 at n = 200. τ-bench chose to trade quantity for quality: a small set of high-quality tasks, run many times with pass^k [R2]; that is the lab's direction, but the lab has only five tasks and one seed.
7. Real-world scenarios
Scenario 1 (fictional): "Ninety percent is good enough". An internal dashboard reports "success rate 90%" for a scheduling assistant, pooling every trial of 30 tasks, 10 trials per task (270/300). The tester recomputes per task: 27 tasks at 10/10, 3 tasks at 0/10, and all 3 are cancellation tasks. Treating the 300 pooled trials as one task gives pass^3 = 0.728; taking the mean over tasks gives pass^3 = 0.9 (27 tasks with pass^3 = 1, 3 tasks with 0). The two numbers differ, but neither says that for someone who needs to cancel a booking the product is always broken; what shows the problem is the per-task table, not any mean. The report switches to a per-task table, and the 3 tasks at 0/10 are checked again to see whether the task or the grader is broken, before blaming the agent.
Scenario 2 (fictional): "Run it a few more times to make it look nice". After 20 trials, a task has pass^4 = 0.491, below the target of 0.5 (exactly t01 of part B). Someone proposes running 10 more times and pooling. The tester refuses, following the rerun rule of L1-M06-L02: the number of trials was chosen beforehand; a new run must use a new seed, the conclusion comes only from the new run, and only once.
8. Hands-on tutorial
8.1 Setup
cd academy-content/code-labs/l3-m06
python3 -m venv .venv && source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install -r requirements.txt
python -m pytest # 145 passed
8.2 Run the demo
python -m lab.demo l03
Output (copied from a real run on Python 3.14.6 and 3.12.3, Linux, 2026-11-05; both versions gave identical output). Every run is simulated:
A. one task with n=10 trials and c=7 successes: estimators against plug-in values
k=1: pass@k=7/10 (plug-in 0.700) pass^k=7/10 (plug-in 0.700)
k=2: pass@k=14/15 (plug-in 0.910) pass^k=7/15 (plug-in 0.490)
k=3: pass@k=119/120 (plug-in 0.973) pass^k=7/24 (plug-in 0.343)
k=5: pass@k=1 (plug-in 0.998) pass^k=1/12 (plug-in 0.168)
k=10: pass@k=1 (plug-in 1.000) pass^k=0 (plug-in 0.028)
B. safe, no faults, 20 trials per task: c/n, Wilson 95%, pass^k per task
t01: 17/20 wilson=(0.6396, 0.9476) pass^4=0.491 pass^8=0.193
t02: 20/20 wilson=(0.8389, 1.0) pass^4=1.000 pass^8=1.000
t03: 12/20 wilson=(0.3866, 0.7812) pass^4=0.102 pass^8=0.004
t04: 20/20 wilson=(0.8389, 1.0) pass^4=1.000 pass^8=1.000
t05: 13/20 wilson=(0.4329, 0.8188) pass^4=0.148 pass^8=0.010
C. curves (mean over the five tasks), k = 1, 2, 4, 8, 16
safe / none: pass@k 0.820 0.945 0.996 1.000 1.000 | pass^k 0.820 0.695 0.548 0.441 0.401
safe / flaky: pass@k 0.820 0.945 0.996 1.000 1.000 | pass^k 0.820 0.695 0.548 0.441 0.401
naive / flaky: pass@k 0.630 0.836 0.963 0.999 1.000 | pass^k 0.630 0.424 0.230 0.091 0.006
D. the same 100 runs pooled into one rate hide which task fails
pooled: 82/100 wilson=(0.7333, 0.883); tasks below 0.8 on their own: ['t03', 't05']
Reading the output: part A: one task at 7/10. From k = 2 the estimators and the plug-in values part ways: pass@2 = 14/15 ≈ 0.933 against 0.910; pass^2 = 7/15 ≈ 0.467 against 0.490. pass@5 is already 1 (only 3 failures); pass^10 = 0 while the plug-in still reports 0.028. Part B: per task for safe without faults; t02, t04 always succeed. Part C: each row is five pass@k values and then five pass^k values at k = 1, 2, 4, 8, 16. Part D: pooling hides (§5.2).
8.3 Try it yourself
Save this as try_l03.py in the lab's root folder, run python try_l03.py, then delete the file:
from lab import trials as T
for k in (1, 2, 4, 6, 8):
print(k, T.pass_at_k(8, 6, k), T.pass_hat_k(8, 6, k), T.fmt(T.plugin_pass_hat_k(8, 6, k)))
print(T.wilson(6, 8), T.wilson(60, 80))
cur = T.curve({"x": (8, 6), "y": (8, 8)}, [1, 2, 4])
print({k: (v["pass@k"], v["pass^k"]) for k, v in cur.items()})
Output (Python 3.14.6 and 3.12.3, Linux, identical):
1 3/4 3/4 0.750
2 27/28 15/28 0.562
4 1 3/14 0.316
6 1 1/28 0.178
8 1 0 0.100
(0.4093, 0.9285) (0.6452, 0.8319)
{1: (Fraction(7, 8), Fraction(7, 8)), 2: (Fraction(55, 56), Fraction(43, 56)), 4: (Fraction(1, 1), Fraction(17, 28))}
Reading: one task at 6/8; each line is k, pass@k, pass^k and the plug-in pass^k (0.75^k). The Wilson line: 6/8 and 60/80 have the same rate of 0.75, but the interval for n = 8 is almost three times as wide. The last line: the curve of 6/8 and 8/8, pass^2 = 43/56 is the mean of 15/28 and 1.
8.4 Run the lesson's tests
python -m pytest tests/test_l03_trials.py -v
Result: 26 passed. Read test_estimators_equal_the_share_of_k_subsets, test_estimators_equal_the_closed_forms_of_the_papers and test_wilson_matches_the_nist_formula_and_never_leaves_0_1.
8.5 Troubleshooting
| Symptom | Cause | Fix |
|---|---|---|
ValueError: need 1 <= k <= n |
k is larger than the number of trials of a task | Choose k ≤ n; or run more trials, chosen beforehand |
Two "equal" fractions but == is False |
One side is a float | Keep Fraction until the printing step; print with T.fmt |
| The curves of two configurations coincide | The fault plan does not reach the place where the two configurations differ | Look at run["faults"]; compare exactly the trials with the same trial number |
| The result changes when the task order changes | There is shared state between trials | Each trial must build a new environment (§4.1) |
9. Independent exercise
Problem: The configuration retry_new_key under the plan flaky, 12 trials per task, seed 7 (exercises/ex_trials_l03.json). Targets set beforehand: pass^3 ≥ 0.2 for every task and a Wilson lower bound ≥ 0.3.
Requirements: complete exercises/test_l03_trials.py:
- TODO 1:
COUNTS: the number of successes c of each task. - TODO 2: the function
my_curve(counts, ks): for each k inks, the pair (pass@k, pass^k) averaged over the tasks, asFraction, using onlymath.combandFraction. The test compares it withT.curveon the counts of TODO 1 and on two other sets of counts from the data file, and blocks the lab's measure functions while your function runs. - TODO 3:
MISSING: for each task that misses at least one target, the sorted list of the targets it misses ("pass^k","wilson").
Test data: exercises/ex_trials_l03.json.
Deliverable: the two variables and the function above, in the file exercises/test_l03_trials.py itself.
Acceptance criteria: python -m pytest exercises/test_l03_trials.py passes (3 tests). The test rejects floats in the result of my_curve.
Hints: TODO 1 needs a real run; do not guess from the slip rates. For TODO 2, check your function by hand on the 4-trial example of §4.2 first, and only then take the mean. For TODO 3, a task can pass one target and miss the other.
10. Knowledge check
0/10 done · Results are kept in this browser only.
P02 · True or false
P03 · Fill in the blank
P04 · Select all that apply
P05 · Put in order
P06 · Match
P07 · Spot the bug
A tester wrote this function to compute the pass^k curve for a report. Find the bug.
def pass_hat_curve(runs, ks):
n = len(runs)
c = sum(r["success"] for r in runs)
return {k: Fraction(math.comb(c, k), math.comb(n, k)) for k in ks}P08 · Essay
A library assistant has pass@8 = 1.000 and pass^8 = 0.441 (mean over tasks). A manager reads the first number and says "it always works". Write three to five sentences answering them.
P10 · Recall
Lesson: L1-M06-L0211. Summary
- Concepts: independent trials from a clean environment [R3]; pass@k and pass^k are unbiased estimators from (n, c), averaged over tasks [R1][R2]; the plug-in version is biased [R1]; Wilson intervals per task [R4].
- Skills: compute the two measures as exact fractions and test them against their definitions; compute and read the rate curve; compare configurations with paired trials.
- Traps: plugging c/n into p^k; pooling all trials; reading a high pass@k as reliability; choosing k after seeing the results; trusting the far end of a curve with a small n.
- You can now: say with what probability an agent does a task every time, with its uncertainty, and point out which tasks pull the number down.
12. References
| ID | Title | Publisher | URL | Accessed | Relevance |
|---|---|---|---|---|---|
| R1 | Evaluating Large Language Models Trained on Code (Chen et al.), arXiv 2107.03374, 2021 | arXiv | https://arxiv.org/abs/2107.03374 | 2026-11-05 | pass@k, the unbiased estimator (equation 1), the stable product form (figure 3), the plug-in version is biased (appendix A) |
| R2 | τ-bench: A Benchmark for Tool-Agent-User Interaction in Real-World Domains (Yao, Shinn, Razavi, Narasimhan), arXiv 2406.12045, 2024 | arXiv | https://arxiv.org/abs/2406.12045 | 2026-11-05 | pass^k: all k i.i.d. trials succeed, averaged over tasks; the estimator C(c,k)/C(n,k) (section 3) |
| R3 | Demystifying evals for AI agents | Anthropic | https://www.anthropic.com/research/demystifying-evals-for-ai-agents | 2026-11-05 | Trials, clean environments, correlated failures; pass@k and pass^k for agents; 0% over many trials is often a broken task |
| R4 | NIST/SEMATECH e-Handbook of Statistical Methods, §7.2.4.1 | NIST | https://www.itl.nist.gov/div898/handbook/prc/section2/prc241.htm | 2026-11-05 | The Wilson interval formula; the lower bound is never negative |
Glossary (English ↔ Tiếng Việt)
| English | Tiếng Việt |
|---|---|
| Trial (of a task) | Lần chạy: một lần chạy trọn một tác vụ từ môi trường sạch; khác lần thử (attempt) của một lời gọi tool ở L02 |
| Unbiased estimator | Ước lượng không chệch: kỳ vọng của nó bằng đúng đại lượng cần ước lượng |
| pass@k / pass^k (from n, c) | 1 − C(n−c,k)/C(n,k) / C(c,k)/C(n,k), trung bình theo tác vụ |
| Plug-in estimate | Ước lượng thay thế: đưa c/n vào công thức của p; bị chệch |
| Success-rate curve | Đường tỷ lệ thành công: pass@k và pass^k theo k |
| Slip (lab) | Lỗi nhầm: model mô phỏng dùng sai tham số ở một bước, với xác suất cho trước |