# ★ Streaming AI replies from Laravel to React

DevFeed: [★ Streaming AI replies from Laravel to React](<https://devfeed.tech/articles/streaming-ai-replies-from-laravel-to-react-65084.md>)

Original publisher: [Read original article](<https://freek.dev/3092-streaming-ai-replies-from-laravel-to-react>)

Author: Freek Van der Herten (freek@spatie.be)

Published: 2026-10-05T10:30:47Z

Content type: tutorial

Language: en

Sources: [Freek](<https://devfeed.tech/sources/freek-dev-all-blogposts.md>)

Topics: [Laravel](<https://devfeed.tech/topics/laravel.md>), [React](<https://devfeed.tech/topics/react.md>), [json lines format](<https://devfeed.tech/topics/json-lines-format.md>), [vercel ai sdk](<https://devfeed.tech/topics/vercel-ai-sdk.md>), [Event-Streaming](<https://devfeed.tech/topics/event-streaming.md>), [Real-time AI interactions](<https://devfeed.tech/topics/real-time-ai-interactions.md>)

Tags: [agent](<https://devfeed.tech/tags/agent.md>), [ai](<https://devfeed.tech/tags/ai.md>), [ai-sdk](<https://devfeed.tech/tags/ai-sdk.md>), [buffer](<https://devfeed.tech/tags/buffer.md>), [cache](<https://devfeed.tech/tags/cache.md>), [helpdesk](<https://devfeed.tech/tags/helpdesk.md>), [json](<https://devfeed.tech/tags/json.md>), [laravel](<https://devfeed.tech/tags/laravel.md>), [llm](<https://devfeed.tech/tags/llm.md>), [react](<https://devfeed.tech/tags/react.md>), [real-time](<https://devfeed.tech/tags/real-time.md>), [replies](<https://devfeed.tech/tags/replies.md>), [sdk](<https://devfeed.tech/tags/sdk.md>), [stream](<https://devfeed.tech/tags/stream.md>), [streaming](<https://devfeed.tech/tags/streaming.md>), [string](<https://devfeed.tech/tags/string.md>)

## AI overview

A tutorial shows how a Laravel service streams AI chat replies and tool-call events to a React frontend using newline-delimited JSON. It explains buffering partial lines in the browser, updating the chat interface as events arrive, and saving the completed assistant message.

## Source excerpt

A couple of posts back I walked through the agent and tools we built on top of the Laravel AI SDK for There There, the helpdesk we're putting together at Spatie. I ended that post on a small cliffhanger. We hand the agent off to a streaming service that turns the SDK's response stream into events the frontend can consume. That's this post. A quick reminder on our angle. After two decades of running our own customer support, we wanted a helpdesk where AI makes support agents faster, not one that tries to replace them. The human reads, thinks, and directs. The model drafts and retrieves in real time while they watch. There There is in private beta right now, and you can apply for early access at there-there.app. Streaming from Laravel to React The obvious way to hand an LLM response to the browser is to wait for it to finish, then render the whole thing. That's easy, but it makes the agent feel sluggish. What we actually want is for the reply to appear word by word, and for the agent's own reasoning (like which tool it's calling) to be narrated along the way. The Laravel AI SDK exposes each step of a conversation as a typed event. We iterate over that stream and emit newline-delimited JSON for the frontend, one line per event. Here's the core of our AgentStreamingService. public function stream(Agent&HasTools $agent, string $message, AgentChat $chat): StreamedResponse { return response()->stream(function () use ($agent, $message, $chat): Generator { yield from $this->streamAgent($agent, $message, $chat); }, 200, [ 'Content-Type' => 'application/x-ndjson', 'Cache-Control' => 'no-cache', ]); } private function streamAgent(Agent&HasTools $agent, string $message, AgentChat $chat): Generator { set_time_limit(120); $fullContent = ''; $toolCalls = []; foreach ($agent->stream($message) as $event) { if ($event instanceof ToolCall) { yield json_encode(['type' => 'tool_call', 'tool_name' => $event->toolCall->name])."\n"; } if ($event instanceof TextDelta) { $fullContent .= $even