LESSON 128 / 215·2 min readIntermediate

Streaming AI chatbot

Must knowConcept lesson
01 Understand02 Recall03 Prepare

Words made simple

A few useful meanings before you read the explanation.

Streaming
Sending an answer in small pieces as it is produced, rather than waiting for the complete response.
API
A way for one piece of software to ask another for data or an action. A claims API might return the current status of a claim.
Token
A small piece of text processed by a model. It may be a whole word, part of a word, or punctuation.
More words from this lesson
JSON
A common text format for exchanging data using named fields, lists, numbers and values. Example: {"status": "pending"}.
State
The data describing where a workflow is now, what it already knows, and what still needs to happen.
Persistence
Saving information so it remains available after a request ends or a process restarts.
Authentication
Checking who a user or service is, such as by verifying a login session.
HTTP
The request-and-response protocol used on the web. HTTPS adds encryption to protect information in transit.

Overview

A streaming chatbot shows safe response pieces and progress as they arrive. Final completion remains a separate event.

Why this matters

Users see activity sooner during slow generation.

An everyday example

A support employee explains that they are checking records before finishing the answer.

A technical example

Stream typed SSE text, progress, done, and error events.

How it works

  1. Authenticate
  2. Establish provider stream
  3. Project safe events
  4. Handle backpressure/disconnect
  5. Persist final status.

Architecture and flow

Provider stream → Node event adapter → fetch/SSE → incremental React state.

The insurance assistant example

Show claim lookup progress and then the verified explanation.

When to use it

Interactive tasks where waiting is noticeable.

When to avoid it

Do not expose raw tool events or execute partial JSON.

Interview answer

“I stream safe application events and explicitly distinguish completion, interruption, and failure.”

Benefits, trade-offs, and production considerations

Test proxy buffering, reconnect, abort, and errors after headers. Buffer sensitive output until validation when risk requires it.

TypeScript
// Server-side SDK illustration, not a complete HTTP route.
// Call only after authentication, quotas, validation, and context construction.
import OpenAI from "openai";
const client = new OpenAI();
async function* answerEvents(
  model: string, message: string, signal: AbortSignal,
): AsyncGenerator<{ type: "text" | "done"; value?: string }> {
  const stream = await client.responses.create({
    model, store: false, stream: true, max_output_tokens: 500,
    input: [
      { role: "system", content: "Explain supplied text. Do not invent account facts." },
      { role: "user", content: message },
    ],
  }, { signal });
  let completed = false;
  for await (const event of stream) {
    if (event.type === "response.output_text.delta") {
      yield { type: "text", value: event.delta };
    }
    if (event.type === "response.completed") completed = true;
    if (event.type === "response.failed" || event.type === "response.incomplete") {
      throw new Error("Generation did not complete");
    }
  }
  if (!completed) throw new Error("Stream ended without completion");
  yield { type: "done" };
}
  • The HTTP adapter maps yielded values to its public stream format, handles refusals/provider errors according to product policy, applies backpressure, and converts errors into a safe terminal error event.
  • The browser renders text as escaped content, never unchecked HTML.
  • See official streaming documentation for the selected SDK's event types.
PAUSE & RECALL

Can you explain it without the notes?

Streaming AI chatbot

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

A streaming chatbot shows safe response pieces and progress as they arrive. Final completion remains a separate event.

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: RAG chatbot

Adapted from 13_AI.MD · “Streaming AI chatbot”. Examples are study fragments; framework behavior can depend on your version.