A single latency number hides the decision you need to make: is the user waiting for prompt processing, or for the model to finish generating?
The implementation measures three related signals from a streamed Responses-style sequence. Start timing before consuming the stream. Set TTFT only when the first nonempty response.output_text.delta arrives; empty deltas are not visible output. When completion arrives, retain usage data alongside the timing result: input tokens, cached input tokens, and output tokens. End-to-end time remains the interval until the stream finishes.
That produces a useful split. TTFT represents the wait before readable text. The remainder of end-to-end time after TTFT represents the observed post-first-text portion. For a cache comparison, hold the task and output workload constant. The companion comparison step requires equal output-token counts and reports TTFT savings separately from post-first-text time. Rejecting unequal outputs is important: a shorter completion can reduce total time without saying anything about prefix reuse.
Read cached_tokens as supporting evidence. Cached input together with a lower TTFT is consistent with cheaper prefix processing. Cached input does not imply faster token generation after text has begun, so do not credit the entire response-time difference to caching.
There is an intentional limit to this approach. Application instrumentation can show when visible text arrived and what usage fields the completed response reported. It cannot prove provider-side cache routing or lifetime, nor can an injected event fixture reproduce browser rendering, retrieval, tool preparation, or network behavior. Use the measurements to narrow the investigation: high TTFT with no cached input suggests prompt or prefix work; low TTFT with a long tail suggests output size or generation work.
Working example
/**
* Measures a streamed Responses-style event sequence with an injected clock.
* This measures application-observed timings; it does not prove a provider's
* cache routing or cache lifetime behavior.
*/
export async function measureStreamingResponse({ stream, now }) {
if (!stream || typeof stream[Symbol.asyncIterator] !== "function") {
throw new TypeError("stream must be an async iterable");
}
if (typeof now !== "function") {
throw new TypeError("now must be a function");
}
const startedAt = now();
let firstTextAt = null;
let completedUsage = null;
for await (const event of stream) {
if (
event?.type === "response.output_text.delta" &&
typeof event.delta === "string" &&
event.delta.length > 0 &&
firstTextAt === null
) {
firstTextAt = now();
}
if (event?.type === "response.completed") {
completedUsage = event.response?.usage ?? null;
}
}
const finishedAt = now();
const details = completedUsage?.input_tokens_details ?? {};
return {
ttftMs: firstTextAt === null ? null : firstTextAt - startedAt,
endToEndMs: finishedAt - startedAt,
inputTokens: completedUsage?.input_tokens ?? null,
cachedTokens: details.cached_tokens ?? 0,
outputTokens: completedUsage?.output_tokens ?? null,
};
}
/**
* Compares two measurements for the same requested output budget/workload.
* A positive ttftSavedMs is an observed difference, not proof that cache reuse
* caused it. Real cache behavior must be verified from provider usage fields.
*/
export function compareIdenticalWorkloads({ withoutCache, withCache }) {
for (const [name, measurement] of Object.entries({ withoutCache, withCache })) {
if (
!measurement ||
!Number.isFinite(measurement.ttftMs) ||
!Number.isFinite(measurement.endToEndMs) ||
!Number.isFinite(measurement.outputTokens)
) {
throw new TypeError(`${name} must contain finite TTFT, end-to-end, and output token measurements`);
}
}
if (withoutCache.outputTokens !== withCache.outputTokens) {
throw new RangeError("identical-workload comparison requires equal outputTokens");
}
return {
outputTokens: withCache.outputTokens,
cachedTokensWithoutCache: withoutCache.cachedTokens ?? 0,
cachedTokensWithCache: withCache.cachedTokens ?? 0,
ttftSavedMs: withoutCache.ttftMs - withCache.ttftMs,
generationMsWithoutCache: withoutCache.endToEndMs - withoutCache.ttftMs,
generationMsWithCache: withCache.endToEndMs - withCache.ttftMs,
};
}
Read the full article: https://chriseugenerodriguez.com/blog/measure-ttft-and-prompt-cache-performance
Top comments (2)
The split is the part worth stealing, Chris — one end-to-end number really does hide whether the user is waiting on prompt processing or on generation, and setting TTFT only on the first nonempty delta is the detail that makes the measurement honest. I also liked that you kept cached_tokens as supporting evidence rather than proof, since a lower TTFT with a high cache hit could still be a short prompt doing the work. The limit you name at the end is the one I keep thinking about: if the instrumentation can't tell network time from model time, what do you trust first when TTFT jumps on one request but cached_tokens looks normal — do you reach for the provider's server-timing headers, or do you re-run the same prompt and compare?
One more useful split is time to response headers versus headers to first nonempty text. A spike in the first interval can point toward connection or queueing delays; a spike in the second leaves prompt processing and stream delivery in the mix. Neither interval alone identifies the provider-side cause.