Stream tokens to the UI with server-side processing of structured outputs, function calls and tool execution.
How a streaming pipeline handles tokens and tool calls together
1 / 4Open stream
Step 1 of 4
Open stream
The UI calls the API, which opens a streaming connection (server-sent events or a WebSocket) to the model. Heartbeats keep the connection alive.
Step 2 of 4
Parse chunks
An incremental parser reads each chunk as it arrives. Chunks can cut a tool call in half, so it buffers partial JSON instead of parsing whole documents.
Step 3 of 4
Tokens to UI
Plain text tokens are forwarded to the UI immediately, which is what keeps time to first token low. Backpressure handling stops a slow client growing server memory.
Step 4 of 4
Run tool
When the parser sees a complete tool call, it runs the tool server-side and feeds the observation back to the model, which continues the stream. The full response is logged on the server.
Text goes straight to the user; a tool call pauses the stream and then resumes it.
clientservicemodeldatabaseagentexternal
Explanation
The API opens a streaming connection to the LLM. A parser incrementally tokenises the stream, detecting tool call boundaries. Text tokens stream to the UI; tool calls trigger execution and feed observations back into the model.
Components
Streaming transport (SSE/WebSocket)Incremental JSON parserTool call detectorBackpressure handlerReconnection logic