Streaming AI output does not require websockets, a broadcast server or a queue worker.
The chain is:
- A Route Handler calls
streamTextwith the assembled system prompt. - The AI SDK returns a streaming response the browser consumes with
useChat. - When the stream finishes,
onFinishwrites the assistant message and bills the account from the token usage the provider actually reported.
Because billing happens in onFinish, the user is never charged for a reply that failed
halfway — and never charged an estimate.
The gotchas
- Buffering. Any proxy that buffers the response defeats the point. Disable it for the chat route specifically.
- Timeouts. Serverless platforms cap execution time; long generations need a raised limit.