Example

Know the pressure before you attempt a request

Run the context-state and can-attempt helpers and see an unreported quota become unknown, and an unknown remaining balance become an allowance rather than a refusal.

When the context window fills, the chapter’s claim is that the extension has to answer two questions before it attempts a request: how full is the window, and is there room for this request. Both answers have an unknown state, and neither threshold is a property of the browser.

Run it

Save this as pressure.mjs and run it with Node 22 or later. No browser API.

// Chapter 10: know the pressure before you attempt a request.

function contextState({ inputUsage, inputQuota }) {
  if (!Number.isFinite(inputUsage) || !Number.isFinite(inputQuota)) {
    return { kind: "unknown" };
  }

  const pressure = inputUsage / inputQuota;
  if (pressure >= 0.9) return { kind: "critical", pressure };
  if (pressure >= 0.75) return { kind: "high", pressure };
  return { kind: "normal", pressure };
}

function canAttempt({ remaining }, estimatedInput, reserve = 512) {
  if (!Number.isFinite(remaining)) return { allowed: true, confidence: "unknown" };
  return {
    allowed: estimatedInput + reserve <= remaining,
    confidence: "estimated"
  };
}

for (const [label, usage, quota] of [
  ["fresh", 0, 9216],
  ["normal", 4000, 9216],
  ["high", 7500, 9216],
  ["critical", 8800, 9216],
  ["not reported", Number.NaN, 9216],
]) {
  const state = contextState({ inputUsage: usage, inputQuota: quota });
  const pressure =
    state.pressure === undefined ? "n/a" : state.pressure.toFixed(3);
  console.log(`${label.padEnd(12)} -> ${state.kind.padEnd(9)} pressure=${pressure}`);
}

for (const [label, remaining, estimate] of [
  ["room to spare", 9216, 800],
  ["tight", 1200, 800],
  ["over the line", 400, 800],
  ["remaining unknown", Number.NaN, 800],
]) {
  const decision = canAttempt({ remaining }, estimate);
  console.log(
    `${label.padEnd(18)} -> allowed=${String(decision.allowed).padEnd(6)} confidence=${decision.confidence}`
  );
}

Expected outcome

fresh        -> normal    pressure=0.000
normal       -> normal    pressure=0.434
high         -> high      pressure=0.814
critical     -> critical  pressure=0.955
not reported -> unknown   pressure=n/a
room to spare      -> allowed=true   confidence=estimated
tight              -> allowed=false  confidence=estimated
over the line      -> allowed=false  confidence=estimated
remaining unknown  -> allowed=true   confidence=unknown

The two unknown rows are the point. A quota the browser did not report is unknown, not normal and not critical. And an unknown remaining balance is allowed, with confidence: unknown: the runtime is the final authority, so a refusal on the grounds of a number the extension does not have would be refusing on a guess.

The confidence field is what tells the two kinds of true apart. estimated means the arithmetic ran on an estimate; unknown means the arithmetic did not run at all.

The two helpers

The chapter’s own code, in full.

function contextState({ inputUsage, inputQuota }) {
  if (!Number.isFinite(inputUsage) || !Number.isFinite(inputQuota)) {
    return { kind: "unknown" };
  }

  const pressure = inputUsage / inputQuota;
  if (pressure >= 0.9) return { kind: "critical", pressure };
  if (pressure >= 0.75) return { kind: "high", pressure };
  return { kind: "normal", pressure };
}
function canAttempt({ remaining }, estimatedInput, reserve = 512) {
  if (!Number.isFinite(remaining)) return { allowed: true, confidence: "unknown" };
  return {
    allowed: estimatedInput + reserve <= remaining,
    confidence: "estimated"
  };
}

The thresholds are policy choices, and the chapter says so: they reserve space for the next request and output, whose cost may not be known exactly in advance. The chapter also records that one observed session reported a quota of 9,216 with usage zero, and that this is one session’s configuration, not a fixed Prompt API constant.

Mechanism and limitations

Documented: the thresholds, the two helpers and the quota observation are in the chapter’s own text, and the code above is that text’s code. The chapter is explicit that the estimate must be labelled as an estimate, and that character count is not token count.

Observed: the run above. The five usage states produced normal, normal, high, critical and unknown, and the four remaining-balance cases produced two allowances and two refusals, one of which carried confidence: unknown rather than estimated.

Not shown. The quota of 9,216 is one observed session configuration, and it may vary with browser version, model, modality or creation options; the numbers here use it because the chapter records it, not because it is authoritative. The estimate of 800 tokens is a figure the driver chooses, and the estimated label says the arithmetic was done on an estimate rather than on a tokenizer’s count. Nothing here shows that refusing a request at the margin is the right behaviour, and the thresholds of 0.75 and 0.9 are declared policy.

Read the chapter .

Understand this example

Copy this prompt into your AI tool. No code runs here.

Apply this example

Copy this prompt into your AI tool. No code runs here.