Streaming allows your application to display AI responses as they’re being generated, creating a more responsive and engaging user experience.
Basic Streaming
The simplest way to use streaming is with therespondStreamed method:
Streaming with Callback
You can also provide a callback function to process each chunk:Understanding Stream Chunks
The stream can yield three types of chunks:StreamedAssistantMessage
Regular text content chunks from the AI assistant
ToolCallMessage
Tool call messages (handled internally by LarAgent)
Array
Final chunk when structured output is enabled
StreamedAssistantMessage chunks as shown in the examples above. Tool calls are processed automatically by LarAgent.
Laravel HTTP Streaming
For Laravel applications, LarAgent provides thestreamResponse method that returns a Laravel StreamedResponse, making it easy to integrate with your controllers:
streamResponse method supports three formats:
- Plain Text
- JSON
- Server-Sent Events (SSE)
- Plain Text
- JSON
- Server-Sent Events
Streaming with Structured Output
When using structured output with streaming, you’ll receive text chunks during generation, and the final structured data at the end:Best Practices
Do use streaming for long responses to improve user experience
Do handle both text chunks and structured output appropriately
Do implement proper error handling in your streaming code

