explainx.ai0k
TrendingNewsPathwaysSkills
Pricing
explainx.ai

Upskill in AI — 16 free pathways, live workshops & bootcamps, and 50+ courses from practitioners. Plus the skills, tools, and MCP servers to practice on.

follow us

follow on google

Add explainx.ai as a preferred source

corporate training

support@explainx.ai

get started

Find your pathTake Free Evaluation

learn

mind: share how you thinkpathways — start freeworkshopsbootcampscoursescertificationsmock testsexplainx universitycorporate traininglearn skills & mcp

discover

skillsmcp serversexplainx mcptoolsagentsllmsdesignsdictionaryagi trackerranks

company

aboutvisionmissionteaminstructorsteach on explainxpartnershipscommunityhackathonscareers

content

daily AI newsstate of AI — live resultsblogreleasespromptsgeneratorsresource libraryfor LLMsexplainx.ai kids

solutions

all solutionsdeveloper upskillingmarketing upskillingproduct manager upskillingleadership upskilling

newsletter · weekly

Get AI news, tools, and insights in your inbox.

supportcontactprivacytermsdata rightshow we create contentsubmission guidelines

© 2026 AISOLO Technologies Pvt Ltd

On this page

  • TL;DR
  • The arithmetic that makes this necessary
  • The prefix-breaking trap
  • The measured results
  • The number Google didn't publish: what it costs
  • The four rules, as a decision tree
  • Porting this to other providers
  • Honest limitations
  • Related on explainx.ai
← Back to blog

explainx / blog

Context Caching in Agent Harnesses: Google's Numbers, and the Ones It Left Out

Context Engineering, Agent Harness, Token Economics, Google Cloud, Gemini

Google Cloud measured context caching across three agent harness topologies and cut transmitted tokens by up to 79%. The rules, and the real cost math.

Sep 3, 2026·10 min read·Yash Thakker
add explainx.ai
go deep
Context Caching in Agent Harnesses: Google's Numbers, and the Ones It Left Out

Google Cloud published a piece by Balaji Subramaniam, a DevRel Engineer on the team, on September 2, 2026, working through context caching for coding-agent harnesses on Google ADK 2.0 and the Gemini Enterprise Agent Platform. It crossed 38,400 views in a day, which is a lot for a post about prompt serialization.

It deserves the attention, because it does something most caching content does not: it publishes measured token counts across three distinct multi-agent topologies, run in Python 3.11 sandboxes on Cloud Run, rather than asserting that caching is good and moving on.

It also reports its headline as transmitted token reduction, which is not the same thing as your bill. Both numbers are worth having, so below is the mechanism, the four operating rules, Google's measurements, and the cost arithmetic the post leaves as an exercise.

A large static block of context held in a server-side cache while only a small dynamic suffix travels on each turn of an agent loop

TL;DR

table · 2 cols
QuestionAnswer
What's the problem?Models are stateless. The harness re-sends the whole static prefix every turn.
How bad?37,500-token prefix + 300-token traceback × 5 turns = ~190,000 prompt tokens. Ten turns ≈ 400,000.
What breaks caching?Any mutable value before or inside the static prefix. The match is byte-for-byte from token zero.
Measured saving74.6%–79.5% fewer transmitted tokens across three topologies.
Actual bill savingCloser to 56%–59% — cached reads still cost 0.25x and creation costs 1.0x.
Minimum prefix32,768 tokens. Below that, don't bother.
Minimum turnsThree. Two turns can't amortize the 1.0x creation charge.
The one design ruleDynamic variables go at the end of the prompt. Never at the beginning.

The arithmetic that makes this necessary

Large language models are stateless. They take an input sequence and return generated text; they remember nothing. The agent harness — the thing managing runtime state, sandbox execution, and prompt preparation — holds the conversational memory, and it does that by re-serializing the full context window on every model invocation.

Follow one typical coding loop. Static reference code is 37,500 tokens. Each turn appends a 300-token error traceback.

table · 3 cols
TurnTransmittedCumulative billed
137,80037,800
237,80075,600
337,800113,400
437,800151,200
537,800189,000

Five turns, 189,000 prompt tokens, of which roughly 187,500 are the same 37,500 tokens uploaded five times. A ten-turn refactor against a large repository approaches 400,000 prompt tokens. The network re-uploads identical files; the server re-tokenizes them.

This is the same accumulation curve we worked through from the Anthropic side in what actually costs you tokens in a Claude Code session and in context window pricing, decoded. Different vendor, identical physics.

Weekly digest3.5k readers

Catch up on AI

