← Blog

Gemini Cached Content Must Stay Bound to Its Creation Model

Parminder Singh
Parminder Singh··7 min read
Summarize with AI

Gemini explicit cache resources carry an immutable model field, and Google says cached content can be used only with the model it was created for. Production gateways should validate the request model against that stored binding before dispatch, keep cache identity separate from authorization, and retain the cache resource, model, expiration, policy, and response usage evidence in one audit chain.

Platform & Architectureai-securityllm-securitypolicy-enforcementforensic-auditarchitecture
Gemini Cached Content Must Stay Bound to Its Creation Model

A Gemini explicit cache is created for one model. Google's API makes the cache's model field required and immutable, then states that cached content can be used only with the model it was created for. A later generation request supplies the cache resource name through cachedContent while naming a model on the generation route.

That creates a pre-dispatch invariant: the request model must match the model bound to the cache resource. I want that comparison completed before provider dispatch, with the observed values recorded separately rather than hidden inside an SDK call.

TL;DR

  • Treat the model stored on Gemini CachedContent as an immutable binding.
  • Resolve cachedContent to an authorized cache record and require an exact normalized model match before dispatch.
  • Keep cache ownership, tenant policy, expiration, content provenance, and model compatibility as separate checks.
  • Record the requested model, cache resource, bound model, policy version, response ID, and cached-token evidence for every attempt.

Google binds cached content to one model

The Gemini caching API reference defines CachedContent as content preprocessed for later use by the generative service. Its contract is explicit: cached content can be used only with the model it was created for. The resource's required model field is immutable and uses the models/{model} format.

The same resource has an output-only name in the cachedContents/{id} format. Its cached contents are input-only and immutable. A developer-set system instruction is also input-only and immutable. Google allows expiration management separately, which means a longer lifetime does not change the model binding or rewrite the cached prompt.

The create cached content method repeats those field constraints at the HTTP request boundary. The creation record therefore needs to preserve the exact model resource name submitted, the returned cache name, the returned expiration time, and a digest of the immutable cache inputs. If an SDK removes the models/ prefix in one object while REST returns it in another, normalization belongs in a versioned adapter. The audit should retain both raw values.

The generation request supplies a cache reference

Google's GenerateContent method reference defines cachedContent as an optional string naming the cached context used for prediction. Its format is cachedContents/{cachedContent}. The generation call also selects the model through the model resource in the HTTP route.

A gateway therefore sees two pieces of model evidence. One comes from the requested generation route. The second sits on the referenced cache resource. Checking only that cachedContent has the right prefix verifies syntax and leaves the binding unresolved.

Build a cache registry when the application creates or imports an allowed resource. Store the raw cache name, bound model, tenant, creator identity, creation time, expiration, immutable-input digest, source API version, and last verification time. At generation time, resolve the reference under the authenticated tenant, then compare normalized model resource names. A missing registry entry should trigger a controlled lookup or a hold, according to route policy.

The LLM API parameter allowlisting guide applies at this point. cachedContent is a provider parameter with security and provenance consequences, so it belongs in the typed route contract rather than an unrestricted pass-through object.

Exact comparison avoids undocumented compatibility guesses

Google documents a model binding, not a family-compatibility rule. A cache created for one model should not be treated as compatible with another model because both names contain flash, share a version prefix, or appear adjacent in a catalog. Prefix matching invents semantics outside the API contract.

Normalize representation, then compare the complete resource names. Safe normalization can add the documented models/ prefix when a trusted client adapter omits it. It can also reject whitespace and malformed paths. It should never rewrite a version, translate an alias, or map one model family to another without an explicit provider contract.

Aliases require care. The request string is still the caller's routing instruction, while the cache stores its immutable creation model. If the two strings differ, hold the request unless a documented, tested adapter can establish identity for that API version. A permissive fallback would turn an exact binding into a guess.

My opinion is blunt: cache reuse is the wrong place for friendly model-name matching. The saved lookup is trivial compared with the incident review created by a cached policy prompt reaching an unintended model route.

Cache identity carries no authorization grant

A valid cache name and matching model establish provider compatibility. They do not prove that the authenticated caller may use the cached material. Cache authorization needs tenant ownership, route permission, data classification, intended purpose, and current lifecycle state.

Consider a cache containing a legal team's contract corpus. Another tenant obtains its resource name through a log leak. The generation request names the correct model, so model binding passes. Tenant authorization must still reject the reference before Gemini sees it. Resource identifiers are correlation handles, not bearer permissions.

Bind every approved cache to the application identity that created it or imported it, plus an allowed tenant and route set. Where users share a cache, record the policy object granting that access. Apply prompt-level classification to any uncached content appended to the request because the final effective prompt combines cached and new material.

A per-route AI policy can declare approved cache sources, allowed bound models, maximum age, data classes, and lookup behavior. The policy decision should emit a specific finding for a model mismatch or tenant denial. Expired and unknown resources need their own findings. One generic cache error erases the reason the request stopped.

