Loading video player…

Calling a Language Model From JavaScript (Overview)

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:

Language: Program Output
#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:

Language: Program Output
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.json and 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.

Download

Course Slides (.pdf)

550.8 KB
Download

Sample Code (.zip)

51.0 KB

00:00 Welcome to Calling a Language Model From JavaScript. You’ve used AI chat tools. In this course, you’ll call a model from your own Node script, and handle what goes wrong: cost, failures and wrong answers.

00:16 Here’s one real run of the script you’ll build, drafting replies to a tea shop’s emails. Each email gets a draft, its token usage and cost. Yours will differ.

00:27 Tokens get their own lesson later.

00:31 Twelve drafts for about one cent.

00:35 Those emails are the inbox of Leaf & Kettle Tea, a tea importer. There’s a question about caffeine, a late parcel, a complaint in Spanish. Your script drafts the replies, and since replies can be wrong, a person checks each one before it’s sent.

00:55 First, you’ll use fetch to call a model. Then you’ll build one small module for OpenAI or Anthropic, and keep your key out of your code. You’ll price every call and catch answers that were cut off.

01:08 You’ll handle rate limits and timeouts. And the project drafts replies for the whole inbox.

01:16 This is the first of five courses on building AI features, and your module carries on through all five. You should already know fetch, async and await, and Node scripts.

01:28 You’ll need Node 26 and an OpenAI or Anthropic key.

01:34 Most lessons are text, with a switch for your provider’s code, and four are videos. Exercises run in your browser on a stand-in model, so they need no key and cost nothing.

01:46 A mentor scenario and a quiz close the course. Downloads hold the code and slides. Next up is See What a Model Does.

Become a Member to join the conversation.