Next.js AI streams keep billing after users leave, unless the upstream request is aborted
Developers building AI chat endpoints in Next.js often find token costs exceed what users actually read. The cause is that when a browser disconnects mid-response, the upstream model request continues generating, incurring charges for output nobody sees. The article recommends building the SSE stream with a ReadableStream and calling the upstream abort in its cancel() callback, which fires when the client connection drops. It also advises setting Cache-Control with no-transform so proxies don't buffer the stream, raising the route's max duration for long generations, and metering input, output, and cache tokens per request. The author promotes a starter kit that packages these patterns.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in