Complete the code to count tokens in a text using the tokenizer.
tokens = tokenizer.encode(text) num_tokens = len([1])
The variable tokens holds the list of tokens, so len(tokens) gives the token count.
Complete the code to calculate the cost given tokens and price per 1000 tokens.
cost = (num_tokens / 1000) * [1]
The cost is calculated by multiplying the number of tokens (in thousands) by the price per 1000 tokens.
Fix the error in the code to correctly estimate total cost for prompt and completion tokens.
total_tokens = prompt_tokens + [1] cost = (total_tokens / 1000) * price_per_1000_tokens
The total tokens are the sum of prompt tokens and completion tokens, so add completion_tokens.
Fill both blanks to create a dictionary with token counts and cost per token type.
costs = {
'prompt': prompt_tokens * [1],
'completion': completion_tokens * [2]
}Multiply prompt tokens by their price and completion tokens by their price to get costs per type.
Fill all three blanks to calculate total cost and print a formatted summary.
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)
The total cost adds prompt and completion costs. The summary prints costs for prompt, completion, and total cost.