I cut our LLM streaming latency by 74%. Here's the exact setup.
Every AI product demo dies the same death: the user hits send and stares at a spinner for four seconds. We shipped a fix in one afternoon — time-to-first-byte went from 3.8s to 0.9s, and completion-abandon rate dropped by a third. The trick isn't a faster model. It's moving the stream boundary to the edge and flushing tokens the moment they exist. Below is the full setup — server, client, and the two failure modes that will bite you.
The edge streaming pattern
Instead of buffering the completion in a serverless function, terminate the SSE connection at an edge worker and pipe the upstream body straight through. The worker owns retries; the client owns rendering.
export default {
async fetch(req, env) {
const upstream = await env.AI.stream(req);
return new Response(upstream.body, {
headers: { "content-type": "text/event-stream" },
});
},
};
Benchmarks
Same model, same prompts, 500 runs each:
| metric | before → after | delta |
|---|---|---|
| TTFB | 3.8s → 0.9s | −74% |
| abandon rate | 22% → 14% | −36% |
| p95 total | unchanged | perception is the win |
Tomorrow’s exercise: wire this into the course starter repo and add abort handling. Takes about twenty minutes, and it’s the foundation for the retry work in Module 03.
Authenticate via Clerk to access daily exercises and keep vibing.