SSE for API Routes
Use this page when you expose a chat endpoint and want live token/chunk streaming to the UI.
Recommended server pattern (toSSE)
Good to know:
toSSE(...)is the route-level helper you usually want. It sets correct SSE headers and writes chunks in SSE format.
Client consumption
For the full list of chunk events and meanings, see Stream Protocol.
If you need manual fetch, parse SSE with readStream(...):
Infrastructure notes
toSSE(...)/createStreamResponse(...)set:content-type: text/event-streamcache-control: no-cacheconnection: keep-alivex-accel-buffering: no
- Keep this route on a runtime that supports streaming responses.
- If you run behind a proxy/CDN, make sure response buffering is disabled.
Good to know: If your client needs a single buffered result instead of live chunks, expose a non-stream mode and return
collectBufferedStream(...)from your route.
Low-level helper
Use createStreamResponse(...) only when you already have your own AsyncIterable<StreamChunk> and want to convert it to SSE directly.
For chunk types and protocol details, see Stream Protocol.