Skip to content

Commit c4f5752

Browse files
committed
fix(tokenize): zero tokenize_ms when neither payload tokenized
1 parent 34a4fe1 commit c4f5752

1 file changed

Lines changed: 9 additions & 1 deletion

File tree

workers/src/tokenize.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,19 @@ export async function measurePayloadShape(
9999
]);
100100
const tokenize_ms = Math.round((performance.now() - start) * 1000) / 1000;
101101

102+
// A `0` from countTokensSafe on empty text is a trivial short-circuit, not
103+
// a real tokenization — only a non-null result on non-empty text proves the
104+
// encoder ran. If neither payload was actually tokenized, zero out
105+
// tokenize_ms to preserve the documented "skipped/failed" signal.
106+
const tokenizerRan =
107+
(requestText !== "" && tIn !== null) ||
108+
(responseText !== "" && tOut !== null);
109+
102110
return {
103111
bytes_in,
104112
bytes_out,
105113
tokens_in: tIn ?? 0,
106114
tokens_out: tOut ?? 0,
107-
tokenize_ms: tIn === null && tOut === null ? 0 : tokenize_ms,
115+
tokenize_ms: tokenizerRan ? tokenize_ms : 0,
108116
};
109117
}

0 commit comments

Comments
 (0)