Prompt caching: stop paying full price for the context you send every time
Most agent and RAG prompts repeat the same long prefix on every call. Context caching on Google Cloud lets you pay for it once. How implicit and explicit caching work, and how to design prompts that hit the cache.
Why repeated context costs so much
Two ways to cache on Google Cloud
Implicit caching is on by default for Google Cloud projects. You change nothing in your code: when a request shares a long enough prefix with a recent one, the discount is applied automatically. There is no storage charge, but hits are not guaranteed, and cached prefixes are evicted within hours. Explicit caching is a cache you create through the API. You send the content once, set a time to live, and reference the cache by name in later requests. Hits are guaranteed while it lives, and you pay a storage charge for the time it is kept.
Design prompts that hit the cache
Stable content first: the system instruction, tool and function declarations, reference documents and few-shot examples. Variable content last: the user's question, retrieved passages that change per request, timestamps and IDs. Keep the prefix byte-for-byte identical. Don't inject the current date, a request ID or a reordered tool list into the system instruction. Version your prompts. Changing the stable prefix invalidates every cache built on the old version, so roll prompt changes out deliberately. Keep similar traffic together. Implicit caches are short-lived, so requests that share a prefix and arrive close together hit more often.
When explicit caching pays off
Measure it
cachedContentTokenCount in their usage metadata, next to the prompt and output token counts. Log those fields for every request, for example to BigQuery, and track the hit ratio (cached tokens divided by prompt tokens) per agent and per prompt version. A sudden drop usually means something variable has crept into the prefix.
response = client.models.generate_content(model=MODEL, contents=contents)
usage = response.usage_metadata
log_row = {
"agent": "support-agent",
"prompt_version": PROMPT_VERSION,
"prompt_tokens": usage.prompt_token_count,
"cached_tokens": usage.cached_content_token_count or 0,
"output_tokens": usage.candidates_token_count,
}Other models on the platform
Where to start
More articles
Tokenomics: the unit economics of AI agents
Token prices are the easy part. What decides whether an agent is affordable at enterprise scale is how many tokens each task really uses. A practical way to estimate, measure and reduce cost per task.