Skip to main content

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:

  • context when Brain added retrieved context, including a character count
  • tool for tool-loop activity
  • token for incremental response text
  • done for the completed, processed response and its metadata
  • error when 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:

  1. inspect the HTTP status before reading frames
  2. handle an error event 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:

  1. send a valid UUID conversation id or let Brain create one
  2. confirm the response content type is text/event-stream
  3. display token events as provisional text
  4. record context and tool events separately
  5. replace the text when done arrives
  6. save the returned turn id
  7. 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