Curated AI updates on agents, skills, and MCP — delivered to your inbox. Unsubscribe anytime.

The prefix-breaking trap

Here is the part that turns a nice optimization into a silent failure mode, and the reason prefix invariance is a design constraint rather than a tuning tip.

Context caching requires an exact, byte-for-byte token match starting from token zero of the prompt. Not "mostly the same." Not "the same file contents." The same tokens, from the first one.

So if the harness places any mutable runtime metadata before or inside the static text — a timestamp, a session UUID, a turn counter, a "current time is..." line in the system preamble — the prompt hash changes. The server cannot match the request to the precomputed cache. It falls through to a cache miss, and the full prompt is charged at standard rates.

The post's rule is one sentence and it is the single most important line in it:

Dynamic variables belong at the end of the prompt, never at the beginning.

The failure is silent. Nothing errors. Your latency is a bit worse and your bill is unchanged, and unless you are watching cached-token counts specifically, you will conclude caching "didn't help much" when in fact it never engaged. If you are building your own harness rather than using someone else's — the territory covered in our minimal agent harness walkthrough and the context, prompt, loop and harness engineering stack — this is the first thing to get right and the easiest to get wrong.

The correct prompt order

snippet
1. Tool definitions            ← static, cache this
2. System prompt               ← static, cache this
3. Repository / spec context   ← static, cache this  (the 37k tokens)
──────────────── cache boundary ────────────────
4. Dynamic suffix              ← traceback, compiler error, this turn's ask

Google's reference implementation enforces this with two components: a CachePayloadBuilder that structurally isolates immutable prompt headers from dynamic execution suffixes so prefix corruption is impossible by construction, and a ContextCacheManager that owns the lifecycle — computing content hashes to reuse existing active caches, extending TTL when needed, and dispatching cached inference requests.

The naming matters less than the shape: one object that can only build a compliant payload, one object that owns cache lifetime. If your harness lets any code path concatenate a string into the prefix, you will eventually break it.

The measured results

Google ran three multi-agent topologies in Python 3.11 sandboxes on Cloud Run. All three used a static prefix in the 37k–39k token range.

table · 5 cols
ScenarioTurnsUncached inputCached transmittedTransmitted reduction
Multi-target batch modernization — five Python 2.7 modules against a 37,659-token monorepo prefix5190,13939,50379.46% (150,636 eliminated)
Adversarial SQLi red/blue debate — exploiter vs fixer agents against a 38,367-token OWASP spec4154,00838,90774.69% (115,101 eliminated)
Multi-file dependency graph refactor — four interdependent microservice layers against a 38,441-token ORM SDK4154,47939,15674.56% (115,323 eliminated)

Two things stand out.

The topology barely matters. A batch of independent tasks, an adversarial two-agent debate, and a cascading dependency refactor all land in the same 75–79% band. What determines the saving is the ratio of static prefix to dynamic suffix and the turn count — not how the agents are wired to each other.

The dynamic suffixes are tiny. In scenario 1, five turns of real agent work produced 1,844 tokens of new content against a 37,659-token prefix. In scenario 2 it was about 540 tokens. That ratio — roughly 1:20 to 1:70 — is the actual shape of iterative coding work, and it is why caching is not a marginal optimization here.

The number Google didn't publish: what it costs

The headline is transmitted reduction — bytes over the wire and server-side re-tokenization avoided. Real, but not your invoice.

Cached content reads are billed at a 75% discount, i.e. 0.25x the standard prompt rate, and cache creation is charged at 1.0x (the post says as much when it explains why sub-three-turn runs cannot amortize it). So the billed volume is:

snippet
creation (prefix × 1.0)
  + reads (prefix × 0.25 × remaining turns)
  + dynamic suffix (× 1.0)

Running that on Google's own figures:

table · 4 cols
ScenarioUncachedEffective billed (cached)Real cost reduction
Batch modernization (5 turns)190,139~77,200~59%
Red/blue debate (4 turns)154,008~67,700~56%
Dependency refactor (4 turns)154,479~68,000~56%

A 56–59% cut in input cost is an excellent result. It is just not 79%, and the gap is worth internalizing before you present a savings estimate to anyone who will hold you to it. The transmitted figure is the right metric for latency and bandwidth; this one is the right metric for the finance conversation — the same distinction we drew in token budget planning and execution.

Note also that the saving improves with turn count — the 1.0x creation charge is fixed while the 0.25x reads keep accruing against a 1.0x counterfactual. At 10 turns the same scenario 1 prefix lands closer to 70% off. Long loops are where this pays.

The four rules, as a decision tree

