# Count of token with cache\_context same as without cache\_context

**URL:** <https://discuss.ai.google.dev/t/count-of-token-with-cache-context-same-as-without-cache-context/99180>\
**Category:** Gemini API\
**Tags:** context\_caching\
**Created:** [August 21, 2025, 2:43pm UTC](https://discuss.ai.google.dev/t/count-of-token-with-cache-context-same-as-without-cache-context/99180 "2025-08-21T14:43:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Marco\_Fosci](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/marco_fosci/32/61446_2.png) [@Marco\_Fosci](https://discuss.ai.google.dev/u/Marco_Fosci)\
**Post date:** [August 21, 2025, 2:43pm UTC](https://discuss.ai.google.dev/t/count-of-token-with-cache-context-same-as-without-cache-context/99180/1 "2025-08-21T14:43:06Z")

</div>

**Problem Description**  
I noticed a discrepancy between the total\_token\_count value returned by the API and the expected calculation based on usage metadata, particularly when using cache (CreateCachedContent).

**Expected Behavior**  
I would have expected total\_token\_count to be calculated as the sum of non-cached tokens (prompt\_token\_count - cached\_content\_token\_count) and generated tokens (candidates\_token\_count). This would reduce the total token count for requests using cache, reflecting a cost savings.

**Current Behavior**  
The total\_token\_count value instead appears to be calculated as the full sum of prompt\_token\_count and candidates\_token\_count, ignoring the cached portion. This behavior is cost-misleading, as it doesn’t reflect the benefit of cache.

**Reproduction Steps**  
Make a first API call with a large prompt.  
Make a second call with the same prompt, ensuring the content is served from the cache.  
Analyze the usage metadata from the second call.

# Technical Details

**API Call (cached):**

response = self.client.models.generate\_content(  
model=self.model,  
contents=[dynamic\_prompt],  
config=types.GenerateContentConfig(  
cached\_content=cache\_id,  
\*\*self.model\_config)  
)

**Example of USAGE METADATA (without cache, i.e. without the cached\_content=cache\_id line, in the code):**  
cache\_tokens\_details=None cached\_content\_token\_count=None candidates\_token\_count=194 candidates\_tokens\_details=[ModalityTokenCount(  
modality=\<MediaModality.TEXT: ‘TEXT’\>,  
token\_count=194  
)] prompt\_token\_count=5202 prompt\_tokens\_details=[ModalityTokenCount(  
modality=\<MediaModality.TEXT: ‘TEXT’\>,  
token\_count=5202  
)] thoughts\_token\_count=None tool\_use\_prompt\_token\_count=None tool\_use\_prompt\_tokens\_details=None total\_token\_count=5396 traffic\_type=None

**Example of USAGE METADATA (before using cache):**  
cache\_tokens\_details=[ModalityTokenCount(  
modality=\<MediaModality.TEXT: ‘TEXT’\>,  
token\_count=4115  
)] cached\_content\_token\_count=4115 candidates\_token\_count=199 candidates\_tokens\_details=[ModalityTokenCount(  
modality=\<MediaModality.TEXT: ‘TEXT’\>,  
token\_count=199  
)] prompt\_token\_count=5332 prompt\_tokens\_details=[ModalityTokenCount(  
modality=\<MediaModality.TEXT: ‘TEXT’\>,  
token\_count=5332  
)] thoughts\_token\_count=None tool\_use\_prompt\_token\_count=None tool\_use\_prompt\_tokens\_details=None total\_token\_count=5531 traffic\_type=None

**Expected vs. Actual Count:**

Expected Count: = 1217 + 199 = 1416

Actual Count: 5332 + 199 = 5531

**Impact**  
This behavior makes it difficult to accurately estimate the costs of API calls that use the cache, as token count optimization is not apparent in the usage data. This can lead to incorrect cost calculations and potential resource waste.

---

<div class="post-metadata">

**Author:** ![Krish\_Varnakavi1](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/krish_varnakavi1/32/56962_2.png) [@Krish\_Varnakavi1](https://discuss.ai.google.dev/u/Krish_Varnakavi1)\
**Post date:** [August 23, 2025, 2:16am UTC](https://discuss.ai.google.dev/t/count-of-token-with-cache-context-same-as-without-cache-context/99180/3 "2025-08-23T02:16:09Z")

</div>

Hi @Marco_Fosci,

Thanks for the reproduction steps.. I will try to reproduce this issue and troubleshoot what’s going on.

---

<div class="post-metadata">

**Author:** ![Doodi\_Saiteja](https://d1dlmcr85iqnpo.cloudfront.net/user_avatar/discuss.ai.google.dev/doodi_saiteja/32/31209_2.png) [@Doodi\_Saiteja](https://discuss.ai.google.dev/u/Doodi_Saiteja)\
**Post date:** [November 14, 2025, 1:58pm UTC](https://discuss.ai.google.dev/t/count-of-token-with-cache-context-same-as-without-cache-context/99180/5 "2025-11-14T13:58:17Z")

</div>

Hi @Marco_Fosci ,

The usage\_metadata can be confusing but it’s really just for tracking purposes, not a reflection of your final billable tokens.

You’re also right that the bill _will_ be less with context caching. But your described expected calculations are wrong. The actual billing calculation is more complex and dependent on many more parameters. Please refer “[https://cloud.google.com/blog/products/ai-machine-learning/vertex-ai-context-caching](https://cloud.google.com/blog/products/ai-machine-learning/vertex-ai-context-caching)” for more details on price optimization using context caching.

Context caching does not reduce the prompt token count. The prompt token count reflects the total number of input tokens for the model, regardless of whether they originate from the cache.

The main benefit of context caching is that the cached tokens are priced at a lower rate, not that it decreases the overall prompt token count.
