Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(debug): track time to first token and first content in streaming responses

Steven Enamakel652c4b109a03
diff --git a/scripts/__tests__/capture-first-inference.test.mjs b/scripts/__tests__/capture-first-inference.test.mjs
index 57050c43dd..b2514e8457 100644
--- a/scripts/__tests__/capture-first-inference.test.mjs
+++ b/scripts/__tests__/capture-first-inference.test.mjs
@@ -221,8 +221,13 @@ test('capture proxy forwards an inference call, dumps the body, and summarises t
assert.equal(record.cached_tokens, 12288);
assert.equal(record.error, null);
assert.ok(record.ttfb_ms >= 0 && record.total_ms >= record.ttfb_ms, JSON.stringify(record));
+ assert.equal(record.stream, true);
+ // The stub streams visible content straight away, so the first token is the
+ // first content, and both land no earlier than the first byte.
+ assert.ok(record.first_token_ms >= record.ttfb_ms, JSON.stringify(record));
+ assert.equal(record.first_content_ms, record.first_token_ms);
const summaryLine =
- /\[capture\] #000 200 model=z-ai\/glm-5\.3-flash msgs=2 tools=1 served_by=StreamLake ttfb=\d+\.\d\ds total=\d+\.\d\ds prompt=12344 cached=12288 cache_key=tap-25675927a3f2160d/;
+ /\[capture\] #000 200 model=z-ai\/glm-5\.3-flash msgs=2 tools=1 served_by=StreamLake ttfb=\d+\.\d\ds total=\d+\.\d\ds prompt=12344 cached=12288 cache_key=tap-25675927a3f2160d stream=yes ttft=\d+\.\d\ds ttfc=\d+\.\d\ds thread=thread-1/;
assert.match(await waitForOutput(proxy.output, summaryLine), summaryLine);
});
diff --git a/scripts/debug/capture-first-inference.mjs b/scripts/debug/capture-first-inference.mjs
index 2079c8be78..9e2ee5b31c 100644
--- a/scripts/debug/capture-first-inference.mjs
+++ b/scripts/debug/capture-first-inference.mjs
@@ -24,7 +24,9 @@ const USAGE = `Usage: node scripts/debug/capture-first-inference.mjs [--help]
Loopback proxy between the OpenHuman core and its inference backend. Records the
exact request bodies the harness sends and prints one summary line per inference
-response: serving endpoint, time to first byte, prompt / cached tokens, status.
+response: serving endpoint, time to first byte, time to the first model token
+(ttft, reasoning or text) and first visible text (ttfc), prompt / cached tokens,
+stream flag, thread id, status.
Configure with environment variables:
CAPTURE_PORT listen port (default 18765)
@@ -184,6 +186,35 @@ function summarizeResponseBody(text) {
return out;
}
+// A streamed delta that carries model output: reasoning (OpenRouter's
+// `reasoning`, DeepSeek's `reasoning_content`) or visible `content`.
+const FIRST_TOKEN_RE = /"(?:content|reasoning|reasoning_content)":"[^"]/;
+const FIRST_CONTENT_RE = /"content":"[^"]/;
+
+/**
+ * Tracks when a streamed response produced its first model token (reasoning
+ * or content) and its first visible content, across arbitrary chunk
+ * boundaries: each chunk is scanned together with the tail of the previous one.
+ */
+function createFirstTokenTracker(startedAt) {
+ let tail = '';
+ const out = { first_token_ms: null, first_content_ms: null };
+ return {
+ observe(chunk, now = Date.now()) {
+ if (out.first_token_ms !== null && out.first_content_ms !== null) return;
+ const window = tail + chunk.toString('utf8');
+ tail = window.slice(-256);
+ if (out.first_token_ms === null && FIRST_TOKEN_RE.test(window)) {
+ out.first_token_ms = now - startedAt;
+ }
+ if (out.first_content_ms === null && FIRST_CONTENT_RE.test(window)) {
+ out.first_content_ms = now - startedAt;
+ }
+ },
+ result: () => ({ ...out }),
+ };
+}
+
function formatSummaryLine(record) {
const ms = value => (value == null ? '-' : `${(value / 1000).toFixed(2)}s`);
return (
@@ -191,7 +222,9 @@ function formatSummaryLine(record) {
`model=${record.model ?? '?'} msgs=${record.messages ?? '?'} tools=${record.tools} ` +
`served_by=${record.provider ?? '?'} ttfb=${ms(record.ttfb_ms)} total=${ms(record.total_ms)} ` +
`prompt=${record.prompt_tokens ?? '?'} cached=${record.cached_tokens ?? '?'} ` +
- `cache_key=${record.prompt_cache_key ?? '-'}` +
+ `cache_key=${record.prompt_cache_key ?? '-'} ` +
+ `stream=${record.stream ? 'yes' : 'no'} ttft=${ms(record.first_token_ms)} ` +
+ `ttfc=${ms(record.first_content_ms)} thread=${record.thread_id ?? '-'}` +
(record.error ? ` error=${JSON.stringify(record.error)}` : '')
);
}
@@ -256,9 +289,11 @@ const server = http.createServer((req, res) => {
// copy into the summary. Inference bodies are small (a few hundred KB
// at most), so buffering the copy is fine.
let firstByteAt = null;
+ const firstTokens = createFirstTokenTracker(startedAt);
const pieces = [];
upstreamRes.on('data', chunk => {
if (firstByteAt === null) firstByteAt = Date.now();
+ firstTokens.observe(chunk);
pieces.push(chunk);
res.write(chunk);
});
@@ -275,6 +310,7 @@ const server = http.createServer((req, res) => {
...summarizeRequestBody(body),
...summarizeResponseBody(text),
ttfb_ms: firstByteAt === null ? null : firstByteAt - startedAt,
+ ...firstTokens.result(),
total_ms: Date.now() - startedAt,
};
if (captureResponses || (captureAll && (status < 200 || status >= 300))) {