Worked examples — Code-generation agents
Before any symbol appears, one reminder in plain words:
The scenario matrix
Every case this topic can throw at you falls into one of these cells. The worked examples below are tagged with the cell they hit, and together they fill every row.
| # | Case class | What's special about it | Covered by |
|---|---|---|---|
| A | Ordinary interior value | , small | Ex 1 |
| B | Large- / limiting behaviour | , what does approach? | Ex 2 |
| C | Degenerate low: | agent never succeeds — no amount of iteration helps | Ex 3 |
| D | Degenerate high: | agent always succeeds — one try is enough | Ex 3 |
| E | Inverse question | given a target , solve for required | Ex 4 |
| F | pass@k from raw counts | small — exact combinatorics, edge and | Ex 5 |
| G | Real-world word problem | budget/cost trade-off, units of dollars & minutes | Ex 6 |
| H | Exam twist: broken independence | why naive formula over-counts; the TDD/ mini-check | Ex 7 |

The figure above plots against for three values of . Notice the shape: every curve climbs fast then flattens toward (the dashed ceiling). The curve is the flat red line stuck at ; the curve jumps to immediately. Those two flats are cells C and D — the degenerate edges of the whole family.
Worked examples
Example 1 — the ordinary case (cell A)
Example 2 — the limit as attempts grow (cell B)
Example 3 — both degenerate edges (cells C and D)
Example 4 — inverse question: solve for (cell E)
Your agent has . You need overall success probability . What is the smallest number of attempts ?
Forecast: is weak (-in-), so this will need several tries — guess "around 8".
- Set up the inequality. . Why this step? We rearrange so the unknown exponent is isolated on one side.
- Apply the natural logarithm. . Why again? Same reason as Ex 2 — it is the tool that frees from the exponent, and its base cancels in the ratio below.
- Divide, and flip the inequality. is negative, so dividing by it reverses "" into "": Why watch the sign? Dividing an inequality by a negative number flips its direction — forgetting this is the #1 exam error here.
- Round up to the next integer. .
Verify: ✓; and , just short — confirming is the smallest that clears the bar. ✓
Example 5 — pass@k from raw counts, including edges (cell F)
On one benchmark problem you sampled candidate solutions; were correct. Compute the exact pass@2 using the parent's estimator, then handle two edges: correct, and correct.
The estimator: , where (" choose ") counts the ways to pick items from ignoring order.
Forecast: with of good and picks, you'll usually catch at least one good — guess "around 0.7".
- Count all ways to pick 2 of 5. . Why this step? This is the denominator — the total, equally-likely draws of samples.
- Count all-wrong draws. Only wrong solutions exist; ways to pick 2 wrong: . Why this step? The numerator counts draws that miss every correct one — the "you got unlucky" outcomes.
- Form the fraction and complement. . Why complement? Same trick as Ex 1: "at least one correct" "not (all wrong)".
- Edge : numerator equals denominator . (No correct samples can never pass.)
- Edge : , and . (All correct always pass.) Why these edges matter? They match cells C and D but for the pass@k formula — the two degenerate ends must give and , and they do.
Verify: main answer lands on the forecast; edges give and ; and note the convention whenever , which is exactly why produced in the numerator. ✓
Example 6 — real-world cost trade-off (cell G)
Each agent attempt costs $0.03 in API tokens and takes 8 seconds. With , your policy is "retry until success, capped at attempts". You want . (a) What ? (b) What is the expected cost per solved task if you retry only until first success?
Forecast: From Ex 2 we saw tries reaches ; should need fewer — guess .
- Find for . . Why this step? Same inverse- method as Ex 4.
- Check the cap's success. ✓.
- Expected attempts until first success (unbounded retry, geometric mean) attempts. Why ? For an independent "keep flipping until heads" process, the average number of flips is one over the success probability — a standard result of the geometric distribution.
- Convert to money and time. Cost = 2.5 \times \0.03 = $0.075= 2.5 \times 8 = 20\times \/attempt $).
Verify: (near the forecast of 6). Units: 2.5\,\text{attempts}\times 0.03\,\tfrac{\}{\text{attempt}} = 0.075,$2.5 \times 8,\tfrac{\text{s}}{\text{attempt}} = 20,\text{s}$ — dollars and seconds, as required. ✓
Example 7 — exam twist: independence breaks (cell H)
A student claims: "My agent has per try and tries, so overall success is ." Their agent actually reads its own error messages and improves each round. Is the true success probability higher or lower than , and why? Also verify the divisor-check bound the parent used for is_prime.
Forecast: does learning from errors help or hurt? (Helps — later tries are informed, so the real number should be higher than the independence estimate.)
- Name the assumption. The formula assumes independence: every attempt has the same fixed , ignoring what happened before. Why this step? You can only judge a formula once you know its assumption.
- Compare to reality. A real agent uses the error trace, so attempt 2's success chance is conditioned on attempt 1's failure and is usually higher than the raw . Successive attempts are positively informative, not independent. Why this direction? Learning removes already-ruled-out mistakes, so the effective per-round rises. Therefore true — the independence formula is a conservative lower bound for a debugging agent.
- The check (why the prime loop stops at ): if with , then . So any composite number must have a factor at or below ; checking past it is wasted work. For , , so we test divisors ; none divide , so it is prime. Why include this? It's the classic exam "explain the bound" that pairs with the agent's TDD example — a reasoning check, not just a plug-in.
Verify: The independence number is correct as a bound; the true value is for a learning agent. And has no divisor in , confirming primality with only tests instead of . ✓
Recall Self-test (reveal after trying)
One-try success , three attempts — ? ::: With , does raising to a million ever help? ::: No — stays ; iteration multiplies an existing chance, it can't create one. Given , smallest for ? ::: (since ). pass@2 with , ? ::: . Why does a real debugging agent beat the independence formula? ::: It conditions on past errors, raising the effective per-round success, so the true is at least the independence estimate.
"Fail-fail-fail, then flip" — compute the chance of failing every time , then flip it with . The two degenerate ends fall out for free: never flips (), flips instantly ().
Prerequisites & neighbours: Large Language Models · Tool use in AI systems · Automated testing · Program synthesis · Retrieval-Augmented Generation · Prompt engineering · AI safety and alignment · back to Code-generation agents.