Loading...
const res = await fetch(url, { method: 'POST', body });
const { text } = await res.json();
render(text);const res = await fetch(url, { method: 'POST', body: streamBody });
const reader = res.body.pipeThrough(new TextDecoderStream()).getReader();
while (true) {
const { value, done } = await reader.read();
if (done) break;
appendChunk(value);
}Streaming shows text as soon as the first tokens arrive, so users do not wait for the whole answer to finish.
It is the time from sending a request until the first token of the response arrives. It is what users feel most in chat interfaces.
Yes, longer prompts take more time to process before the first token. Prompt caching can reduce this for repeated prefixes.
No, total generation time stays about the same. Streaming improves perceived speed because users can start reading right away.