---
source: https://iinventedlight.com/library/notes/ao-caching-gemini-context-caching.md
title: "Gemini context caching: caches.create, TTL and cached_content"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "Apache-2.0 (google-gemini/cookbook (GitHub)); Apache-2.0 (googleapis/python-genai (GitHub)); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: google-gemini/cookbook (GitHub), Context caching quickstart, Apache-2.0 (https://raw.githubusercontent.com/google-gemini/cookbook/main/LICENSE); googleapis/python-genai (GitHub), Google Gen AI Python SDK README, Apache-2.0 (https://raw.githubusercontent.com/googleapis/python-genai/main/LICENSE)."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-16
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-16
as_of: 2026-11-16
confidence: medium
untested: "Not reproduced (2026-11-16). Docs read; no API call was made for this note."
category: agent-operations
topic_area: "agent operations: prompt caching"
tags: [context-caching, gemini, google-genai, ttl]
client: Librarian
sources:
  - id: S1
    title: "Context caching quickstart"
    publisher: google-gemini/cookbook (GitHub)
    url: https://github.com/google-gemini/cookbook/blob/main/quickstarts/Caching.ipynb
    accessed: 2026-11-16
    license: "Apache-2.0"
  - id: S2
    title: "Google Gen AI Python SDK README"
    publisher: googleapis/python-genai (GitHub)
    url: https://github.com/googleapis/python-genai/blob/main/README.md
    accessed: 2026-11-16
    license: "Apache-2.0"
---
# Gemini context caching: caches.create, TTL and cached_content
**Answer:** Gemini context caching helps when you ask many questions of the same document, because it avoids passing the same tokens through the model for every new request.

## Steps (google-genai SDK)

1. Upload the file, for example with `client.files.upload`.
2. Create the cache: `client.caches.create(model=..., config={'contents': [...], 'system_instruction': ...})`.
3. Update the expiry with `client.caches.update` and `UpdateCachedContentConfig(ttl="7200s")`.
4. Pass `GenerateContentConfig(cached_content=cache.name)` to `client.models.generate_content()`.

## Gotchas

- Caches are model specific: you cannot use a cache made with a different model, as tokenization might be slightly different.
- Cached prompt tokens are included in `prompt_token_count` but excluded from `total_token_count`; the cache share is `cached_content_token_count`.
- Some models use automatic implicit caching and do not support explicit cache creation.

## See also

- [ao-caching-openai-automatic-prompt-caching](https://iinventedlight.com/library/notes/ao-caching-openai-automatic-prompt-caching/)
- [ao-caching-anthropic-cache-control](https://iinventedlight.com/library/notes/ao-caching-anthropic-cache-control/)
- [ao-tokens-gemini-count-tokens](https://iinventedlight.com/library/notes/ao-tokens-gemini-count-tokens/)

## Claims

- id: C1
  claim: Caches are model specific.
  source: S1
  evidence: "Note that caches are model specific."

- id: C2
  claim: TTL can be updated.
  source: S1
  evidence: "config=types.UpdateCachedContentConfig(ttl=\"7200s\")"

- id: C3
  claim: The SDK supports caches.
  source: S2
  evidence: "client.caches.create("

## Sources

- [S1] Context caching quickstart. google-gemini/cookbook (GitHub). https://github.com/google-gemini/cookbook/blob/main/quickstarts/Caching.ipynb . License: Apache-2.0. Accessed 2026-11-16.

- [S2] Google Gen AI Python SDK README. googleapis/python-genai (GitHub). https://github.com/googleapis/python-genai/blob/main/README.md . License: Apache-2.0. Accessed 2026-11-16.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: google-gemini/cookbook (GitHub), Context caching quickstart, Apache-2.0 (https://raw.githubusercontent.com/google-gemini/cookbook/main/LICENSE); googleapis/python-genai (GitHub), Google Gen AI Python SDK README, Apache-2.0 (https://raw.githubusercontent.com/googleapis/python-genai/main/LICENSE). Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
