Welcome to Calling a Language Model From JavaScript! You’ve typed into AI chat tools. In this course, you’ll call a language model from your own Node script and treat it as what it is: an outside service that costs money on every call, can fail, and can return a reply that’s wrong. Your code handles the cost and the failures, and a person checks every draft for wrong answers.
What You’ll Build
The course follows Leaf & Kettle Tea, a small online tea importer whose inbox fills up with the same kinds of email every week: a late parcel, a question about caffeine, a café’s wholesale enquiry, and a complaint written in Spanish. You’ll build a script that drafts a reply to each of the inbox’s twelve emails and prints each draft with its token usage and its cost. A person at the shop then checks every draft before anything is sent.
Here’s how one real run of that script began. Yours will differ in wording, tokens, and cost:
#1 Caffeine in the sencha? { inputTokens: 109, outputTokens: 60 } 0.082 cents
Hi Priya,
Sencha typically has 20–50 mg of caffeine per cup, versus 80–120 mg for
brewed coffee. It varies with brewing. If caffeine affects your sleep,
we’d suggest a caffeine-free herbal tea in the evening.
Leaf & Kettle Tea
And here’s how it ended, with twelve drafts for about one cent:
Saved 12 drafts. Held back: 0. Not drafted: 0.
Cost of reported usage: 1.017 cents
Tokens and cost each get their own lesson later in the course.
What You’ll Learn
By the end of this course, you’ll be able to call a language model from a Node script with fetch(), through a small module that works with OpenAI or Anthropic. You’ll keep the key secret, read the token usage and cost of every call, and handle rate limits, timeouts and cut-off answers.
Step by step, you’ll:
- Send your first call with
fetch(), after getting a key and loading it from the environment - Build one small module that works with OpenAI or Anthropic, and keep your key secret
- Price every call from its token usage, and catch answers that were cut off
- Handle rate limits and timeouts, retrying only what’s worth retrying
- Put it all together in a project that drafts replies for the whole inbox
Before You Start
This is the first of five courses on building AI features in JavaScript. The small module you build here carries on into the other four, and each course’s downloads include it, so you can also start a later course on its own.
You should already know how to use fetch(), async and await, error handling and retries, cancellation with AbortController, JSON, and Node scripts with npm. You don’t need any experience with language models beyond typing into a chat tool.
You’ll need:
- Node 26, a terminal, and any editor. The course’s runs used Node v26.13.1.
- A key for one model provider, OpenAI or Anthropic, for the lessons and the project that call a real model. An early lesson walks you through getting one and setting a spending limit. At November 2026 prices, following the whole course costs around ten cents, depending on how often you rerun examples.
- No key for the exercises. They run on a stand-in model.
How the Course Works
- Text lessons carry most of the course. Where code differs between providers, a switch shows both versions, so open the one for your provider.
- Four video lessons cover the parts where something moves on screen.
- Exercises run in your browser, against a stand-in model that answers like a provider, so they need no key and cost nothing.
- A mentor scenario has you explain your inbox script and change it, and a quiz checks what you’ve learned.
- Downloads include the starting project (with the inbox in
data/inbox.jsonand a.env.example), the finished code, and the course slides.
The next lesson, See What a Model Does, starts with what a model does with a single request.