Google's operational guidance is refreshingly short. Applied in order:

table · 3 cols
ConditionDecisionWhy
Static context < 32,768 tokensDon't cacheTransmission cost is minor; cache management overhead isn't justified
Turn count < 3Don't cacheOne- and two-turn calls can't amortize the 1.0x creation cost
Prompt headers change every cycleDon't cache — refactor firstYou'll never get a hit. Isolate the static files, then revisit
Iterative test-and-repair against a large repoAlways cacheThis is the case the mechanism exists for

The third row is the one teams skip. "Refactor prompt construction to isolate static files before attempting to cache" is not a suggestion you can defer — an un-isolated prefix means caching is disabled in practice while appearing enabled in configuration.

Porting this to other providers

The mechanism is universal; the constants are not.

table · 3 cols
Google Cloud (as described)Anthropic
Cached read0.25x standard prompt rate~0.1x
Cache write1.0xup to 2x
Minimum useful prefix32,768 tokensMuch lower, model-dependent
Cache lifetimeManaged TTL, extendable5 min on API key, 1 hour on subscription
What breaks itAny change before/inside the prefixSame, plus model and effort switches

Two consequences of that table. First, Anthropic's cheaper reads and pricier writes push the break-even toward more turns with a smaller prefix, while Google's cheaper writes and pricier reads favor fewer turns with a bigger prefix. Second, the engineering discipline is identical either way: isolate the static prefix, append dynamics at the tail, own cache lifetime in one place. Build the harness that way and you can retarget providers without redoing the work. Our prompt caching cost-optimization guide covers the cross-provider version in more depth.

Honest limitations

  • These are vendor benchmarks. Google measured Google's platform on synthetic workloads it designed. The methodology is disclosed and reproducible, which is more than most, but it is not third-party.
  • Synthetic workloads are unusually favorable. Real coding sessions include file reads mid-loop, tool results of unpredictable size, and users changing their minds — all of which enlarge the dynamic suffix relative to these 540–1,844 token tails.
  • No dollars, no latency figures. Ratios and token counts only. Wall-clock improvement from skipping re-tokenization is not reported.
  • 32,768 is a rule of thumb, not a hard threshold, and it will move as pricing does.
  • Cache management is real operational surface. TTL extension, hash computation, eviction, and cache-miss observability are code you now own and have to monitor. Below the threshold, that overhead is the whole reason not to bother.
  • ADK 2.0 specifics may not survive contact with your stack. CachePayloadBuilder and ContextCacheManager are that implementation's names; the design pattern ports, the API does not.

Related on explainx.ai

  • What actually costs you tokens in a Claude Code session
  • Prompt caching and LLM cost optimization
  • Context window pricing, decoded
  • Context, prompt, loop — the harness engineering stack
  • Building a minimal agent harness
  • Token budget planning and execution
  • Context engineering vs prompt engineering
  • Loop engineering for coding agents

Token counts, discount rates, and threshold guidance reflect Google Cloud's post as published September 2, 2026. The cost-reduction figures in the "what it costs" section are our own arithmetic on Google's published token counts using the stated 0.25x read and 1.0x creation rates — they are estimates, not vendor figures. Pricing and thresholds change; verify against current Gemini Enterprise Agent Platform documentation before budgeting against them.

Spotted something out of date? Let us know.
Yash Thakker

Written by

Yash Thakker

Yash is an AI expert with over 300K learners. Join his workshops →

Related posts

Sep 3, 2026

Maximizing Claude Code Sessions: What Actually Costs You Tokens

Anthropic published a guide to running efficient Claude Code sessions, and the useful part is not the tip list — it is the mechanism underneath. Cache reads cost 0.1x input, output costs roughly 5x, and five specific actions throw the whole cached conversation away mid-session. Here is what that means for how you actually work.

Sep 2, 2026

Google AlphaEvolve: Gemini-Powered Evolutionary Code Optimization Agent

Google Cloud launched AlphaEvolve in September 2026 — a Gemini-powered evolutionary agent that takes base code and a client-side evaluator to evolve production-ready code. Here is how it works, why evaluators are mandatory, and how to configure client-side scoring.

Aug 29, 2026

How Uber Runs Coding Agents Cost-Effectively at Scale

Uber Engineering published "Running a Software Factory Efficiently at Uber Scale" on August 29, 2026. Agentic usage grew 7-9x in six months while total AI spend stayed flat since April. The reusable part is the cost equation: six multiplicative terms, benchmark-driven model selection, cheaper subagent defaults, prompt-cache TTL tuning, and killing MCP schema bloat with code-mode.