When you build a streaming AI endpoint in Next.js App Router, handling the happy path is easy. Handling what happens when a user clicks Stop , closes their mobile browser tab, or your upstream LLM provider hangs for 45 seconds is where production bugs and wasted token bills creep in.
If your route handler doesn't propagate abort signals and separate first-token timeouts from total stream duration, your server keeps paying for tokens nobody will ever read—or leaves the UI stuck on an infinite loading spinner.
Here is a clean, dependency-free pattern using standard Web AbortSignal APIs.
1. Combine the Client Disconnect Signal With a Hard Timeout
Next.js Route Handlers pass a standard Request whose req.signal aborts if the client disconnects.…