6.2.8 · D2AI Agents & Tool Use

Visual walkthrough — Code-generation agents

2,205 words10 min readBack to topic

Everything below refers to a picture. Read the picture first, then the words.


Step 1 — What is a single attempt, and what does mean?

WHAT. An agent writes code once, runs it in the sandbox, and the run either works or doesn't work. That is one attempt. We give it a name: the chance it works is the number . We also name how many attempts we're willing to make: , a positive whole number () — you cannot make "half an attempt" or "zero-and-a-bit" attempts, so counts .

WHY a single number? Because before we can talk about "trying again," we must nail down what one try even is. Every symbol must be earned — so is earned here: it is a fraction between and measuring "out of many single tries, how many succeeded."

  • If : the code never works on the first go (a broken agent).
  • If : it always works first try (a dream agent).
  • Real code agents sit around — less than half. That is the whole reason this page exists.

PICTURE. A bar of 10 tries; the coloured slice is the fraction that worked.

Figure — Code-generation agents

Step 2 — The trick: it is easier to count FAILURE

WHAT. We want the chance of succeeding at least once in several tries. That phrase "at least once" is slippery — success could happen on try 1, or try 2, or 3... lots of cases to add up. So we flip the question: what is the chance of the oppositefailing every single time?

WHY flip it? Because "at least one success" and "all failures" are the only two possible worlds. There is no third. If we know the size of one world, the other is just the rest. This is the complement idea, and it turns a messy sum into one subtraction.

The here is "the whole pie" — all possible outcomes together. Chop off the failure slice, and what remains must contain at least one success.

PICTURE. The full circle = 1. Two slices only: "all fail" vs "the rest."

Figure — Code-generation agents

Step 3 — The chance ONE try fails

WHAT. If a try succeeds with chance , then it fails with whatever is left over.

WHY? Success and failure of a single try are also a two-slice pie summing to . So failure fills the rest.

  • failure chance .
  • Edge check: failure (never fails, good). failure (always fails).

PICTURE. One try = one coin with a "works" face and a "breaks" face, sized and .

Figure — Code-generation agents

Step 4 — Failing times in a row: why we MULTIPLY

WHAT. The agent tries times (recall from Step 1: is a positive whole number, ). For every try to fail, try 1 fails and try 2 fails and ... and try fails.

WHY multiply, not add? Here is the key assumption, stated out loud: each retry is independent — the agent re-rolls fresh each time (fresh prompt, fresh generation), so one failure doesn't change the odds of the next. When independent things must all happen, their chances multiply. (Adding would give the chance of "one or another" — the wrong question.)

Think of a branching tree: each try splits into "fail" (weight ) and "success." To reach the "all-failed" leaf you walk down the fail branch times, multiplying the weight each step:

The exponent is literally how many times you walked down the fail branch.

PICTURE. A failure tree; the highlighted all-fail path multiplies each level.

Figure — Code-generation agents

Step 5 — Snap it together: the parent's formula

WHAT. Plug Step 4 back into the complement flip of Step 2.

WHY? Step 2 said "what we want = (all fail)." Step 4 just told us "all fail ." Substitute:

Term by term:

  • — the whole probability pie.
  • — one try's failure chance (Step 3).
  • — that failure repeated and multiplied across independent tries (Step 4).
  • The leading — the complement flip: subtract the all-fail world to get "at least one win" (Step 2).

Sanity on the edges:

  • : . One try = the plain single-shot rate. ✓
  • : . A broken agent never succeeds no matter how many tries. ✓
  • with : , so the whole thing . Given enough tries and any positive skill, success becomes near-certain. ✓

PICTURE. The curve vs for , with the parent's numbers marked.

Figure — Code-generation agents

Step 6 — Edge case: what if retries are NOT independent?

WHAT. Real agents see the error message and adjust — so retry 2 is smarter than retry 1. The tries are not independent, and is not constant.

WHY show this? Because Step 4's multiplication rule assumed independence. We must not let you walk into a scenario we hid. When feedback helps, let be the success chance on the -th try (rising as the agent learns from retrieved context and stack traces).

The symbol (capital Greek "pi") is a multiply-them-all sign: just means "take the factors and multiply the whole list together" — the multiply-cousin of the more familiar (add-them-all) sign. It runs over the same tries. If every , this collapses back to — our simple formula is the special case.

  • If feedback helps ( rising): each factor shrinks faster → success climbs even faster than the independent curve.
  • If feedback hurts (agent loops on the same wrong idea, the Step-4 mistake box): can stall or drop, and the curve flattens. This is the real-world failure mode.

PICTURE. Two curves: constant- (dashed) vs improving- (solid, steeper) vs stuck- (flat).

Figure — Code-generation agents

Step 7 — The benchmark version: pass@k with a fixed pool

WHAT. Benchmarks like HumanEval don't retry live. Instead they generate samples once, count how many () are correct, then ask: "if I randomly grab of those , chance at least one is correct?" That is pass@k. (Here is how many we draw, and it must satisfy so that we never try to draw more samples than exist — otherwise is not defined.)

WHY a different formula? Because now we are drawing without replacement from a fixed bag of samples — not re-rolling fresh coins. So instead of we count how many ways to pick all-wrong samples out of the wrong ones.

  • — ways to choose any samples from all (the whole pie of picks).
  • — ways to choose samples using only the wrong ones (the all-fail picks).
  • Their ratio = chance a random draw is all-wrong; that = at least one right. Same complement flip as Step 2!

Edge cases:

  • (no correct samples): , so pass@k . Can't pick a winner that isn't there. ✓
  • and large enough to force a correct pick: , pass@k . ✓

PICTURE. A bag of marbles, green (correct); we scoop and win if any is green.

Figure — Code-generation agents

The one-picture summary

Figure — Code-generation agents

The single figure ties it all: one coin () → flip to failure () → repeat and multiply across tries () → subtract from the whole pie () → the rising success curve. The benchmark box on the side shows the without-replacement cousin, pass@k.

Recall Feynman retelling — say it to a 12-year-old

Imagine your agent is a kid solving a puzzle who gets it right 4 times out of 10 on a single guess. That's . Now let the kid guess again and again — call the number of guesses . The clever move is: don't ask "how likely to win eventually?" — ask "how likely to lose every single time?" Losing once has chance . Losing twice in a row is , three times — the number shrinks fast, because to lose you have to be unlucky over and over. After 5 guesses, losing-every-time is only about , so winning at least once is about . Flip "all losses" over, and out pops . If the kid actually learns from each wrong answer, they win even faster. And when a test just makes guesses up front and grabs of them, the same "count the all-wrong picks and flip it" trick gives pass@k. Same idea, dressed three ways.

Recall Quick self-check

Why do we compute "all fail" instead of "at least one success" directly? ::: "At least one" spreads across many cases (win on try 1, or 2, or 3...); "all fail" is a single product. They're complements, so . Why do the failure chances multiply rather than add? ::: The tries are independent and must all fail together; independent "and" events multiply. Adding would overcount and could exceed 1. What does do as grows (with )? ::: It shrinks toward 0, so success rises toward 1 — more tries, near-certain success. When does the simple formula break? ::: When retries aren't independent (the agent learns from feedback), so changes per try; use instead. What is the valid range of ? ::: For retries, is any positive integer ; for pass@k it must also satisfy so is defined.

Related: Program synthesis · Prompt engineering · AI safety and alignment · back to the parent topic.