Start learning free

LESSON 5 OF 129 · AI ENGINEERING: CALLING A MODEL

Tokens and Cost

Practice

A model is not paid by the call. It is paid by the token: a piece of a word, about four characters of English text. Every reply reports its own counts in reply.usage. input_tokens is what the model had to read: the system prompt, if there is one, and every message you sent. output_tokens is what the model wrote.

Prices are quoted per million tokens, and output has its own, higher price. So the cost of a call is two small sums: tokens times the price, divided by one million.

An example: a call reads 300 tokens and writes 100, at 2 dollars per million input tokens and 10 dollars per million output tokens.

input:   300 × 2 / 1,000,000  = 0.0006 dollars
output:  100 × 10 / 1,000,000 = 0.001 dollars
total:                          0.0016 dollars

This lesson uses example prices: INPUT_PRICE is 1 dollar and OUTPUT_PRICE is 5 dollars per million tokens. Real prices differ from model to model and change over time, so look them up on the provider's pricing page.

The program asks for a thank-you note and must print the note, the two counts and the two costs:

Thank you for choosing our shop! We hope your lovely new teapot brings you many warm and cozy afternoons. Enjoy every cup, and come back to visit us again soon.
Input tokens: 16
Output tokens: 40
Cost in dollars: 0.000216
10,000 calls like this: 2.16

Your task: the call and the print of the note are already written. Print the input tokens and the output tokens from reply.usage. Compute input_cost and output_cost, add them into cost, and print the cost with six decimals. Then print what 10,000 calls like this one cost, with two decimals.


Assignment
Follow the Coding Tutorial to read the token counts of a reply and turn them into the cost of one call and of 10,000 calls.


Hint
Tokens times the price gives the dollars for a million times too many tokens, so divide by 1000000. round(cost, 6) keeps six digits after the point, and round(cost * 10000, 2) keeps two, like money.


STEP 1 OF 6
    Ctrl + Enter to check