Skip to content

Streaming

Stream text and reasoning events from Workers AI, and read usage and neurons from a stream.

Updated

Call stream() on an agent and loop over the events:

use Laravel\Ai\Streaming\Events\TextDelta;

foreach (agent('You are a helpful assistant.')->stream('Tell me a story.', provider: 'workers-ai') as $event) {
    if ($event instanceof TextDelta) {
        echo $event->delta;
    }
}

Reasoning events

A reasoning model sends ReasoningStart, ReasoningDelta and ReasoningEnd events before the text events. All three classes are in Laravel\Ai\Streaming\Events.

Different models send reasoning text under different keys. The package reads reasoning_content, reasoning and thinking, so you get the same events either way.

Usage on streams

The package asks Cloudflare to include usage data in the stream and adds it up across tool-call steps. Cloudflare sometimes sends a final usage chunk of all zeros; the package ignores it and keeps the real figures.

Stream and tools

Streaming works with tool calls. A tool call arrives in pieces across several chunks, and the package joins them before it hands the complete call to Laravel AI. See Tools.

Stream errors

On laravel/ai 0.11 and later, a stream that ends on an error event throws StreamErrorException. On 0.9 and 0.10, the stream just ends with no exception, so if you support those versions, check for an error event in your loop.

Neuron events on streams

WorkersAiUsageReported fires for streamed calls too, once per model step after the last chunk. See Cost tracking. The StreamEnd event also carries the total token usage. On laravel/ai 1.x, StreamEnd::$usage is a TextUsage with inputTokens. On 0.9 to 0.11, it is a Usage with promptTokens.

Type to search…

↑↓ navigate↵ selectEsc close