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?

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

Thanks, this helped. One extra tip for anyone using the AI SDK or another wrapper: the cost field doesn’t always survive there, some SDKs strip the raw usage object. In that case I just work it out myself from the token counts — input tokens times the model’s input price, plus output tokens times the output price, plus the request cost where it applies. Adding that up per request over a day or a month gives you the total. Just double-check the current per-model prices first, they do change from time to time.

Heads-up: the code sample above contains what looks like a live API key. If it is real, revoke or rotate it in the dashboard and use an environment variable instead, since public posts get scraped. For the original cost question, if the AI SDK only exposes token counts, log the full provider response or check provider metadata before falling back to calculating input/output tokens at current per-model prices.

With @ai-sdk/perplexity the cost isn’t in result.usage at all: it comes back in result.providerMetadata.perplexity.cost (total cost plus the token and tool breakdown, in USD), so there’s no extra request parameter to set. One catch if you upgrade: v5 of the provider talks to the Agent API only, so old Sonar model IDs have to be swapped for presets like fast or low, and the cost fields can differ from what v4 returned. Perplexity’s pricing docs also describe that total as an estimate, so I’d still reconcile against Billing in the API console. (Disclosure: I write for aicost.tools.)