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

Catch a Cut-Off Answer

In this video, you lowered the output limit to make the tea importer’s drafts cheaper, watched every draft get cut off mid-sentence, and taught ask() to say when that happens so the draft script can hold a cut-off draft back.

Key Points

  • A lower output limit doesn’t give you a shorter reply. With MAX_OUTPUT_TOKENS=50, every draft was cut off mid-sentence: Priya’s stopped partway through a sentence about caffeine, Marcus’s at “I’d be happy to check wholesale”, and Hannah’s in the middle of the signature, at “Leaf & K”. Where yours stops will differ. Each draft did cost less: 50 output tokens, about 0.07 cents, down from about a tenth of a cent.
  • The model doesn’t plan around the limit. The limit travels in the request as a setting the provider applies, not as text the model reads: the model reads only the instructions and the email. It generates its reply the same way it would with no limit, one token at a time, and the provider stops it at the limit, even mid-word. Reasoning tokens count toward the limit too.
  • A cut-off call still succeeds. The script finished normally, ask() threw no error, and you paid for all 50 tokens. Only the stop reason says the reply was cut off.
  • ask() turns the stop reason into cutOff. Each provider has its own word for “hit the output limit”, so you add it to each provider file as a constant, CUT_OFF (your provider’s line is below). adapter holds everything its provider file exports, so adapter.CUT_OFF is your provider’s word. ask() compares the reply’s stop reason with it and adds cutOff, true or false, to every result. Your feature code never needs a provider’s word.
  • cutOff: false isn’t a guarantee. It only means the limit didn’t stop the reply. It doesn’t promise a usable draft.
  • Hold a cut-off draft back. The draft script checks reply.cutOff, prints a “Held back” line, and moves on with continue, so the draft’s text is never used. You still pay for every token the provider generated, finished or not.
  • Ask for length in the instructions. The model usually follows a length you ask for there, not always, so the cap and the cutOff check stay. With “under 40 words” in the instructions and the limit back at 400, the drafts took 55, 47 and 43 output tokens. That’s the saving you wanted: about what the cut-off ones cost, under a tenth of a cent, and every draft was finished and signed. Your drafts will differ in wording.

The rule: the output limit stops a reply, it doesn’t shorten it. Ask for length in the instructions, keep the limit as a safety cap well above the reply you expect (400 here, against drafts of about 50 tokens), and check cutOff before you use a draft.

Your Provider’s Word for a Cut-Off

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 The last video ended on a question. Most of a draft’s cost is output, so would a lower output limit make drafts cheaper?

00:09 Your drafts used about 60 to 100 output tokens, and the limit in your env file is 400. So you lower it to 50, expecting shorter replies. To read those shorter replies, you add one line to the draft script from the last video.

00:25 It prints each reply’s text under its usage and cost. What will the three drafts look like with a limit of 50 tokens? Your turn: pause the video and predict.

00:44 Every draft is cut off mid-sentence. Priya’s stops on the word our. Marcus’s stops on wholesale. Hannah’s stops in the middle of the signature, at Leaf and K.

00:55 Where yours stops will differ.

00:58 And each one did cost less, at 50 output tokens and about 0.07 cents, down from about a tenth of a cent. Here’s Hannah’s draft again. The limit does travel in the request, but as a setting the provider applies.

01:14 The model reads only the instructions and the email. It generates its reply the same way it would with no limit, one token at a time. At the fiftieth output token, the provider stops it, mid-word.

01:28 Reasoning tokens count toward the limit too. And nothing complained. The script finished normally, and ask() threw no error. You paid for all 50 tokens. To your script, a cut-off draft looks like a success.

01:43 The response does say why generation stopped. That’s the stop reason ask() already returns. Each provider has its own word for a reply that hit the limit.

01:54 So you add that word to each provider file as a constant, CUT_OFF. The text below this video has your provider’s line.

02:03 In model/index.js, ask() now keeps the reply that send() returns. adapter holds everything its provider file exports, so adapter.CUT_OFF is your provider’s word.

02:15 ask() compares the reply’s stop reason with it, and stores the answer on the reply as cutOff, true or false. False only means the limit didn’t stop the reply.

02:25 It doesn’t promise a usable draft. Your feature code never needs to know a provider’s word.

02:33 Now the draft script can check it. After printing the usage, an if statement checks cutOff on the reply. A cut-off draft gets a Held back line, and continue moves on to the next email, so its text is never used.

02:47 Run it with the limit still at 50. All three drafts are held back.

02:54 A shorter draft has to come from the instructions. The model generates its reply from them, so a length you ask for there shapes the whole reply. That usually works, but not always, so the cap and the cutOff check stay.

03:07 Change under 80 words to under 40 words, and put the limit back to 400. Run it again. The drafts take 55, 47 and 43 output tokens here; yours will differ. They cost about what the cut-off ones did, under a tenth of a cent, so you get the saving you wanted.

03:26 And every draft is finished and signed. So the output limit doesn’t shorten a reply. It stops one. Ask for length in the instructions. Keep the limit as a safety cap well above the reply you expect, like 400 here against drafts of about 50 tokens.

03:44 Then check cutOff before you use a draft.

03:48 Next, in Exercise: Total a Batch and Hold Cut-Offs, you’ll add up what a batch of inbox drafts costs and keep the cut-off ones out of the drafts you save.

Become a Member to join the conversation.