Welcome to Streaming AI Answers to a Web Page! In an AI chat window, the answer appears a few words at a time, so you start reading while the model is still generating it. In this course, you’ll build that into a web page. The page talks to your own server, the server keeps the API key and calls the model, and a Stop button ends the server’s call to the model as well as the page’s request.
What You’ll Build
The course follows a community language exchange club whose members practice Spanish between meetups. The club wants a practice partner on its page. A member orders at a café in Spanish, and the partner replies in Spanish and ends with one tip, in English, about a mistake in the member’s message.
Every message a member sends is a call to a model, so every message costs the club money. The page talks only to the club’s server, and only the server holds the key. The server streams the reply back, keeps each session’s history under a token budget, and stops its own call when the member clicks Stop.
Here’s a first taste from one real session on the finished page. Mei sent her first message, spotted a mistake in it, and clicked Stop to fix it. Then she sent the message again, fixed, and the reply arrived a few words at a time. The chat panel showed this, and your replies will differ:
Hola, me llamo Mei. Es mi primero vez aquí.
¡Hola, Mei, bienvenida a nuestro café! [Stopped]
Hola, me llamo Mei. Es mi primera vez aquí.
¡Hola, Mei, bienvenida a nuestro café! Qué bien que estés aquí por primera
vez. ¿Qué le gustaría tomar hoy?
Tip: "Es mi primero vez" should be "Es mi primera vez," because "vez" is
feminine, so the adjective must be "primera" too.
The reply ends with the partner’s tip about the mistake in Mei’s first try. That first try is still part of the conversation, so the tip is about it rather than the fixed message.
What You’ll Learn
By the end of this course, you’ll be able to stream a model’s answer from a Node server to a web page as it’s written, through streamAnswer() in the model module. You’ll keep the conversation’s history within the context window and let the user stop an answer, with the key kept on the server.
Step by step, you’ll:
- Stream an answer by asking a model for its reply as it’s written, reading the stream’s pieces in a loop, and adding
streamAnswer()to your model module, for OpenAI or Anthropic - Keep the conversation by sending its history with each call, and trimming it to a token budget so a long session stays within the context window and its cost levels off
- Stream to the page from a small Express server that holds the key, with a Stop button that also stops the server’s call to the model
- Put it all together in a project that builds the club’s whole practice partner
Before You Start
This is the third of five courses on building AI features in JavaScript. It builds on a small model/ module whose ask() function sends instructions and an input to your provider and returns the reply with its token usage. The lesson that adds streaming walks through the rest of it. If you’re starting here, you don’t need to have built it. The course’s code download, linked in this lesson, has the starting project with model/ ready.
You should already know how to use fetch(), async and await, errors and retries, cancellation with AbortController, JSON, Node.js scripts with npm packages, and that a model call is billed by the token. If the async part is new to you, then Async JavaScript for Pythonistas covers it.
You don’t need to know any server code. This is the path’s first course with a server, and it teaches only the parts of the Express framework the feature uses.
You’ll need:
- Node 26, a terminal, and any editor. The course’s runs used Node v26.14.0 and Express 5.3.0.
- A key for one model provider, OpenAI or Anthropic, for the lessons and the project that call a real model. The starting project’s
.env.exampleshows where the key goes.
The real calls are cheap. In the course’s runs, a whole thirty-message practice session through the finished partner, on each provider’s fast model, cost under three cents. A low monthly spending limit with your provider, such as five dollars, leaves plenty of room to rerun things.
How the Course Works
- Text lessons carry most of the course, and you build the club’s project in them as you type along. Where code differs between providers, each version sits in its own collapsible section, With OpenAI or With Anthropic, so open the one for your provider.
- Five video lessons show the partner at work as a stream breaks midway, the conversation grows, and the reply arrives on the page and stops.
- Exercises run in your browser, on a stand-in model, so they need no key.
- A project puts the partner together, a mentor chat reviews it with you as you explain your code and change it, and a quiz checks what you’ve learned. A short summary video closes the course.
- Downloads include the starting project and the finished code.
The next lesson, Ask for a Stream and Watch the Chunks, sets up the club’s project and asks your provider for its first streamed answer.