# Question with "Usage" in OpenAI compatibility

**URL:** <https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760>\
**Category:** Gemini API\
**Tags:** openai\_compatibility\
**Created:** [August 19, 2025, 4:45am UTC](https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760 "2025-08-19T04:45:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Potato](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/potato/32/24119_2.png) [@Potato](https://discuss.ai.google.dev/u/Potato)\
**Post date:** [August 19, 2025, 4:45am UTC](https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760/1 "2025-08-19T04:45:37Z")

</div>

Hi, I’m using the Gemini 2.5 Pro model with OpenAI compatibility for my project. The openai node sdk version I am using is `5.12.2`

I am trying to use the usage object to approximate cost per request but I am seeing some odd behavior.

Here is an example usage from a chat Completion response:

```auto
 usage: { completion_tokens: 102, prompt_tokens: 758, total_tokens: 1725 },

```

I am assume the completion\_token didn’t include the thinking\_tokening. It doesn’t make sense to me that `completion_tokens + prompt_tokens != total_tokens`

Another question is that how can I get the information on cached\_tokens?  
Here is an example usage when I run with GPT-5 with low reasoning:

```auto
usage: {
    prompt_tokens: 1486,
    completion_tokens: 651,
    total_tokens: 2137,
    prompt_tokens_details: { cached_tokens: 1408, audio_tokens: 0 },
    completion_tokens_details: {
      reasoning_tokens: 512,
      audio_tokens: 0,
      accepted_prediction_tokens: 0,
      rejected_prediction_tokens: 0
    }
  }

```

The cached token information is inside `prompt_tokens_details` but it is missing in the usage object from gemini model.

Would I have to switch to use the google gemini node sdk in order to get a more accurate usage object?

---

<div class="post-metadata">

**Author:** ![Lalit\_Kumar](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lalit_kumar/32/37058_2.png) [@Lalit\_Kumar](https://discuss.ai.google.dev/u/Lalit_Kumar)\
**Post date:** [August 21, 2025, 7:06am UTC](https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760/3 "2025-08-21T07:06:23Z")

</div>

Hello,

I would recommend going through [count token doc](https://ai.google.dev/gemini-api/docs/tokens?hl=en&lang=python) for detailed information on this topic.

To answer your questions:

1. All tokens are processed in a query are included in total token that include input, output and thinking.

2. To get token count with Google SDK, you can refer to [count token doc.](https://ai.google.dev/gemini-api/docs/tokens?hl=en&lang=python#text-tokens) Code:

```auto
from google import genai

client = genai.Client()
prompt = "The quick brown fox jumps over the lazy dog."

# Count tokens using the new client method.
total_tokens = client.models.count_tokens(
    model="gemini-2.0-flash", contents=prompt
)
print("total_tokens: ", total_tokens)
# ( e.g., total_tokens: 10 )

response = client.models.generate_content(
    model="gemini-2.0-flash", contents=prompt
)

# The usage_metadata provides detailed token counts.
print(response.usage_metadata)
# ( e.g., prompt_token_count: 11, candidates_token_count: 73, total_token_count: 84 )

```

1. If you run “ **response.usage\_metadata** ” command you will get information about **cached\_tokens** as well.

2. To get token count with OpenAI compatibility you can use “ **response.usage** ”. Example code below:

```auto
from openai import OpenAI

client = OpenAI(
    api_key="GEMINI_API_KEY",
    base_url="https://generativelanguage.googleapis.com/v1beta/openai/"
)

response = client.chat.completions.create(
    model="gemini-2.5-flash",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {
            "role": "user",
            "content": "Explain to me how AI works"
        }
    ]
)

print(response.usage)

```

---

<div class="post-metadata">

**Author:** ![Potato](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/potato/32/24119_2.png) [@Potato](https://discuss.ai.google.dev/u/Potato)\
**Post date:** [August 21, 2025, 2:57pm UTC](https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760/5 "2025-08-21T14:57:12Z")

</div>

Hi Lalit Kumar,  
I am not sure I am understanding your comment.  
This is the object when I print out response.usage

```auto
{completion_tokens: 102, prompt_tokens: 758, total_tokens: 1725}

```

The model I used was `gemini-2.5-pro` and nodeJS sdk with OpenAI compatibility .

The input token + output token does not equal the total token which it is not a blocker for me because I can get the real output token by doing total minus prompt.

I was reading the link you shared [Understand and count tokens &nbsp;|&nbsp; Gemini API &nbsp;|&nbsp; Google AI for Developers](https://ai.google.dev/gemini-api/docs/tokens?hl=en&lang=python)

Non of the example response included `cached_content_token_count`, does this mean that it only response with that field if cached\_content\_token\_count \> 0 ? Does the same apply to OpenAI compatibility?

---

<div class="post-metadata">

**Author:** ![Lalit\_Kumar](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/lalit_kumar/32/37058_2.png) [@Lalit\_Kumar](https://discuss.ai.google.dev/u/Lalit_Kumar)\
**Post date:** [August 22, 2025, 6:41am UTC](https://discuss.ai.google.dev/t/question-with-usage-in-openai-compatibility/98760/7 "2025-08-22T06:41:17Z")

</div>

Hello,

To understand context caching token count in detail you can go through [context caching docs](https://ai.google.dev/gemini-api/docs/caching?hl=en&lang=python). [Here](https://ai.google.dev/gemini-api/docs/caching?hl=en&lang=python#generate-content) you can find a python example as well.
