How Context Caching Works
Slashing Costs by 70%
When you cache a large prompt (like a codebase or a 1-hour video), Google processes the input and stores the Key-Value (KV) cache in memory.
Subsequent queries against that cached content skip the initial processing phase. This results in:
- Up to 70% lower input token costs.
- Near-instant time-to-first-token (TTFT).
from vertexai.preview import caching
# Cache a massive 1-hour video (minimum 32k tokens required)
cache = caching.CachedContent.create(
model_name="gemini-3.8-flash",
system_instruction="You are a video analyst.",
contents=[video_part],
ttl=datetime.timedelta(minutes=60)
)
Implicit Caching (Gemini 2.0+)
Implicit Caching is automatically enabled on Gemini 2.0 and later models. Unlike explicit Context Caching (which requires creating a cache object), implicit caching detects repeated prompt prefixes across requests and caches them transparently - with up to a ~90% discount on cached input tokens. No code changes are needed; simply ensure your requests share a common prefix (system instructions, large context) and implicit caching kicks in automatically.
Your team queries the same 500-page contract hundreds of times a day. Create a cachedContents resource over REST, then list active caches to confirm it exists.