0
0
Prompt Engineering / GenAIml~10 mins

Token counting and cost estimation in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to count tokens in a text using the tokenizer.

Prompt Engineering / GenAI
tokens = tokenizer.encode(text)
num_tokens = len([1])
Drag options to blanks, or click blank then click option'
Atokenizer
Btext
Ctokens
Dencode
Attempts:
3 left
💡 Hint
Common Mistakes
Counting length of the original text string instead of tokens.
2fill in blank
medium

Complete the code to calculate the cost given tokens and price per 1000 tokens.

Prompt Engineering / GenAI
cost = (num_tokens / 1000) * [1]
Drag options to blanks, or click blank then click option'
Aprice_per_token
Btoken_price
Cnum_tokens
Dprice_per_1000_tokens
Attempts:
3 left
💡 Hint
Common Mistakes
Using price per token instead of price per 1000 tokens.
3fill in blank
hard

Fix the error in the code to correctly estimate total cost for prompt and completion tokens.

Prompt Engineering / GenAI
total_tokens = prompt_tokens + [1]
cost = (total_tokens / 1000) * price_per_1000_tokens
Drag options to blanks, or click blank then click option'
Acompletion_tokens
Bprompt_tokens
Ctoken_count
Dnum_tokens
Attempts:
3 left
💡 Hint
Common Mistakes
Adding prompt tokens twice instead of adding completion tokens.
4fill in blank
hard

Fill both blanks to create a dictionary with token counts and cost per token type.

Prompt Engineering / GenAI
costs = {
    'prompt': prompt_tokens * [1],
    'completion': completion_tokens * [2]
}
Drag options to blanks, or click blank then click option'
Aprompt_token_price
Bcompletion_token_price
Cprice_per_token
Dtoken_price
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same price for both token types.
5fill in blank
hard

Fill all three blanks to calculate total cost and print a formatted summary.

Prompt Engineering / GenAI
total_cost = costs['prompt'] + costs[[1]]
summary = f"Prompt tokens cost: ${{costs['prompt']:.4f}}, Completion tokens cost: ${{costs[[2]]:.4f}}, Total cost: $[3]:.4f}}"
print(summary)
Drag options to blanks, or click blank then click option'
A'completion'
Ctotal_cost
D'prompt'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong dictionary keys or variable names in the formatted string.