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)
- Upload the file, for example with
client.files.upload. - Create the cache:
client.caches.create(model=..., config={'contents': [...], 'system_instruction': ...}). - Update the expiry with
client.caches.updateandUpdateCachedContentConfig(ttl="7200s"). - Pass
GenerateContentConfig(cached_content=cache.name)toclient.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_countbut excluded fromtotal_token_count; the cache share iscached_content_token_count. - Some models use automatic implicit caching and do not support explicit cache creation.
See also
- ao-caching-openai-automatic-prompt-caching
- ao-caching-anthropic-cache-control
- 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.
License: CC-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).
Cite this
iinventedlight. 2026. "Gemini context caching: caches.create, TTL and cached_content." iinventedlight Library, iil:note:4t058pfs, version of 2026-11-16. https://iinventedlight.com/library/notes/ao-caching-gemini-context-caching/. License: CC-BY-4.0.
@misc{iil_note_4t058pfs,
author = {{iinventedlight}},
title = {{Gemini context caching: caches.create, TTL and cached\_content}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-16},
url = {https://iinventedlight.com/library/notes/ao-caching-gemini-context-caching/},
note = {Permanent ID iil:note:4t058pfs. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/ao-caching-gemini-context-caching.md.