Start with the failure condition
A cache comparison is misleading when the two requests produce different amounts of output. A faster request may simply have had less text to generate. The supplied checks therefore reject a comparison unless both measurements report the same output-token count. This is the first review gate: do not calculate a cache “saving” from mismatched workloads.
Prerequisites
You need a streamed Responses-style event sequence, an application clock, and completed-response usage data. Keep the measurement code at the stream boundary so it can observe text events and final token accounting together.
Implementation decision
Treat the first nonempty text delta as TTFT. Do not stop the timer on the first event, because streams may emit empty text deltas before anything a user can read. Continue consuming events through completion, then record end-to-end duration plus input, cached-input, and output-token fields.
For valid paired measurements, calculate two separate observations: the TTFT difference and the duration after first visible text. This avoids turning one number into an unsupported explanation. If cached tokens rise while TTFT falls and post-first-text time is unchanged, the measurements are consistent with cheaper prefix processing rather than faster generation.
Expected result
The instrumentation classifies the visible wait. Late first text points investigation toward pre-generation work such as prompt size, unstable prefixes, retrieval, or preparation. Early first text with a long completion tail points toward output length or generation behavior.
Limitation
An injected recording stream verifies the measurement contract, not an external engine’s cache routing, cache lifetime, network path, or rendering performance. Treat usage fields and timings as application-observed evidence, then validate conclusions with comparable production request groups rather than claiming causation from one fixture.
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