← All notes

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

See also

Claims

Sources

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

Permanent ID: iil:note:4t058pfs

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.},
}

Markdown

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.