Streaming a model's output through your own server sounds like plumbing: open an SSE response, loop over the provider's chunks, echo each one, flush. It works on the first try and then quietly costs you money in three places.
The first is a client that closes the tab. The provider keeps generating, you keep reading, and nobody is on the other end. The second is a buffer that grows because you saved every chunk "in case you need it later". The third is a usage number that describes what the provider said it produced rather than what the user received. All three are the same underlying mistake: treating the stream as a single operation when it is really three parties who can each leave early.
Here is how I think about each one.
Detecting a disconnect in PHP is a write, not an event
connection_aborted() does not flip the moment the browser goes away. PHP learns about the disconnect when it next tries to write and the write fails.…