Expiration and deletion need fresh lifecycle evidence

Google exposes expireTime on output and accepts a TTL on input. The caching reference says only expiration can be updated. A registry entry can therefore become stale while its immutable model and content digest remain unchanged.

Define a freshness window for lifecycle verification. A high-impact route can call the cache get method before use or require a recently verified registry record. A lower-impact route may accept a shorter local lease derived from the returned expiration time. In both cases, compare the current time against the provider-reported expiration and reject an entry already outside its allowed window.

Deletion creates another stale-reference case. A local registry can still display a cache after the provider resource has been removed. Preserve the provider's failed lookup or dispatch response as attempt evidence and mark the registry entry unavailable. Avoid silently creating a replacement cache under the same local label because the new resource has a new identity and may contain different bytes.

On the operator screen, print the requested model and bound cache model on adjacent lines. Put the cache resource beneath them in monospace and color a mismatch amber. That narrow three-line card makes the binding visible without forcing a responder to open the raw JSON.

Usage metadata confirms use after dispatch

Preflight proves that the request was allowed to reference a model-compatible cache. Response evidence answers a different question. Gemini's GenerateContent schema reports cachedContentTokenCount for the cached part of the prompt, while promptTokenCount remains the total effective prompt size and includes cached content.

Retain those fields with modelVersion, responseId, candidate outcome, and the application disposition. A zero or absent cached-token count deserves a named observation, but avoid turning it into proof of provider misconduct. API version, model support, request shape, and provider behavior all belong in the investigation.

The Gemini usage metadata validation guide explains the accounting rule. Never add cached tokens to the effective prompt total because they are already included. Model binding evidence belongs beside that usage object so an auditor can connect the approved resource to the provider's token report.

Retries need immutable child attempts. One business operation may first fail because a cache expired, create or select another approved cache, and then succeed. Keep both cache names and both decisions. The safe idempotent LLM retries guide provides the parent operation and attempt structure for that sequence.

Release fixtures should break the binding deliberately

Put a Gemini-compatible stub behind the deployed HTTP route and register a cache named cachedContents/cache-a bound to models/model-a. Send a request to models/model-a with that cache and confirm that dispatch occurs, the response is inspected, and the audit chain contains both raw model values.

Next, route the same cache to models/model-b. The gateway should stop before provider dispatch and emit cache_model_mismatch. Repeat with a missing models/ prefix supplied by a trusted SDK adapter to confirm that representation normalization works without changing the model identifier.

Add a malformed cache name, unknown registry entry, expired resource, deleted provider resource, and cache owned by another tenant. Use a valid matching model in every authorization fixture so the test proves that model compatibility never bypasses ownership policy.

Finish with stale registry data. Return a newer provider expiration on lookup, then return the same cache model with a changed immutable-input digest in a deliberately contradictory stub response. The gateway should preserve the contradiction, hold the request, and create a parser or registry-integrity finding. Destructive fixtures should fail visibly before production traffic depends on the cache.

DeepInspect

DeepInspect can inspect authenticated user or agent HTTP requests deliberately routed between an application and Gemini. At that boundary, it can resolve an allowed cache reference, compare the requested model with the cache's registered creation model, enforce tenant and route policy, and write the decision with response usage evidence into a per-decision audit record.

The application owns cache creation, registry population, cache sharing rules, lifecycle refresh, immutable-input digests, and any direct Gemini calls that bypass the proxy. DeepInspect evaluates the identity and cache context supplied at the proxied HTTP boundary.

Book a technical deep dive at deepinspect.ai.

Frequently asked questions

Can a Gemini cache be reused with another model in the same family?

Google says cached content can be used only with the model it was created for. The public API reference does not define family-level compatibility. Compare the complete normalized model resource names and hold a mismatch rather than inferring compatibility from a shared family label.

Is the CachedContent model field mutable?

No. Google marks the required model field immutable. The cache contents and system instruction are also documented as immutable. Expiration is the lifecycle field that the cache API allows clients to update.

Does a valid cachedContent name authorize access?

No. The name identifies the provider resource. The application still has to verify authenticated identity, tenant ownership, route permission, data classification, and lifecycle state before allowing that reference to leave the HTTP boundary.

Should the gateway call Gemini before every cache use?

That depends on route policy and the acceptable freshness window. A registry populated from trusted creation responses can support bounded local verification. Higher-impact routes can require a fresh provider lookup. Every path should retain the verification time and provider expiration.

What should happen after a cache model mismatch?

Stop the generation request before dispatch, preserve the requested model and cached model as separate raw fields, and emit a stable mismatch finding. Avoid retrying against a different model automatically because that changes the caller's requested route.

Does cachedContentTokenCount prove the cache was authorized?

No. It is provider-reported usage evidence for the cached part of the prompt. Authorization is an application policy decision made before dispatch. Retain both records so usage can be tied back to the identity-bound decision.