Example

Count what you send, and do not send the text you will not use

Run the capture helper over three strings and see characters counted as code points while bytes are counted as UTF-8, and the redacted mode carrying no text at all.

An extension that records what happened should not have to record the text to record what happened. The chapter’s claim is that the safe prompt is the prompt the extension never sees, and that the counts an observatory keeps should be counts of the thing that is actually sent.

Run it

Save this as capture.mjs and run it with Node 22 or later. It uses one built-in, TextEncoder, and no browser API.

// Chapter 4: count what you send, and do not send the text you will not use.

function summarizeText(text) {
  return {
    chars: [...text].length,
    bytes: new TextEncoder().encode(text).byteLength,
  };
}

function captureText(text, mode) {
  if (mode === "content") {
    return { ...summarizeText(text), text, redacted: false };
  }

  return { ...summarizeText(text), redacted: true };
}

const samples = [
  { label: "ascii", text: "hello" },
  { label: "accented", text: "café" },
  { label: "emoji", text: "🙂" },
];

for (const sample of samples) {
  const summary = summarizeText(sample.text);
  console.log(
    `${sample.label.padEnd(9)} chars=${summary.chars} bytes=${summary.bytes}`
  );
}

const captured = captureText("a secret the extension does not need", "content");
console.log(`content mode: redacted=${captured.redacted} chars=${captured.chars} bytes=${captured.bytes}`);

const redacted = captureText("a secret the extension does not need", "summary");
console.log(`summary mode: redacted=${redacted.redacted} hasText=${"text" in redacted}`);

Expected outcome

ascii     chars=5 bytes=5
accented  chars=4 bytes=5
emoji     chars=1 bytes=4
content mode: redacted=false chars=36 bytes=36
summary mode: redacted=true hasText=false

The three rows are the whole argument. Five ASCII characters are five bytes. Four characters of “cafe” with an accent are five bytes, because the accented letter is two bytes in UTF-8. One emoji is one character and four bytes. A character count and a byte count are different numbers, and a program that labels one as the other is wrong about a class of strings.

The last two lines are the redaction. In content mode the text is carried because the caller asked for it; in summary mode the counts are carried and the text is not present at all, so there is nothing to leak later.

The capture helper

The chapter’s own code, in full:

function summarizeText(text) {
  return {
    chars: [...text].length,
    bytes: new TextEncoder().encode(text).byteLength,
  };
}

function captureText(text, mode) {
  if (mode === "content") {
    return { ...summarizeText(text), text, redacted: false };
  }

  return { ...summarizeText(text), redacted: true };

captureText has two modes and no third. The redacted branch does not compute the text and then drop it; it never puts the text in the object, so the object cannot be logged by accident.

Mechanism and limitations

Documented: the chapter’s rule that the transport should not receive a sensitive string only to discard it later, and the helper above, are in the chapter’s own text. The chapter notes that the same count is reported elsewhere as a token estimate, and that a character count is not a token count.

Observed: the run above. “cafe” with an accent counted as four characters and five bytes, one emoji counted as one character and four bytes, and the summary mode produced an object with no text property.

Not shown. The byte count is the UTF-8 length, which is what a transport would send, and the model’s own tokenizer may count differently. The samples here are three short strings, chosen to make the difference visible, and they are not a corpus. The redaction protects the text; it says nothing about what the character and byte counts themselves reveal about the text, and a length is not always harmless. Nothing here shows that an observatory’s counts are sufficient to debug a real feature.

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.