Bird
0
0

Find the bug in this token cost calculation code:

medium📝 Debug Q7 of 15
Agentic AI - Agent Observability
Find the bug in this token cost calculation code:
response = {'usage': {'prompt_tokens': 100, 'completion_tokens': 50}}
cost_per_token = 0.0001
total_tokens = response['usage']['prompt_tokens'] + response['usage']['completion_tokens']
cost = total_tokens * cost_per_token
print(f"Cost: ${cost}")
Atotal_tokens calculation misses 'total_tokens' key
Bcost_per_token should be multiplied by 100
CNo bug, calculation is correct
Dcost should be divided by total_tokens
Step-by-Step Solution
Solution:
  1. Step 1: Verify token sum

    Sum of prompt and completion tokens is correct: 100 + 50 = 150.
  2. Step 2: Check cost calculation

    Cost is tokens times cost per token, which is correct.
  3. Final Answer:

    No bug, calculation is correct -> Option C
  4. Quick Check:

    Correct formula = No bug [OK]
Quick Trick: Sum tokens then multiply by cost per token [OK]
Common Mistakes:
  • Thinking total_tokens key is mandatory
  • Incorrect multiplication or division
  • Changing cost_per_token unnecessarily

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes