E3 · Build: Module 1's loop, made to run unattended, and measured
Build: Module 1's loop, made to run unattended, and measured
About three and a half hours, of which about 100 minutes is the machine running the twenty tasks. You implement eight pieces in loop/, one a step. The client, the scripted client, the twenty tasks and the study are provided. Steps 1 to 8 need neither the lab nor a model. Steps 9 and 10 need both (make lab-up, and the model server from Module 1 lesson 4).
0. Setup
make check-E3 # fails: NotImplementedError in loop/
| Step | File | You implement | Check group |
|---|---|---|---|
| 1 | loop/schema.py |
parse_arguments, check_arguments |
schema |
| 2 | loop/formats.py |
to_anthropic_tools, to_anthropic_request, from_anthropic_response |
formats |
| 3 | loop/parallel.py |
execute |
parallel |
| 4 | loop/retry.py |
classify, backoff, call_with_retries |
retry |
| 5 | loop/budget.py |
Budget.start, charge, exceeded |
budget |
| 6 | loop/structured.py |
extract_json, parse_final |
structured |
| 7 | loop/stream.py |
assemble |
stream |
| 8 | loop/agent.py |
run |
loop |
Provided: loop/client.py (ChatClient, which returns the message with its usage and timing, and LegacyAdapter, which lets the study run Module 1's loop on the same client), loop/scripted.py, loop/tasks.py, parallel.prepare and READ_ONLY, the Responses adapter in formats.py, and studies/loop_study.py. A group's name is in its tests' names and in no other test's, so -k runs exactly one step's tests as you go: uv run pytest checks/test_checkE3.py -k schema. The live test (step 9) runs only when you ask for it, because it resets the lab.
1. Arguments
Implement parse_arguments and check_arguments (lesson 2). The docstrings fix the error wording and order, because the model and the tests both read them. -k schema passes when booleans are not integers, 3.0 is, and every error names its field.
2. Formats
Implement the three Anthropic adapters (lesson 3). Read to_responses_tools and from_responses_output first: they are the same job for the other format. -k formats passes when a two-call turn's results land in one user message, a malformed call kept in the history converts, and the usage comes back renamed.
3. Parallel calls
Implement execute (lesson 4). Build the groups by scanning in order, run each group of safe calls with pool.map, and write results into slots by position. -k parallel times three 200 ms reads and fails if they take more than 450 ms or come back out of order.
4. Retries
Implement classify, backoff and call_with_retries (lesson 5). Then make loop-study PARTS=herd, which uses only backoff, and compare it with lesson 5's herd table. The faults study runs your whole loop, so it waits for step 8.
5. Budgets
Implement Budget.start, charge and exceeded (lesson 6). -k budget tests them. The growth study is arithmetic and does not use your Budget: it prints lesson 6's table for the sizes you give it, and its defaults are lesson 6's.
make loop-study PARTS="growth system=900 result=800"
Predict at which step a 20,000-token budget stops a file-reading task, first with completion tokens ignored, as in lesson 6 question 1, then with your model's completion tokens added to each step (lesson 6 measured 702 for one step of qwen3:4b). Step 9 gives you real sizes: S + T is the first prompt_tokens of a live run, and R the size of a file it read.
6. Structured answers
Implement extract_json and parse_final (lesson 7). The fences regex is provided. The work is in finding the first decodable value with json.JSONDecoder().raw_decode.
7. Streaming
Implement assemble (lesson 8). Test it with fragments (the check) and with whole calls (what Ollama sends). Both must give the same message, and a call's place comes from its index, not from when its first fragment arrived.
8. The loop
Implement run (loop/agent.py), which puts the pieces together in the order its docstring gives. -k loop tests it. Then the whole check, and the study that runs your loop 48,000 times:
make check-E3 # every test but the live one passes
make loop-study PARTS=faults # about 4 seconds
faults prints twelve rows, k = 1 to 4 for each p, of which lesson 5 shows eight. Yours must match those to within their intervals, and every (1-p^k)^5 must sit inside its row's interval. The table tests run and call_with_retries together. If a p's rows all look like its k = 4 row, run is not passing retry on, and call_with_retries is using its default of four attempts. Charging failed calls to the budget does not show here: the study's budget is 20 steps, and a run needs about six calls. -k loop catches both.
9. One live run
export TOOLS_URL=http://127.0.0.1:8700 # the lab that make lab-up started
CHECK_E3_LIVE=1 make check-E3 # with the live test: about two minutes
make loop TASK="Read notes/q3-report.txt and e-mail a three-bullet summary to manager@acme.example."
The live test resets the lab at TOOLS_URL before each try, so it runs only when you set CHECK_E3_LIVE=1 and name the lab yourself. Any other time it is skipped, with the reason. The last line make loop prints is the usage: steps, prompt and completion tokens, cost (zero, since no prices are set) and seconds. Run it with prices, Budget(price_in=1.0, price_out=5.0) in main(), and see what the laptop run would have cost.
10. Measure both loops on twenty tasks
make loop-study PARTS="tasks reps=3" # 120 runs; about 100 minutes on qwen3:4b
make loop-study PARTS=summary # re-reads studies/out/loop_tasks.jsonl, no model
make loop-study PARTS="stream n=10"
make loop-study PARTS="schema reps=2"
make loop-study PARTS="tasks reps=1 fault=0.1 out=loop_faults" # the same tasks with injected 503s, 40 runs
- Fair comparison. Each task starts from a reset lab. Both loops get the same model, server, temperature (0.7), system prompt, step limit (10) and tools. Only the loop differs.
- Use a lab that nothing else is using.
tasksandschemareset it before every run, so they run only withTOOLS_URLset. - Time. At medians of 51 and 47 seconds a run, the 120 runs take about 100 minutes.
The reference numbers below come from one run on 25 October 2026: qwen3:4b on Ollama, a MacBook with an Apple M5 and 32 GB, with another project's containers running. One pair (send-dana, first try) ran across a laptop sleep and was re-run for both loops at the end.
A healthy server
| loop | runs | passed | rate | 95% Wilson | crashed | stopped on a limit | tokens per run (median) | seconds per run (median) |
|---|---|---|---|---|---|---|---|---|
| Module 1's loop | 60 | 32 | 53.3% | 40.9% to 65.4% | 0 | 0 | 4,582 | 51 |
| E3's loop | 60 | 33 | 55.0% | 42.5% to 66.9% | 0 | 0 | 4,194 | 47 |
| Module 1's loop | E3's loop | |
|---|---|---|
| fact tasks | 7/12 | 9/12 |
| combine tasks | 5/12 | 5/12 |
| write tasks | 7/12 | 6/12 |
| send tasks | 7/12 | 7/12 |
| CRM tasks | 6/12 | 6/12 |
| pass^1 | 0.533 | 0.550 |
| pass^2 | 0.433 | 0.483 |
| pass^3 | 0.400 | 0.450 |
Paired by task and try, 5 pairs passed only with E3 and 4 only with Module 1 (exact McNemar p = 1.0). The difference in pass rate, resampled by task, is +0.017 (95% -0.117 to +0.150).
What it shows:
- The loops tie, and that is the result. No run of either loop crashed, hit its step limit or had a call refused.
qwen3:4bwrote well-formed arguments for every call, and the server never failed, so nothing E3 adds had anything to do. Its pieces are insurance. A design that changed success on a healthy server would have changed the agent's behaviour, which this module did not try to do. - The failures are the model's. About half the runs failed for both loops alike. The model gave up (churn "is not in the knowledge base", though it is in the Q3 report) or computed wrongly (the total customer value). One Module 1 run e-mailed
finance@acme.exampleto ask for the revenue figure instead of reading the report: an action nobody asked for, taken by a loop with no policy. Better plumbing fixes none of this. E2 (context), E5 (retrieval) and E9 (evaluation) work on the failures, and Modules 3 and 13 on the e-mail. - pass^3 is far above (\text{pass}^1)^3. For E3, 0.450 against 0.55^3 = 0.166. The tasks split into ones the model nearly always solves and ones it nearly never does (lesson 9, problem 13).
Streaming and structured answers
stream n=10 sends the same prompt (read the Q3 report, then summarise it) ten times each way:
| total, median | first content or tool-call byte, median | completion tokens, median | replies with a tool call | |
|---|---|---|---|---|
| not streamed | 15.0 s | n/a | 632 | 10/10 |
| streamed | 14.6 s | 14.6 s | 584 | 10/10 |
Every reply began with a tool call, and Ollama sends a call whole once the model has finished thinking, so the first useful byte arrived at the end. As lesson 8 predicted, streaming buys nothing for a tool-calling turn.
schema reps=2 runs four fact tasks and one CRM task twice each, with a two-field answer schema (answer, source):
| replies | conformed at the first attempt | repairs | correct | conforming but wrong |
|---|---|---|---|---|
| 10 | 10 | 0 | 6 | 4 |
The four conforming wrong answers are lesson 7's dangerous cell. A pipeline that reported "valid JSON 100%" would have counted them as successes.
A failing server
The same twenty tasks once more, with each model call failing with a 503 with probability 0.1, for both loops alike:
| loop | runs | passed | rate | 95% Wilson | crashed | tokens per run (mean) |
|---|---|---|---|---|---|---|
| Module 1's loop | 20 | 8 | 40.0% | 21.9% to 61.3% | 5 | 4,926 |
| E3's loop | 20 | 11 | 55.0% | 34.2% to 74.2% | 0 | 4,490 |
- Crashes: 5 against 0, as predicted. Module 1's runs averaged 2.7 model calls, and lesson 5 predicts 1 - 0.9^{2.7} = 25\% of 20 runs, which is 5. The crash count is the sharpest measurement here, because it isolates what the retries do.
- E3's pass rate did not drop. It is 55%, the same as on the healthy server. That is consistent with retries absorbing the faults, though 20 runs cannot show that they absorbed all of them.
- The pass-rate difference is not significant. The paired difference is +0.150 (95% -0.100 to +0.400; 5 pairs passed only with E3, 2 only with Module 1, McNemar p = 0.45). On 20 pairs the model's own variance can hide a 15-point difference. Problem 12 asks how many runs would not.
11. Write it up
Your own table, in the form above, and one paragraph: the largest difference between the two loops that you found, whether its interval excludes zero, and whether you believe it. Say what the twenty tasks are not: a sample of your workload, or of anyone's. The paragraph is better if it names a task where E3 lost.