Bird
0
0

Which of the following is the correct way to track token usage in a Python script using an AI API?

easy📝 Syntax Q12 of 15
Agentic AI - Agent Observability
Which of the following is the correct way to track token usage in a Python script using an AI API?
Atokens_used = response['total_tokens']
Btokens_used = response['usage']['total_tokens']
Ctokens_used = response['usage']['tokens']
Dtokens_used = response['token_count']
Step-by-Step Solution
Solution:
  1. Step 1: Identify the correct key for token usage in API response

    Most AI APIs return token usage under response['usage']['total_tokens'].
  2. Step 2: Compare options with common API response structure

    Only tokens_used = response['usage']['total_tokens'] matches the standard nested key for total tokens used.
  3. Final Answer:

    tokens_used = response['usage']['total_tokens'] -> Option B
  4. Quick Check:

    Correct key path = response['usage']['total_tokens'] [OK]
Quick Trick: Look for nested 'usage' then 'total_tokens' key [OK]
Common Mistakes:
  • Using wrong key names like 'token_count'
  • Missing nested 'usage' dictionary
  • Assuming flat keys for token counts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes