economics · lesson 8 of 9 · sources checked 2026-11-20
build cost guards and budget gates
This lesson builds the guard chain that stops spend before it occurs, and a test that proves each guard stops a lab job.
status of this lesson: documented in my test systempurpose
A report of the spend after a job shows a loss, but it does not stop it. A guard that acts before the spend can stop it, but a guard with no test can fail and give no sign. Thus test each guard with a lab job that runs away.
objectives
- Name the four guards of the guard chain and the place of each guard in a job.
- Explain why a pre-run estimate cannot stop each job that runs away.
- Build a byte budget and a kill switch for a lab job.
- Test each guard with a lab job that runs away, and record its refusal row.
new terms
- spend cap
- A limit on the cost of a job or an account, with a set action when the cost reaches it.
- crawler trap
- A link structure or hidden link that leads a crawler into an endless or a flagged path.
how it works
A cost guard stops spend before it occurs. The guard chain is a set of four guards in a fixed order:
- A pre-run estimate. It computes the attempts of a job before the first request. It refuses a job above the attempt ceiling.
- A byte budget. It is a spend cap on the bytes of one run. The job stops when its bytes reach the budget.
- A cap for each site. It limits the requests or the spend for one target in one period.
- A kill switch. It is a stop command that the operator or an alarm sets from outside the job.
Each guard that stops a job writes a refusal row in the spend ledger. The row gives the guard, the requests, and the bytes at the stop. Thus the ledger shows each stop, and not only the spend.
the pre-run estimate
The benchmark platform, my test system, computes the attempt count of a sweep before the run, and it shows the count. It refuses a sweep above its attempt ceiling, and it gives the two numbers. It does not run a part of the sweep.
example A full factorial sweep has 625 candidates with 3 attempts each, thus 1,875 attempts. The ceiling is 500 attempts. The estimate refuses the sweep before the first request.
A pre-run estimate sees only the plan. It cannot see a job that grows after it starts.
the byte budget
status of this section: estimate, not measuredA job can grow after it starts. An example is a crawler trap: pagination with no last page. Each page links to a next page, and the job does not end.
The byte budget counts the bytes of each response. Before each request, the job compares its bytes with the budget. When the bytes reach the budget, the job stops. The check comes before the request, thus the overrun is at most one page.
example The budget is 2,000,000 bytes, and each page has 40,000 to 46,000 bytes. The job stops after 47 requests, at 2,021,000 bytes.
the cap for each site
A cap for each site limits the load on one target, also when many jobs use it. It counts the requests or the spend of all jobs for the target in one period.
example A target has a cap of 200 requests each day. Two jobs send 120 requests each. The cap stops the second job after its 80th request.
the kill switch
status of this section: estimate, not measuredThe kill switch is outside the job. Thus it works also when the code of the job has a fault. The job reads the switch before each request, and it stops when the switch is on.
example An alarm sets the switch when the spend of the last hour is three times the plan for that hour.
test each guard
A guard with no test can fail and give no sign. Thus test each guard with a lab job that runs away. The test has three parts:
- Run the job with no guard. It must reach the cap of the lab job.
- Run the job with one guard. It must stop with the reason of that guard.
- Check the refusal row: the guard, the requests, and the bytes.
example The test of a byte budget of 2,000,000 bytes passes when the job stops with the reason "byte budget". The bytes must be at or above the budget, and less than one page above it.
the two sides
| automation side | detection side |
|---|---|
| A guard refuses a run before the spend, not after it. A kill switch stops a job that runs away. | A guard that refuses a target also limits the load on that target. The site sees fewer retries from the operator. |
design decisions
| option | gain | cost | when it is correct |
|---|---|---|---|
| Pre-run estimate | It stops the job before the first request. | It sees only the plan, not a job that grows. | Each sweep and each job with a known plan |
| Byte budget for each run | It stops a job that grows after it starts. | The overrun can be one page. A low budget stops a real large page. | Jobs with an unknown page count, for example pagination |
| Cap for each site | It limits the load and the spend for one target over all jobs. | It must have one shared count for all jobs. | When many jobs use one target |
| Kill switch outside the job | It works also when the job code has a fault. | A person or an alarm must set it. | Each job that can spend a large part of the daily budget |
| Refuse, not truncate | The result is complete or absent, never partial. | No partial result is available. | Each sweep and each measurement |
| A budget gate for each day | It defers new work when the plan of the day is over the limit. | The work waits until the next day. | Work that can wait |
diagram
the guard chain of a lab job
code example
The example runs a lab job that runs away, with no network. It applies a byte budget and a kill switch before each request. Three tests prove that each guard stops the job, and that the job with no guard reaches the cap.
example The job and its pages are synthetic. No request leaves the process.
Apply a byte budget and a kill switch to a lab job that runs away, and prove with a test that each guard stops it.
import assert from "node:assert/strict";
// A lab job that runs away: pagination with no last page.
// No request leaves the process. Each page is a synthetic byte count.
type Row = { reason: string; requests: number; bytes: number };
type Guards = { byteBudget?: number; kill?: (requests: number) => boolean };
const LAB_CAP = 1000; // the hard cap of the lab job itself
const ledger: Row[] = [];
function pageBytes(n: number): number {
return 40_000 + (n % 7) * 1_000; // each page links to a next page
}
function stop(reason: string, requests: number, bytes: number): Row {
const row = { reason, requests, bytes };
ledger.push(row); // each stop writes a refusal row
return row;
}
function runJob(g: Guards): Row {
let bytes = 0;
for (let n = 0; n < LAB_CAP; n++) {
// The guards look before each request, not after the job.
if (g.kill?.(n)) return stop("kill switch", n, bytes);
if (g.byteBudget !== undefined && bytes >= g.byteBudget) {
return stop("byte budget", n, bytes);
}
bytes += pageBytes(n + 1);
}
return stop("lab cap", LAB_CAP, bytes);
}
// Test 1: with no guard, the job runs to the lab cap.
const none = runJob({});
assert.equal(none.reason, "lab cap");
// Test 2: the byte budget stops the job, and the overrun is at most one page.
const budget = 2_000_000;
const byBudget = runJob({ byteBudget: budget });
assert.equal(byBudget.reason, "byte budget");
assert.ok(byBudget.bytes >= budget && byBudget.bytes < budget + 46_000);
// Test 3: the operator sets the kill switch after 25 requests.
const byKill = runJob({ kill: (n) => n >= 25 });
assert.equal(byKill.reason, "kill switch");
assert.equal(byKill.requests, 25);
for (const r of ledger) console.log(`${r.reason}: ${r.requests} requests, ${r.bytes} bytes`);
console.log("3 tests passed");output
One refusal row for each test: the reason, the requests, and the bytes. Then the count of passed tests.
lab cap: 1000 requests, 43003000 bytes
byte budget: 47 requests, 2021000 bytes
kill switch: 25 requests, 1073000 bytes
3 tests passedI ran the code with the Node runtime and the flag --experimental-strip-types. The code has no argument.
The first row shows the loss with no guard: the job sends each request until the cap of the lab job. The two other rows show the stop of each guard, with its numbers.
data
spend of a runaway lab job for each guard, median of 5 seeds
status of this figure: measured by me
source: Lab result of this course: runaway lab job with and without each guard, 5 seeds for each arm; date 2026-11-16
data table
| series | item | u |
|---|---|---|
| Pagination trap | no guard | 45.3004 |
| Pagination trap | pre-run estimate | 45.3004 |
| Pagination trap | byte budget | 8.0216 |
| Pagination trap | site cap | 3.7839 |
| Pagination trap | kill switch | 4.0182 |
| Pagination trap | all guards | 3.7839 |
| Factorial sweep | no guard | 40.6562 |
| Factorial sweep | pre-run estimate | 0 |
| Factorial sweep | byte budget | 8.0355 |
| Factorial sweep | site cap | 4.8798 |
| Factorial sweep | kill switch | 5.269 |
| Factorial sweep | all guards | 0 |
The chart shows the median spend of two lab jobs that run away, with each guard. In the pagination trap, the pre-run estimate does not lower the spend, because the plan does not show the trap. In the factorial sweep, the pre-run estimate refuses the job before the first request, thus its spend is zero.
Thus one guard is not sufficient. The pre-run estimate stops a large plan, and the guards in the run stop a job that grows.
failure modes
| problem | cause | correction |
|---|---|---|
| The guard stops the job only after a large loss. | The guard checks the spend after the job, not before each request. | Check each guard before each request. |
| The byte budget stops the job too late. | The budget counts only the bodies of the responses, and not the headers or the retries. | Count all bytes of each request and each response. |
| The cap for each site does not limit the load. | Each job keeps its own count, and many jobs use one target. | Keep one shared count for each target and each period. |
| The kill switch does not stop the job. | The switch is in the code of the job, and the code has a fault. | Put the switch outside the job. Make the job read it before each request. |
| The ledger shows the spend, but not the reason of a stop. | A guard stops the job and writes no row. | Make each guard write a refusal row with the guard, the requests, and the bytes. |
| A guard passes its test, but it does not work in a job. | The test did not run a job that runs away. | Test each guard with a lab job that must reach the lab cap with no guard. |
check questions
Name the four guards of the guard chain, and say when each guard looks at the job.
sample answer
The pre-run estimate looks at the plan before the first request. The byte budget, the cap for each site, and the kill switch look before each request of the run.
Explain why a pre-run estimate does not stop a crawler trap.
sample answer
A pre-run estimate sees only the plan. A crawler trap, for example pagination with no last page, grows after the job starts. Thus a guard in the run, for example a byte budget, must stop it.
Where does a lab job read its kill switch?
- In the code of the job, after the job ends
- Outside the job, before each request
- In the spend ledger, after the last request
answer
b. Outside the job, before each request
The kill switch is outside the job, thus it works also when the job code has a fault. The job reads it before each request.
A byte budget is 2,000,000 bytes, and each page has at most 46,000 bytes. Which refusal row proves that the guard works?
- lab cap: 1000 requests, 43003000 bytes
- byte budget: 30 requests, 1290000 bytes
- byte budget: 47 requests, 2021000 bytes
- byte budget: 60 requests, 2600000 bytes
answer
c. byte budget: 47 requests, 2021000 bytes
The row gives the reason of the guard. Its bytes are at or above the budget, and less than one page above it.
exercise
exercise Do this task in your own lab, with synthetic data only. Do not send a request to a site.
- Save the code example in an empty folder of your lab.
- Run it with Node and the flag
--experimental-strip-types. - Record the reason, the requests, and the bytes of each refusal row.
- Change the byte budget to 500,000 bytes.
- Run the code again.
- Record the new row of the byte budget.
- Write if the test still proves that the byte budget stops the job.
sample answer
The answer uses the synthetic job only.
The first run gives three rows. The job with no guard stops at the lab cap of 1000 requests and 43003000 bytes. The byte budget stops the job at 47 requests and 2021000 bytes. The kill switch stops the job at 25 requests and 1073000 bytes.
With a budget of 500,000 bytes, the byte budget stops the job at 12 requests and 516000 bytes. The bytes are at or above the budget, and less than one page above it. Thus the test passes, and it still proves that the byte budget stops the job.
sources
- My model of the guard chain, the byte budget, the cap for each site, and the kill switch, 2026-11 (not public).
- Benchmark platform, design (my test system): the pre-run estimate and the attempt ceiling of a sweep, 2026-11 (not public).