context-caching
Large context caching, cached token management, TTL config, and cost optimization.
- License 许可
- MIT
- 最近更新
- 2026-06-05
- Stars
- ★ 2
- Forks
- 0
01 前言 Front Matter
- name
- context-caching
- license
- MIT license
- metadata
- { "skill-author": "Lord1Egypt" }
- description
- Large context caching, cached token management, TTL config, and cost optimization.
- allowed-tools
- Read Write Edit Bash
03 SKILL.md 正文
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
# Initialize the Gemini GenAI Client
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)
# Wait for file to become active
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}...")
# Create the cache
cache = client.caches.create(
model='gemini-2.5-flash',
config=dict(
# Pass the files or text contents to cache
contents=[doc_file],
# Time to live (TTL) before the cache expires automatically
ttl='300s', # 5 minutes
# Provide a descriptive name/label for management
display_name="large_document_reference_cache"
)
)
print(f"Cache created successfully! Cache Name: {cache.name}")
print(f"Expires at: {cache.expire_time}")
# Query using the cached content
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(
# Link the query to the cache resource name
cached_content=cache.name
)
)
print("Response 1:", response1.text)
# Run a second query (reuses the cache, saving time and tokens)
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)
# Cleanup: Delete the cache token bank
print("\nCleaning up cache...")
client.caches.delete(name=cache.name)
client.files.delete(name=doc_file.name)
if __name__ == "__main__":
# Create a dummy file for demonstration
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:
# Retrieve active cache information
my_cache = client.caches.get(name=cache.name)
print(f"Cached tokens count: {my_cache.metadata.total_token_count}")
# Extend cache lifetime by resetting the TTL
client.caches.patch(
name=cache.name,
config=dict(
ttl='600s' # Extend by another 10 minutes
)
)
Key References
- Gemini Context Caching Guide
- Google API Pricing for Cached Tokens
Dependencies
google-genai>=0.1.1
04 同仓库更多 Skills
zarr-python
Chunked N-D arrays for cloud storage. Compressed arrays, parallel I/O, S3/GCS integration, NumPy/Dask/Xarray compatible, for large-scale scientific computing pipelines.
what-if-oracle
Run structured What-If scenario analysis with multi-branch possibility exploration. Use this skill when the user asks speculative questions like "what if...", "what would happen if...", "what are the possibilities", "explore scenarios", "scenario analysis", "possibility space", "what could go wrong", "best case / worst case", "risk analysis", "contingency planning", "strategic options", or any question about uncertain futures. Also trigger when the user faces a fork-in-the-road decision, wants to stress-test an idea, or needs to think through consequences before committing.
venue-templates
Access comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
vaex
Use this skill for processing and analyzing large tabular datasets (billions of rows) that exceed available RAM. Vaex excels at out-of-core DataFrame operations, lazy evaluation, fast aggregations, efficient visualization of big data, and machine learning on large datasets. Apply when users need to work with large CSV/HDF5/Arrow/Parquet files, perform fast statistics on massive datasets, create visualizations of big data, or build ML pipelines that do not fit in memory.
usfiscaldata
Query the U.S. Treasury Fiscal Data API for federal financial data including national debt, government spending, revenue, interest rates, exchange rates, and savings bonds. Access 54 datasets and 182 data tables with no API key required. Use when working with U.S. federal fiscal data, national debt tracking (Debt to the Penny), Daily Treasury Statements, Monthly Treasury Statements, Treasury securities auctions, interest rates on Treasury securities, foreign exchange rates, savings bonds, or any U.S. government financial statistics.
treatment-plans
Generate concise (3-4 page), focused medical treatment plans in LaTeX/PDF format for all clinical specialties. Supports general medical treatment, rehabilitation therapy, mental health care, chronic disease management, perioperative care, and pain management. Includes SMART goal frameworks, evidence-based interventions with minimal text citations, regulatory compliance (HIPAA), and professional formatting. Prioritizes brevity and clinical actionability.