Free tools Windows power users keep installed
One-click scans. No signup required.
To stream an LLM response from PHP to a browser, have PHP call the model API with streaming enabled, read its incoming events incrementally, and send the useful content to the browser as a text/event-stream response. The browser can then append text as it arrives instead of waiting for the full answer. This is incremental delivery—not a promise that each network chunk contains exactly one model token or complete word.
How the PHP-to-browser stream works
- The browser requests your application. It connects to a PHP endpoint on your server.
- PHP calls the model API. The request enables streaming, so the API returns events or chunks as generation proceeds. OpenAI describes this as HTTP streaming over server-sent events, allowing an application to process the beginning of an answer before the full response is ready: OpenAI’s streaming guide.
- PHP reads the upstream stream. It parses each event or chunk and selects the content and status information your application needs.
- PHP sends an SSE response to the browser. It emits browser-facing event frames with the response content type set to
text/event-stream. - The browser renders arriving text. Client-side code handles each event and adds its text to the displayed answer.
PHP is a relay in this design, not a source of model output. Keep API credentials on the server; do not put provider secrets in browser code or send them to the client. The OpenAI API overview points developers to authentication, endpoint, streaming-event, error, and rate-limit guidance.
As an Amazon Associate I earn from qualifying purchases.
Choose how PHP reads the model stream
OpenAI’s Responses API and Chat Completions both support streaming, but their incremental data is organized differently. Choose the API surface your application uses and parse its documented event or chunk format rather than assuming every stream has the same shape.
Recommended Free Tools
| API surface | Incremental text | Progress and completion | Usage after interruption |
|---|---|---|---|
| Responses API | The typed response.output_text.delta event carries incremental text. |
Lifecycle events communicate progress and completion, failure, or an incomplete response. | The cited event reference describes lifecycle events; it does not establish a guarantee of final usage data after an interrupted stream. |
| Chat Completions | The stream arrives as incremental completion chunks. | Consume chunks as they arrive; do not treat a content chunk alone as proof that the response completed successfully. | If the stream is interrupted or cancelled, the final usage chunk may not arrive. |
For the Responses API, use the streaming event reference to identify text deltas and lifecycle events. For Chat Completions, consult the streaming chunk reference, especially if your application records usage.
#1 Best Overall
What “streaming tokens” means in practice
Although developers often say “stream tokens,” an application should think in terms of incremental response content. The API provides chunks or typed events, and their boundaries are transport and event boundaries—not a reliable mapping to individual model tokens, words, or sentences. A delta may contain only part of a word, several words, or another portion of generated text. Append content as it arrives; do not split or reconstruct model tokens based on the timing or size of network chunks.
Text can reach the browser before the response’s final status or usage information is known. Keep rendering separate from final accounting: treat the lifecycle or terminal event as the signal to finalize the UI, and handle failure or incomplete status distinctly from successful completion.
Rank #2
What your PHP endpoint needs to handle
The API documentation establishes upstream streaming behavior, but it does not prescribe a universal PHP implementation or deployment configuration. The exact code depends on your PHP runtime, framework, HTTP client, web server, and reverse proxy. Whatever stack you use, verify these behaviors end to end:
- Incremental reads: Confirm your HTTP client exposes upstream events as they arrive rather than collecting the entire response first.
- Browser-facing SSE: Send valid SSE frames and the
text/event-streamcontent type. Relay only the data your client needs, not raw provider credentials or unrelated upstream details. - Buffering: Check PHP output buffering and every web-server or proxy layer between PHP and the browser. A layer that buffers the response can make a nominally streamed endpoint appear to deliver everything at once.
- Timeouts and disconnects: Decide how the endpoint handles a slow upstream response, an upstream error, or a browser that disconnects. Avoid continuing work unnecessarily after the client is gone, where your stack allows you to detect that condition.
- Terminal states: Distinguish successful completion from failure or an incomplete response. Do not present partial text as a confirmed complete answer.
- Usage records: Store final usage only when the relevant completion data actually arrives. In Chat Completions, cancellation or interruption can prevent the final usage chunk from arriving.
These are deployment checks, not fixed PHP settings: the available evidence does not establish a safe, universal buffer or timeout value. Test the complete route through the same runtime, framework, server, and proxy that will handle production traffic.
Quick Recap
Rank #4
Common implementation mistakes
- Exposing the API key: Sending a model request directly from browser code reveals a server credential. Make the authenticated upstream request from PHP.
- Waiting for the full upstream body: Reading the response as one completed body defeats incremental delivery. Use an HTTP client and handling path that consume streamed data.
- Assuming chunk boundaries are tokens: Render the text content in order without relying on chunk size to indicate token, word, or sentence boundaries.
- Treating the last visible text as success: A stream can fail or end incomplete after sending some content. Use the API’s lifecycle signals to decide whether generation completed.
- Assuming usage is always present: An interrupted or cancelled Chat Completions stream may not deliver its final usage chunk.
- Testing only against PHP: Output buffering or proxy behavior can delay delivery after PHP has emitted data. Verify when bytes reach the browser through the real deployment path.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




