Streaming
Show users chunks of response text and UI as they arrive rather than blindly waiting for the full response.

To stream the AI response you should use the stream() method on the agent, instead of chat(). This method prepares the agent workflow to use the StreamingNode instead of ChatNode.
Calling the events() method on the returning agent handler you get a PHP generator that can be used to consume the streamiong as an iterable object.
Streaming chunks
When you process the streamed response of the agent you can expect to receive three types of chunk objects:
TextChunk: represents a piece of textReasoningChunk: contains chunks of the reasoning summary of the model (only available for reasoning models)ToolCallChunk: represents the LLM asking for a tool executionToolResultChunk: contains the results of tool execution
These objects are a layer of abstraction between the underlying messages flow inside the agent to perform a task and the data needed on the client side to stay informed on what's going on behind the scenes.
The stream composition depends by your agent implementation. If the agent has no tools attached there is no chance to receive a ToolCallChunk or ToolResultChunk instance, so you can iterate the output stream expecting only text and reasoning chunks.
Streaming & Tools
Neuron support Tools & Function calls in combination with the streaming response. You are free to provide your Agents with Tools and they will be automatically handled in the middle of the stream, to continue toward the final response.
When the agent receive a tool call request from the LLM, it will stream two types of chunk: ToolCallChunk, ToolResultChunk.
These classes contain the instance of the tool behind called by the LLM so you can expose informative output to the client about what the agent is doind to answer the user prompt.
Here is an example of how you can deal with this scenario:
Get The Final Result
When the model finishes streaming output you can retrieve the final AssistantMessage instance with the getMessage() method on the workflow handler:
Monitoring & Debugging
Many of the applications you build with Neuron will contain multiple steps with multiple invocations of LLM calls. As these applications get more and more complex, it becomes crucial to be able to inspect what exactly is going on inside your agentic system. The best way to do this is with Inspector
Stream Adapters
Neuron's Stream Adapter system provides a flexible, protocol-agnostic way to help you easily integrate Neuron powered agents with your frontend stack.
Stream adapters translate Neuron's internal streaming events (text chunks, tool calls, reasoning steps) into specific frontend protocols events like AG-UI.
This architecture allows you to seamlessly integrate Neuron agents with various frontend frameworks without modifying your core agent logic. Adapters handle protocol-specific concerns such as message lifecycle events, event formatting, and ID tracking, while maintaining consistent streaming behavior across all providers (Anthropic, OpenAI, Gemini, Ollama, etc.). The system is highly extensible, you can create custom adapters by extending SSEAdapter to implement streaming data transofrmations, or directly implement the StreamAdapterInterface for custom needs.

