# How to get total API cost?

**URL:** <https://community.perplexity.ai/t/how-to-get-total-api-cost/1275>\
**Category:** General\
**Created:** [August 31, 2025, 5:58pm UTC](https://community.perplexity.ai/t/how-to-get-total-api-cost/1275 "2025-08-31T17:58:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![slooowshutter](https://sea1.discourse-cdn.com/flex001/user_avatar/community.perplexity.ai/slooowshutter/32/867_2.png) [@slooowshutter](https://community.perplexity.ai/u/slooowshutter)\
**Post date:** [August 31, 2025, 5:58pm UTC](https://community.perplexity.ai/t/how-to-get-total-api-cost/1275/1 "2025-08-31T17:58:27Z")

</div>

I saw [this post](https://community.perplexity.ai/t/cost-information-now-included-in-api-response-json/833) 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?

---

<div class="post-metadata">

**Author:** ![tonykipkemboi](https://sea1.discourse-cdn.com/flex001/user_avatar/community.perplexity.ai/tonykipkemboi/32/886_2.png) [@tonykipkemboi](https://community.perplexity.ai/u/tonykipkemboi)\
**Post date:** [September 3, 2025, 2:34am UTC](https://community.perplexity.ai/t/how-to-get-total-api-cost/1275/2 "2025-09-03T02:34:42Z")

</div>

you can get the **`usage`** metrics from the response JSON by parsing it like so:

```python
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:

 ![Screenshot 2025-09-02 at 10.32.00 PM](https://us1.discourse-cdn.com/flex001/uploads/sonar1/original/1X/38fcb3431308f866166770bc7607c51231486a33.png)

Full example notebook here: [Google Colab](https://colab.research.google.com/drive/1xl08FxtrH2pzRClPXbs8_HgE9vRhCg0q?usp=sharing)
