Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Calling a Language Model From JavaScript (Summary)

In this video, you replayed one real run of the batch script you built for Leaf & Kettle Tea’s inbox, and looked back at the module behind every call it made.

What You Can Do Now

You can now call a language model from a Node script with fetch(), through a small module that works with OpenAI or Anthropic. You keep the key secret, read the token usage and cost of every call, and handle rate limits, timeouts and cut-off answers.

The Whole Inbox for About a Cent

One real run of node --env-file=.env draft-inbox.js drafted a reply to each of the twelve emails and printed each draft with its token usage and its cost in cents. Yours will differ in wording, tokens and cost. The run ended with these two lines:

Language: Program Output
Saved 12 drafts. Held back: 0. Not drafted: 0.
Cost of reported usage: 1.017 cents

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 You’ve reached the end of Calling a Language Model From JavaScript. You can now call a language model from a Node script, through one small module, with either provider.

00:10 It keeps the key secret, prices every call, and handles rate limits, timeouts and cut-off answers.

00:19 Here’s one real run of your batch script over the tea importer’s inbox, replayed. Yours will differ in wording, tokens and cost. Each email gets a draft, its token usage and cost.

00:34 Twelve drafts saved, none held back, and the batch cost about one cent. Your stand-in check lets two emails through. With a tiny output limit, it held back both drafts.

00:47 Kenji’s draft promises to skip his January box. Nothing told the model the shop can do that. It predicted the promise. A person still reads every draft.

01:00 Behind those calls is your model folder. Your draft script calls ask(), and gets one result from either provider: the text, the stop reason, the usage, and whether it was cut off.

01:13 Inside, ask() retries what’s worth retrying and times out each attempt. Each provider has its own file, and the key comes from the environment.

01:26 Things to remember. A model predicts text, so a person checks it. Keep the key in .env, out of your code and Git. Price every call from its usage. Check cutOff, and that there’s text, before you use a reply.

01:42 Retry only what’s worth retrying, with a cap and a timeout.

01:48 This course is the first of five on building AI features. The next one picks up your model folder where you left it. Thanks for following along.

Become a Member to join the conversation.