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

Watch the Input Grow Turn by Turn

In this video, you priced every turn of Ravi’s thirty-message practice session and watched each request send more input than the one before. In the video’s run, the session cost four times what its first turn suggested.

Key Points

  • A long session of similar messages. Ravi’s session in the club’s data has thirty messages. He takes a table, orders, chats with the waiter, and pays. Each message is a sentence or two, about as long as the others.
  • Price each turn from its usage. The script from the last lesson gets three additions. It imports costInCents() from cost.js, which comes with the starting project. That function estimates a call’s cost in cents from its usage, at your model’s full input and output prices. It counts the turns. After each reply, it prints the turn’s input tokens and cost and adds the cost to a total. It leaves out the replies and prints one line per turn.
  • The input climbs every turn. In the video’s run, turn 1 sent 162 input tokens for 0.007 cents, and turn 30 sent 4,206 for 0.046 cents.
  • The cost climbs more slowly. The input grew about 26 times but the cost under 7 times, because each turn also pays for its reply, which costs more per token and stays about the same size. Your numbers will differ with your provider and its prices, but your input tokens will climb the same way. Your cents may jump around more, as the OpenAI run below shows.
  • A turn doesn’t cost only its new message. If every turn cost what the first one did, the session would come to 30 × 0.007 = 0.21 cents. In this run, it came to 0.84 cents, four times that. Its first fifteen turns came to about 0.27 cents, so twice the turns cost about three times as much. As sessions get longer, the bill grows faster than the number of turns.
  • Every request carries the whole history. Turn 1’s request carries the instructions and Ravi’s first message. Turn 2’s carries the same, plus the partner’s first reply and Ravi’s second message. Every turn adds two messages, the last reply and the new one, and sends everything before them again. By turn 30, the request carries 59 messages. The provider processes all of them as input, and you pay for every one.
  • Discounts don’t stop the growth. Some providers bill input they’ve already seen at a lower price, on their own or when the request asks for it. costInCents() prices every input token at the full rate, so its figures are estimates, and the provider switches below show where each provider reports cached input. But every turn still sends it all.
  • The context window is a ceiling. Each model accepts only so many tokens in one request, its context window, and the reply has to fit in it too. It’s far larger than this session, but one that keeps going would eventually fill it.

Every turn sends the whole history as input, so the input grows with the conversation, and over a session, the cost grows with it. To keep both the cost and the size in check, the partner needs a limit.

The Code

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 partner now remembers a whole session. In this video, you’ll see what a long session costs, turn by turn.

00:08 Ravi’s session in the club’s data is the long one. He takes a table, orders, chats with the waiter, and pays, in thirty messages. Each one is a sentence or two, about as long as the others.

00:21 To price each turn, the script from the last lesson gets three additions. It imports costInCents() from cost.js, which comes with your starting project. That function estimates a call’s cost in cents from its usage, at your model’s prices.

00:37 The script counts the turns. And after each reply, it prints the turn’s input tokens and cost, and adds the cost to a total.

00:45 Run it, and the first line arrives: 162 input tokens, for 0.007 cents.

00:54 Ravi’s other messages are all about as long as his first one, and those 162 tokens also include the partner’s instructions. About how many input tokens will turn 30 send?

01:06 Your turn: pause the video and predict.

01:20 4,206. Each turn sends more. The cost per turn climbs too, but more slowly than the input, because each turn also pays for its reply. Your numbers will differ with your provider and its prices, but your input tokens will climb the same way.

01:40 If every turn cost what the first one did, the session would come to 0.21 cents. In this run, it came to 0.84 cents, four times that. And all thirty turns cost about three times what the first fifteen did.

01:55 As sessions get longer, the club’s bill grows faster than the number of turns.

02:01 Here’s why. Turn 1’s request carries the instructions and Ravi’s first message. Turn 2’s carries the same, plus the partner’s first reply and Ravi’s second message.

02:14 Every turn adds two messages, the last reply and the new one, and sends everything before them again.

02:21 By turn 30, the request carries 59 messages. The provider processes all of them as input, and you pay for every one. Some providers bill input they’ve already seen at a lower price, on their own or when the request asks for it. costInCents() uses the full price, so check what your provider reports, and the text below shows where.

02:43 But every turn still sends it all.

02:47 So a turn never costs only its new message. Every turn sends the whole history as input, so the input grows with the conversation, and over a session, the cost grows with it.

03:00 The history can’t grow forever, either. Each model accepts only so many tokens in one request, its context window, and the reply has to fit in it too. It’s far larger than this session, but one that keeps going would eventually fill it.

03:15 To keep both in check, the partner needs a limit. Next, in the lesson Trim the History to a Budget, you’ll keep the history under a token budget you choose.

Become a Member to join the conversation.