you can get the usage metrics from the response JSON by parsing it like so:
import requests
response = requests.post(
'https://api.perplexity.ai/chat/completions',
headers={
'Authorization': 'Bearer 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',
'Content-Type': 'application/json'
},
json={
'model': 'sonar-pro',
'messages': [
{
'role': 'user',
'content': "What are the major AI developments and announcements from today across the tech industry?"
}
]
}
)
response.json()['usage']
Here’s an example from colab notebook:
Full example notebook here: Google Colab
