| name | context-caching |
| description | Large context caching, cached token management, TTL config, and cost optimization. |
| allowed-tools | Read Write Edit Bash |
| license | MIT license |
| metadata | {"skill-author":"Lord1Egypt"} |
Gemini Context Caching Skill
Overview
This skill implements Gemini's native Context Caching API. Context Caching allows you to upload large, reusable pieces of data (e.g. video files, massive directories of text, or large PDFs) to Google's servers once, cache the parsed tokens, and query them multiple times.
Caching significantly reduces both query latency and per-request token costs when dealing with contents larger than 32,768 tokens (up to 2 million tokens).
When to Use This Skill
- Building interactive developer agents that need to inspect an entire codebase continuously.
- Q&A bots querying thick books, academic corpuses, or large financial directories.
- Repetitive analysis of long videos or extensive audio recordings.
- Cost reduction for high-frequency user interactions over a stable background dataset.
Quick Start (with runnable code examples)
import time
from google import genai
client = genai.Client()
def create_and_use_cache(document_path: str):
print("Uploading target file for context caching...")
doc_file = client.files.upload(file=document_path)
while doc_file.state.name == "PROCESSING":
time.sleep(2)
doc_file = client.files.get(name=doc_file.name)
print(f"Creating Gemini Context Cache for: {doc_file.name}...")
cache = client.caches.create(
model='gemini-2.5-flash',
config=dict(
contents=[doc_file],
ttl='300s',
display_name="large_document_reference_cache"
)
)
print(f"Cache created successfully! Cache Name: {cache.name}")
print(f"Expires at: {cache.expire_time}")
print("\nQuerying Gemini using the cache token bank...")
response1 = client.models.generate_content(
model='gemini-2.5-flash',
contents="Summarize Section 3 of the document.",
config=dict(
cached_content=cache.name
)
)
print("Response 1:", response1.text)
response2 = client.models.generate_content(
model='gemini-2.5-flash',
contents="Extract all dates mentioned in the text.",
config=dict(
cached_content=cache.name
)
)
print("\nResponse 2:", response2.text)
print("\nCleaning up cache...")
client.caches.delete(name=cache.name)
client.files.delete(name=doc_file.name)
if __name__ == "__main__":
with open("temp_large_doc.txt", "w") as f:
f.write("Section 1: Intro. Section 2: General Rules. Section 3: Analysis of results from 2026-06-04.")
create_and_use_cache("temp_large_doc.txt")
Advanced Usage
Monitoring Cache Resources & Extending TTL
You can inspect the cache size, verify token details, and update the time-to-live expiration dynamically:
my_cache = client.caches.get(name=cache.name)
print(f"Cached tokens count: {my_cache.metadata.total_token_count}")
client.caches.patch(
name=cache.name,
config=dict(
ttl='600s'
)
)
Key References
Dependencies