You simply need to provide an adapter instance to the stream() method of the agent used to stream the LLM response.
Vercel AI SDK Adapter
Adapter for Vercel AI SDK Data Stream Protocol: https://ai-sdk.dev/docs/ai-sdk-ui/stream-protocol
AG-UI Adapter
Implements the streaming event-based protocol defined by AG-UI protocol for real-time agent-frontend interaction. Supports text messages, tool calls, reasoning, and lifecycle events.
For more information, visit: https://docs.ag-ui.com/concepts/events
Connecting an AG-UI frontend
An AG-UI client (like CopilotKit) does not just open a connection. It sends a POST request with a JSON body called RunAgentInput, containing the conversation and the identifiers of the current run:
Your endpoint should read this payload, map the messages to Neuron message objects, and pass threadId and runId to the adapter constructor. The adapter echoes them back in the RUN_STARTED and RUN_FINISHED events, so the client can correlate the stream with the run it requested. If you omit them, the adapter generates its own identifiers (useful for testing, but a real AG-UI frontend expects its own IDs back).
The adapter also provides the HTTP headers required by the SSE transport via the getHeaders() method. Remember to send them and to flush the output after each line, otherwise the stream can get stuck in PHP output buffers or proxies.
Here is a complete endpoint example:
The adapter translates Neuron streaming chunks into the following AG-UI events:
The run starts
RUN_STARTED
Text chunks
TEXT_MESSAGE_START, TEXT_MESSAGE_CONTENT, TEXT_MESSAGE_END
Reasoning chunks
REASONING_START, REASONING_MESSAGE_START, REASONING_MESSAGE_CONTENT, REASONING_MESSAGE_END, REASONING_END
A server-side tool runs
TOOL_CALL_START, TOOL_CALL_ARGS, TOOL_CALL_END, TOOL_CALL_RESULT
The model calls a frontend tool (from RunAgentInput.tools)
TOOL_CALL_START, TOOL_CALL_ARGS, TOOL_CALL_END, then RUN_FINISHED so the client can run the tool
StepStartedStreamEvent, StepFinishedStreamEvent
STEP_STARTED, STEP_FINISHED
ActivityStreamEvent
ACTIVITY_SNAPSHOT
CustomStreamEvent
CUSTOM
A tool call needs approval
STATE_SNAPSHOT, MESSAGES_SNAPSHOT, then RUN_FINISHED with an interrupt outcome carrying one confirmation interrupt per tool call
The run waits for another external input
STATE_SNAPSHOT, MESSAGES_SNAPSHOT, then RUN_FINISHED with an interrupt outcome whose reason is neuron:<type>
The run completes
RUN_FINISHED
The run fails
RUN_ERROR
Tool calls are published when Neuron dispatches them, not while the model is still writing the arguments. STATE_SNAPSHOT and MESSAGES_SNAPSHOT are sent only when a run pauses for input, and the adapter does not emit STATE_DELTA.
Tools attached to a Neuron agent are executed on the server. The client is informed of the ongoing execution through the TOOL_CALL_* events and receives the tool output in the TOOL_CALL_RESULT event, followed by the agent's final text message.
Custom Events
Both adapters understand portable step, activity, and custom events. AG-UI uses its native STEP_STARTED, STEP_FINISHED, ACTIVITY_SNAPSHOT, and CUSTOM events. Vercel emits transient data-* parts, so intermediate information is available to the UI without being added to assistant-message history.
Map an application event by its exact class when domain code should remain independent from Neuron's portable event objects:
The callback returns a portable event, never SSE, JSON, or a protocol-specific array. Return null to suppress the explicitly mapped event. Mappings are exact class matches, so a parent-class mapping does not silently capture subclasses.
Custom Adapters
The stream() method of the agent handler accept an instance of StreamAdapterInterface. So you are free to implement this interface with custom implementation, and pass it to the handler. Here is how the interface looks like:
You can always get inspiration by the built-in implementations.
Streaming Channels
As agents become more interactive and capable, application developers are increasingly forced to run them outside the HTTP request lifecycle because of its timeout limits, which leaves the streamed output with no way to reach the UI. Channels deliver the streamed output of Agents and Workflows to the user interface through external broadcast systems such as Pusher, websockets, a Redis queue, or whatever your application already uses.
The problem it solves
There are two ways to run an Agent. You can consume its real-time events:
or you can just ask for the final result:
The first style works well when your application code holds the stream from start to finish, like a controller that keeps the HTTP connection open and prints every chunk to the browser.
The problem appears when nobody is holding the stream. Think about an Agent running in a background job. The job calls events(), the agent produces chunks, but there is no browser attached to that process. Without a Channel, all that output is simply thrown away.
Channels automatically forwards the streamed events to a custom transport: a websocket, a Redis queue, an SSE response, anything you want, in order to stream real-time results to your web app from a background process.
Remeber that a streaming adapter is required to use channels. If you want to forward the Neuron native chunks to the channel you can use the built-in NeuronAI\Agent\Adapter\AgentChunkAdapter.
Attaching a channel
You attach a Channel implementing the channel() method in the Agent class, or using setChannel() directly on the Agent instance.
CallbackChannel
The fastest way to get started is CallbackChannel. It wraps up to four closures, one for each method of the interface. All of them are optional. Here is a complete example that publishes every streamed item to Redis, so a websocket server can forward it to the browser:
Now it does not matter if stream() is called by a controller, a queue worker, or a cron job. The chunks always reach Redis, and from there your frontend.
You can also react to the terminal calls. This example notifies the frontend when the run pauses for approval, completes, or fails:
Pusher
You can stream the Agent output to the frontend via Pusher, or Pusher compatible servers.
maxRequestBytes allows you to adjust the component compatibility with Pusher compatible servers. Its default value of 10 KB is good for Pusher but other servers could support a different request size.
batchSize instead allows you to define how many events must be collected before sending them in a single batch request. Higher values make the backend execution smoothly but can result in a scattered frontend experience. Lower values makes the frontend experience smoothly running more sending requests on the backend side.
Partial Events
Pusher caps an event at 10 KB; Reverb defaults to the same, Soketi to 100 KB, and maxRequestBytes (default 10_000) tunes the ceiling.
To support streaming when your real-time server has such a limit, an event that does not fit the max size - typically a tool call, tool result, or a message snapshot - is split into consecutive fragments. Furthermore servers do not guarantee that fragments arrive in order and never interleave. So the browser must be able to reconcile this fragmentation and ordering in order to deliver consistent streaming to your UI components.
Neuron ships with a first-party Typescript module that brings this capability into your frontend.
Install the module with:
Check out the dedicated documentation to integrate it in your frontend: https://www.npmjs.com/package/@neuron-core/streaming
Mercure
You can stream the agent output to a Mercure Hub using SSE:
By default maxRequestsPerSecond is set to null, because self hosted instances doesn't have any rate-limit. If you use a managed cloud instance, they impose strict rate limits from one up to twenty requests per second based on your subscription plan.
Once you configure the MercureChannel with the same restriction you have on the Mercure server the component automatically limits the pace of which it sends events, and eventually it split the event in multiple fragments if the size is over maxRequestBytes . The browser must be able to reconcile this fragmentation and ordering in order to deliver consistent streaming to your UI components.
Neuron ships with a first-party Typescript module that brings this capability into your frontend.
Install the module with:
Check out the dedicated documentation to integrate it in your frontend: https://www.npmjs.com/package/@neuron-core/streaming
Redis
RedisChannel publishes the segment on a Redis Pub/Sub channel, the usual fan-out between a worker running the agent and the process holding the client's connection (an SSE endpoint, a websocket server). It needs ext-redis and a connected Redis client.
Redis consumer
With channels your agent sends streamed chunks to the specified redis queue. So now your app need a way to listen for these chunks and forward the stream to the frontend.
RedisChannelReader reads what RedisChannel publishes. An agent that started before it subscribed, raises ChannelReadException, as does silence past its timeout. Redis delivers one publisher's messages in order and RedisChannel never fragments, so it needs no reordering or reassembly.
Channel errors never break the run
A Channel talks to external systems, and external systems fail. A Redis server can be down, a socket can be closed. The framework protects the run from this: every call to the Channel is guarded. If your Channel throws an exception, the workflow catches it, reports it as a ChannelError event through the observability system, and continues the run as if nothing happened.
This means the AI work is never lost because a delivery transport had a problem. The chat history remains the source of truth. The live stream is only a convenience on top of it, and a client that missed some chunks can always reload the final result from history.
If your transport needs a real policy for repeated failures, for example stop trying after ten errors, put that logic inside your Channel implementation, because only the Channel knows what a failure means for its own transport.
Custom Channels
A Channel is any class that implements ChannelInterface. It has four methods. One receives the streamed items, and three tell you how the run ended:
Every run ends with exactly one of the three terminal calls: suspended(), completed(), or failed(). This is important for user interfaces. If a run fails and you have no error signal, the user is left with a spinner that never stops. With failed() your frontend always receives a clear end signal and can recover, for example by reloading the chat history.
Two details are good to know. The failed() method is a notification only: the exception still reaches the code that called chat().
Here is an example of a LogChannel:
And attach it like any other channel:
Last updated

