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

Count What a Call Costs

In this video, you priced a draft reply from the token counts in its usage, then ran the draft script over the first three emails in the tea importer’s inbox and estimated what a month of drafts would cost.

Key Points

  • Usage holds two counts. One real run for Hannah’s late parcel reported { inputTokens: 103, outputTokens: 60 }. For a short request like this one, with no cached input, those two numbers and the model’s prices are all you need to price the call.
  • A token isn’t a word. Split by one model family’s tokenizer, common words such as “still” and “arrived” are one token each, while “Okafor” takes three, the order number 4417 is cut in two, and “Darjeeling” takes three. Spaces, punctuation and line breaks are tokens too, and other models split text their own way. Names, IDs and numbers can take several tokens, so a word count runs low: take your counts from usage.
  • The instructions are input on every call. Hannah’s email and the instructions together are 65 words, and usage counted 103 input tokens, a few of them added by the request itself. You pay for those too.
  • Input and output have separate prices, per million tokens. When the video was recorded, the drafting model this course uses cost $2 per million input tokens and $10 per million output tokens. For Hannah’s draft, 103 × 2 + 60 × 10 = 806. Divided by 1,000,000, that’s 0.000806 dollars, and times 100 for cents, about 0.08 cents. The output tokens were 600 of the 806, about three quarters of the cost.
  • Output can include reasoning. A model can generate reasoning before its reply. Hannah’s draft had none, but one of the three drafts below did. Reasoning never shows up in the reply’s text, but it’s billed as output, so price a call from its output count, not from the reply’s length.
  • Estimate a month from a few real calls. Three drafts cost 0.119, 0.115 and 0.093 cents. In the REPL, ((0.119 + 0.115 + 0.093) / 3).toFixed(3) gives '0.109', an average of 0.109 cents a draft. At a guessed 40 emails a day, 0.109 × 40 × 30 = 130.8 cents, about $1.31 a month. Your counts will differ a little on every run, so rerun the estimate whenever the counts or the prices change.

The rule:

  • One call, in cents: (input tokens × input price + output tokens × output price) ÷ 1,000,000 × 100, with prices in dollars per million tokens. This holds for short requests like these, with no cached input: the next section says how your provider bills cached input.
  • One month: the average cost of a few real calls × the calls you expect.

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 Your draft script calls ask() and prints the reply and its usage. Before the tea importer runs it on every email, they’ll want to know what each draft costs.

00:10 Here’s one real run for Hannah’s late parcel. The usage holds two counts: 103 input tokens and 60 output tokens.

00:20 For these drafts, those two counts are all you need to price the call. Next, look at what they count.

00:29 Models read and write tokens, short chunks of text that are often a word with its space. Here’s Hannah’s email, split into tokens by one model family’s tokenizer.

00:40 Common words such as still and arrived are one token each. Her surname, Okafor, takes three. The order number, 4417, is cut in two, and Darjeeling takes three.

00:53 Spaces, punctuation and line breaks are tokens too. Other models split text their own way, so treat this split as one example.

01:04 The instructions are input too, sent again with every call. Together, they make 65 words, and usage counted 103 input tokens, a few of them added by the request itself.

01:16 Watch out: a token isn’t a word. Names, IDs and numbers can take several tokens, so a word count runs low. Take your counts from usage instead.

01:29 Providers price tokens by the million, with one price for input and a higher one for output. When this video was recorded, the drafting model this course uses cost 2 dollars per million input tokens and 10 dollars per million output tokens.

01:44 The text below this video has your provider’s current prices.

01:49 In the Node REPL, multiply each count by its price and add them up. 103 times 2, plus 60 times 10, gives 806. The prices are per million tokens, so divide by a million for dollars.

02:05 That’s a tiny fraction of a cent. Times 100 for cents, it’s about 0.08 cents.

02:14 Look at the output tokens. They make 600 of the 806, about three quarters of the cost. Watch out: output tokens can include reasoning, text the model generates before its reply.

02:28 It never shows up in the reply, but it’s billed, so price a call from its output count, not from the reply’s length.

02:37 Put that sum in a function, in a new file, cost.js. It takes a usage and the role the call used, capable by default, like ask(). It gets that role’s prices from pricesFor(), which you add to the model module.

02:51 Each role’s prices sit next to its model id in config.js. The text below shows your provider’s lines. The function multiplies each count by its price, divides by a million, and turns dollars into cents.

03:05 The underscores only group digits.

03:09 Next, a script drafts replies to the first three inbox emails. For each, it calls ask(), prices the usage, and prints the subject, the counts and the cost.

03:20 Run it, and the costs arrive call by call. The caffeine question cost 0.119 cents. The wholesale enquiry cost 0.115. Hannah’s parcel cost 0.093.

03:35 Hannah’s input is 103 tokens again, but her reply took 72 output tokens this time, so it cost more than before. Your counts will differ a little on every run.

03:48 For a month, average those three costs in the REPL: 0.109 cents a draft. Say the shop gets 40 emails a day. 0.109 times 40 times 30 gives 130.8 cents, about $1.31 a month.

04:06 Three calls and a guess make it rough, but rerun it whenever the counts or prices change. So a call costs each token count times its price, added up and divided by a million for dollars.

04:19 A month costs the average of a few real calls times the calls you expect.

04:25 Most of each draft’s cost is output. So would a lower output limit make drafts cheaper? The next video, Catch a Cut-Off Answer, tries that.

Become a Member to join the conversation.