Stream a Brain response
Status: Ready
Article ID: 022
Truth label: Current
Audience: API integrator
Reader outcome: Consume the Brain server-sent event stream while treating the final done event as the authoritative response and metadata record.
Related articles: 017, 021, 030
Target week: 09
Last verified: 2026-09-13
Technical reviewer: Codex source audit
Editorial reviewer: Codex editorial audit
Use the streaming route for progress
POST /chat/stream accepts the same chat choices as the non-streaming route,
then returns text/event-stream. Each event arrives as a JSON object on an SSE
data: line. Streaming improves perceived responsiveness and can expose tool
progress, but it does not change the retrieval, provider, output-lint, or trace
boundaries of the request.
The client should parse event frames, then switch on each object's type.
Do not parse visual text from the interface or assume that every frame contains
answer text.
Handle each event type
The current stream can emit:
contextwhen Brain added retrieved context, including a character counttoolfor tool-loop activitytokenfor incremental response textdonefor the completed, processed response and its metadataerrorwhen the generator catches a failure after streaming has started
Treat unknown event types as forward-compatible information. Log their type and ignore them until the client supports them. This is safer than failing a long response because the server gained a new progress event.
Make done authoritative
Token events are provisional display material. The final done event carries
the response after output linting and any allowed local revision pass. It also
includes provider, model, reasoning lens, voice register, conversation id,
turn id, generation settings, usage when available, and the retrieval trace.
When done arrives, replace the accumulated display text with its response
field. This avoids retaining text that the final processing step corrected.
Persist ids and metadata from done, not from local guesses.
Separate HTTP errors from stream errors
Some validation and authorization failures happen before the response starts.
Those arrive as ordinary HTTP errors. Failures inside the generator arrive as
an SSE error event because the HTTP stream is already open.
A client therefore needs two error paths:
- inspect the HTTP status before reading frames
- handle an
errorevent while reading a successful stream response
If the stream closes without done, mark the turn incomplete. Do not present
the last token buffer as a verified final answer.
Build a small client checklist
For one synthetic request:
- send a valid UUID conversation id or let Brain create one
- confirm the response content type is
text/event-stream - display token events as provisional text
- record context and tool events separately
- replace the text when
donearrives - save the returned turn id
- retrieve that turn through the trace route
This checks rendering, finalization, and lineage without requiring private prompt content.
Current limits
SSE transport does not make a provider call resumable or idempotent. A dropped
connection can leave the caller without a done event. Context, tool details,
and final text can contain sensitive material. Stream only across the trusted
deployment boundary and do not log raw frames into public analytics.
Next lesson
Continue with Separate chat models from embedding models.
Derivative map
- Weekly package: WEEK-09
- Long video: VIDEO-W09-LONG
- Shorts: SHORT-W09-PROBLEM, SHORT-W09-MECHANISM, SHORT-W09-WARNING
- Email: EMAIL-W09
- LinkedIn: LI-W09-A, LI-W09-B
- X: X-W09-A, X-W09-B
- Visual: VISUAL-W09
Review record
- Event types, final response behavior, ids, and error paths checked against current API and chat source.
- Incomplete-stream and sensitive-frame limits reviewed.
- Client checklist uses no live token or private prompt.
- Internal links and repository voice reviewed.
Review completed: 2026-08-27
Re-audited against the running product: 2026-09-13