Agentic AI - Agent Observability
You have a dictionary of responses with some missing 'completion_tokens' keys. How do you safely calculate total tokens without errors?
responses = [
{'usage': {'prompt_tokens': 10, 'completion_tokens': 5}},
{'usage': {'prompt_tokens': 20}},
{'usage': {'prompt_tokens': 15, 'completion_tokens': 10}}
]
cost_per_token = 0.0003
# Fill in the blank:
total_tokens = sum(____)
total_cost = total_tokens * cost_per_token
print(f"Total cost: ${total_cost:.4f}")