Skip to main content

How Context Caching Works — Context Caching

[ ABORT TO HUD ]
SEQ. 1
SEQ. 2
SEQ. 3

How Context Caching Works

⚡ Context Caching 15m 300 BASE XP⌨ HANDS-ON LAB

Slashing Costs by Up to 90%–95%

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 90% off cached input tokens on Gemini 3.8 Flash ($0.075/MTok) and 95% off on Gemini 4 Argon ($0.10/MTok promo, $0.20/MTok standard).
  • Near-instant time-to-first-token (TTFT).
from google import genai
from google.genai import types

client = genai.Client(vertexai=True, project="my-project", location="us-central1")

# Explicit cache on Gemini 3.8 Flash (minimum 4,096 tokens required)
cache = client.caches.create(
    model="gemini-3.8-flash",
    config=types.CreateCachedContentConfig(
        system_instruction="You are a video analyst.",
        contents=[video_part],
        ttl="3600s"
    )
)

Implicit vs Explicit Caching (Gemini 3.x & Gemini 4 Argon)

Implicit Caching is enabled by default across Gemini 3.x and Gemini 4 Argon with zero hourly storage fees (persisting up to 24 hours). Shared prompt prefixes meeting the minimum threshold (4,096–6,144 tokens; plus ≥2,050 new tokens per turn in Argon multi-turn loops) automatically receive a 90%–95% input token discount. Critical API Rule: gemini-4-argon and the Interactions API support implicit caching only — explicit cachedContents objects (with TTL-governed storage billing) are supported specifically on Gemini 3.x models such as gemini-3.8-flash and gemini-3.6-flash.

⌨ HANDS-ON LABCreate & List a Context Cache
⭐ +150 XP

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.

1Capture a fresh access token in the TOKEN shell variable.
2POST to the cachedContents endpoint to create a cache with a model, contents, and a ttl.
3GET the same cachedContents endpoint to list your active caches.
lab-sandbox — simulated environment
INFINITY LAB SANDBOX v2.6 — simulated shell
Type the command for the current objective. Helpers: "hint", "solution", "clear".
$
OBJECTIVE 1 / 3 — type "hint" if stuck
SYNAPSE VERIFICATION
QUERY 1 // 2
What is the primary benefit of Context Caching in Vertex AI?
It trains the model on your data
It significantly reduces latency and cost when repeatedly querying massive prompts
It generates better images
It automatically translates the prompt into 50 languages