Gemini Context Caching Not Hitting
You created cached_content but bills show full input token pricing — the cache isn't being applied. Common causes: TTL expired between calls, minimum 4096 tokens not met, content or model mismatch, or you forgot to reference the cache name in generate_content.
usage.cached_content_token_count is 0 despite cacheCategory: CachingCost impact: 75% missing discountLast tested: Nov 15, 2026Explicit caching requires: (1) create_cached_content first to store, (2) pass cached_content=cache.name in GenerateContentConfig, (3) same model as cache creation, (4) at least 4096 tokens in cache, (5) TTL still valid. Verify hit via usage.cached_content_token_count in response.
The Symptoms You're Seeing
# Symptom 1: Cache created but not used
response.usage_metadata.cached_content_token_count = 0
response.usage_metadata.prompt_token_count = 45000 # full price
# Symptom 2: Cache expired
{"error": {"code": 404, "message": "Cached content not found:
projects/*/locations/*/cachedContents/abc123",
"status": "NOT_FOUND"}}
# Symptom 3: Minimum tokens not met
{"error": {"code": 400, "message": "Cached content must have at
least 4096 tokens.", "status": "INVALID_ARGUMENT"}}
# Symptom 4: Model mismatch
{"error": {"code": 400, "message": "cached_content model
'gemini-2.5-flash' does not match request model
'gemini-2.5-pro'.", "status": "INVALID_ARGUMENT"}}Cache Rules Reference
| Rule | Value | Notes |
|---|---|---|
| Minimum tokens | 4,096 | Cache smaller and API rejects |
| Default TTL | 1 hour | Configurable at creation |
| Max TTL | Up to 1 year (Vertex) | AI Studio caps lower |
| Model binding | Locked at creation | Can't reuse cache across model versions |
| Content match | Exact prefix | Cache is prepended verbatim to new prompt |
| Cost savings | ~75% on cached tokens | Plus small storage fee per hour |
| Storage cost | Per token per hour | Small but adds up on long TTLs |
What Actually Causes This Error
Fixes That Work (Tested Nov 2026)
1Correct Explicit Cache Workflow
2TTL Management & Refresh
Storage costs vs discount: caching saves 75% on repeated input tokens but you pay per-token-per-hour storage. Break-even for a 50K token cache is usually 2-3 uses/hour. Below that, longer TTL costs more than the savings. Above it, extend TTL aggressively.
3Cache-Friendly Content Design
Preventing This Error Going Forward
- Verify hit via cached_content_token_count on every call in dev.
- Keep cached prefix stable — no timestamps, IDs, per-request data.
- Set TTL to match your access pattern. Analyze cache reuse rate.
- Only cache 4096+ token content. Below floor won't cache.
- Delete unused caches to stop storage billing.
- Match model exactly between cache creation and request.
- Log cache hit ratio; alert on drops. Indicates content drift.
Researcher · AI Error Hub
Frequently Asked Questions
For some models Google applies implicit prefix caching under the hood, but with no guarantees. For predictable savings use explicit caching (create_cached_content). Only explicit gives you the referenced cache name and reliable hit accounting.
On Vertex AI ~$1/M tokens/hour storage rate (varies by model tier), so 100K × 24 = $0.024. Trivial vs the ~75% discount on repeated calls. Only becomes painful for millions of tokens over long TTLs with few reuses.
Yes — tools count as part of cached content in most SDK versions. Especially useful for large tool arrays (agent frameworks). Confirm hit via cached_content_token_count; some SDK versions handle tools separately.
Vertex AI: caches are region-scoped. Cache created in us-central1 isn't accessible from europe-west3. Use consistent region per workload. AI Studio caches are global.
usage_metadata.cached_content_token_count reports total cached tokens hit. Combined with your explicit cache name reference, if the count is nonzero and matches your cache size, explicit hit worked. Implicit hits also populate this field.
Related Errors
Master Gemini Context Caching
Weekly deep dives on cache design, cost optimization, and multi-provider comparisons. 12,000+ developers subscribe.
Subscribe Free →