How to get total API cost?

I saw this post was mentioning that the API is now returning the full cost of the request but I can’t manage to see it. I have been using the AI SDK and do get some usage like input and output token but no cost, total_tokens or search_context_size like the previous post.

Do we need to add a specific parameter in the request body to see it?

2 Likes

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

4 Likes