AI & Agents

Gemini Context Caching: Token Limits, TTL, and Cost Architecture

Gemini context caching allows developers to reuse input tokens across requests to lower latency and API costs. Explicit context caching requires at least 32,768 tokens and defaults to a 1-hour time to live, balancing input discounts against hourly storage fees. For sporadic queries or large multi-file corpora, pairing Gemini models with persistent indexed workspaces eliminates holding fees while preserving retrieval precision.

Tom Langridge 18 min read Updated
Architectural diagram of Gemini context caching token limits and storage costs

What Is Gemini Context Caching and How Does the 32,768 Token Floor Work?

Developers turning to google gemini context caching to lower API expenses often find their first request rejected with a 400 Bad Request error: explicit context caching enforces a strict minimum threshold of 32,768 tokens. Gemini context caching is an API optimization that allows developers to cache frequently used input tokens (requiring a minimum of 32,768 tokens) to reduce latency and input processing costs by up to 75%. While developers familiar with Anthropic or OpenAI sometimes refer to this mechanism as gemini prompt caching, Google's implementation differs structurally by enforcing strict minimum token floors and an hourly holding cost architecture.

In large-scale AI pipelines, passing substantial context blocks repeatedly introduces computational latency and token costs. Whether feeding an entire code repository, financial filings, extensive system instructions, or video footage to an LLM, the model must process every prompt token before generating its first response token. Context caching changes this dynamic by persisting the precomputed attention states server-side.

During the prefill stage of inference, transformer models compute Key and Value (KV) vector projections for every token across their multi-head attention layers. In standard stateless API calls, this computation is repeated from scratch on every request, even when prompt content remains unchanged. Context caching saves these precomputed KV activation states in accelerator memory. On subsequent requests referencing the cache, the engine skips prefill for cached tokens and evaluates only the new query tokens.

Google divides context caching into two operational tiers:

  • Explicit Caching: The developer-controlled system supported across Gemini models. Developers make a dedicated API call to create a standalone cache object with a declared Time to Live (TTL). Explicit caching delivers guaranteed token discounts. However, it enforces two strict constraints: the payload must meet the gemini 32k cache limit (at least 32,768 tokens), and the developer pays an ongoing hourly storage fee for the entire duration of the TTL.
  • Implicit Caching: An automatic prefix-matching tier available on Gemini 2.5 and Gemini 3 models. When incoming requests share an identical prompt prefix exceeding lower minimum thresholds (2,048 tokens on Gemini 2.5; 4,096 tokens on Gemini 3), the serving engine routes the request to an existing KV cache at no extra storage fee. Context caching on Google Cloud is designed for scenarios where large initial context is referenced repeatedly across requests, supporting models with specific minimum token thresholds.

However, implicit caching is opportunistic. Cache entries can be evicted during cluster memory pressure without warning, falling back to standard input pricing. For mission-critical workflows requiring guaranteed latency reductions, explicit caching is required. Further background on endpoints is available in the Google Gemini API documentation.

The table below outlines operational parameters across Gemini model tiers:

Model Tier Caching Mode Minimum Token Limit Default TTL Cached Input Discount Hourly Storage Price
Gemini 3.8 Flash Implicit & Explicit 4096 tokens (implicit) / 32768 tokens (explicit) 1 hour (explicit) 75% to 90% $0.50 per 1M tokens
Gemini 3.1 Pro Preview Implicit & Explicit 4096 tokens (implicit) / 32768 tokens (explicit) 1 hour (explicit) 75% to 90% $4.50 per 1M tokens
Gemini 2.5 Flash Implicit & Explicit 2048 tokens (implicit) / 32768 tokens (explicit) 1 hour (explicit) 75% to 90% $1.00 per 1M tokens
Gemini 2.5 Pro Implicit & Explicit 2048 tokens (implicit) / 32768 tokens (explicit) 1 hour (explicit) 75% to 90% $4.50 per 1M tokens

Recognizing these architectural boundaries allows teams to select the proper caching mode for their workload profile.

How to Manage Time to Live and Explicit Cache Lifecycles

Explicit context caching requires developers to take active ownership of resource lifecycles. Every explicit cache is an ephemeral cloud resource identified by a unique resource path formatted as cachedContents/{cache_id}. Understanding gemini context cache limits and lifecycle controls is essential to avoid unexpected API errors and unmanaged storage costs.

The core control of an explicit cache is its Time to Live (TTL). When you create a cache without specifying a duration, the API applies a default TTL of 1 hour (3,600 seconds). The Google Cloud Gemini API and the Google GenAI SDK enforce a minimum allowable TTL of 1 minute (60 seconds). While there is no hard ceiling on maximum TTL duration, storage fees accrue continuously for every second a cache exists. If an application creates a cache with a 24-hour TTL, storage billing continues for the full 24 hours even if query volume drops to zero.

Managing an explicit cache involves four primary operations exposed by the Gemini API:

  1. Create (client.caches.create): Submits the token payload, verifies that content meets the 32,768 token threshold, precomputes KV tensors, and establishes the initial expiration timestamp.
  2. Get (client.caches.get): Inspects cache metadata, verifying active status, token counts, and remaining time before expiration.
  3. Update (client.caches.update): Dynamically extends the TTL or updates the expiration timestamp when ongoing agent interactions require the cache to stay alive.
  4. Delete (client.caches.delete): Immediately removes the cached KV representations from memory and halts storage charges.

The Python example below demonstrates creating an explicit cache with the official google-genai SDK, querying the model, reading usage metadata, extending TTL, and safely deleting the cache:

import os
from google import genai
from google.genai import types

### Initialize client using GEMINI_API_KEY environment variable
client = genai.Client()
model_id = "gemini-3.8-flash"

### Prepare reference corpus exceeding 32,768 tokens
clause = "Operational Directive: Maintain strict schema compliance."
large_corpus_text = "System Instruction: Technical Auditor. " + " ".join([clause] * 2500)

### Step 1: Create explicit cache with 30-minute TTL
cache = client.caches.create(
    model=model_id,
    config=types.CreateCachedContentConfig(
        display_name="audit_corpus_v1",
        contents=[large_corpus_text],
        ttl="1800s",
    )
)
print(f"Created cache: {cache.name}, Tokens: {cache.usage_metadata.total_token_count}")

### Step 2: Query model referencing cached content
response = client.models.generate_content(
    model=model_id,
    contents="Verify compliance directives across specification clauses.",
    config=types.GenerateContentConfig(cached_content=cache.name)
)

### Step 3: Inspect metadata to confirm cache hit
if response.usage_metadata:
    print(f"Cached tokens read: {response.usage_metadata.cached_content_token_count}")
    print(f"Candidates generated: {response.usage_metadata.candidates_token_count}")

### Step 4: Dynamically extend TTL by 15 minutes
updated_cache = client.caches.update(
    name=cache.name,
    config=types.UpdateCachedContentConfig(ttl="900s")
)
print(f"Updated expiration: {updated_cache.expire_time}")

### Step 5: Delete cache immediately upon completion to stop storage billing
client.caches.delete(name=cache.name)
print("Cache deleted. Storage charges stopped.")

For teams communicating via HTTP REST, cache creation uses the Gemini v1beta endpoint:

POST /v1beta/cachedContents?key=YOUR_API_KEY HTTP/1.1
Host: generativelanguage.googleapis.com
Content-Type: application/json

{
  "model": "models/gemini-3.8-flash",
  "displayName": "rest_audit_corpus",
  "ttl": "3600s",
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "Corpus text exceeding 32,768 tokens..." }]
    }
  ]
}

A successful response returns HTTP 200 with the cache resource representation and token counts. Payloads falling short of 32,768 tokens return an INVALID_ARGUMENT error status.

Comparing Hourly Storage Fees and Input Token Discounts

Most vendor documentation highlights input token discounts while downplaying continuous storage holding fees. Context caching transforms inference costs from a pure pay-per-token model into a hybrid system combining discounted per-call execution fees with fixed hourly holding expenses.

Evaluating whether explicit context caching saves money requires balancing two competing metrics:

  1. The input discount, which reduces token fees on every request that successfully hits the cache.
  2. The holding fee, which accrues continuously for stored tokens across the declared TTL regardless of request volume.

The pricing table below contrasts standard inference rates against cached storage fees across Gemini tiers:

Model Tier Standard Input (per 1M tokens) Cached Input (per 1M tokens) Input Discount Hourly Storage Fee (per 1M tokens) Daily Storage Fee (per 1M tokens)
Gemini 3.8 Flash $0.75 $0.075 90% $0.50 $12.00
Gemini 3.1 Pro Preview $2.00 $0.20 90% $4.50 $108.00
Gemini 2.5 Flash $0.30 $0.03 90% $1.00 $24.00
Gemini 2.5 Pro $1.25 $0.125 90% $4.50 $108.00

On Pro tiers, storage economics escalate rapidly. Maintaining 1 million tokens in active cache on Gemini 3.1 Pro Preview costs notable sums every 24 hours in passive holding fees alone.

This structure creates a mathematical break-even threshold. Explicit context caching only produces net savings when the total input discounts earned across all queries exceed the storage fees accrued over the TTL duration.

The formula for calculating the minimum break-even queries is:

Break-Even Queries = (Hourly Storage Rate * TTL in Hours) / (Standard Input Price per Token - Cached Input Price per Token)

The table below contrasts the financial outcomes of two real-world production scenarios:

Scenario Parameter Scenario A: Flash Batch Code Review Scenario B: Pro Incident Agent
Model Selected Gemini 3.8 Flash Gemini 3.1 Pro Preview
Cached Token Volume 100000 tokens 500000 tokens
Cache TTL Duration 1 hour 24 hours
Total Storage Holding Fee $0.05 $54.00
Input Discount Savings per Query $0.0675 $0.90
Break-Even Query Threshold 0.74 queries (profitable at 1 query) 60 queries
Actual Completed Queries 40 queries 6 queries
Net Financial Outcome $2.65 net savings $48.60 net loss

As shown above, concentrated high-frequency workloads quickly amortize storage expenses because input savings compound within a tight timeframe. Conversely, sporadic monitoring agents that query a large repository only a few times per day generate steep financial losses because ongoing storage costs dwarf intermittent query savings.

An additional cost driver is cache immutability. An explicit context cache cannot be modified in place. If an engineer updates a single paragraph in the prompt or alters a reference document, the cache is invalidated. The application must discard the old cache, provision a replacement, and restart storage charges. For dynamic corpora that change throughout the day, explicit context caching often leads to mounting holding fees for superseded data.

Fastio features

Coordinate Large Agent Corpora Without Hourly Cache Fees

Store and index massive document collections in shared workspaces with built-in RAG and MCP tooling. Monthly plans start with a 14-day free trial.

Why Large Corpora Exceed Context Window Limits

The economic limitations of context caching reflect a fundamental challenge across all foundation models: balancing context window capacity, file upload ceilings, and persistent storage.

Consider the documented file handling rules for Anthropic's Claude. In Claude chat, users can upload files subject to strict item counts and per-file size caps. In Claude Projects, individual files can reach specific size boundaries, and the project accepts an unlimited number of files. However, the total content must fit within Claude's context window. Claude Projects does not have a fixed file-count cap; the practical ceiling is the context window itself. Once a corporate knowledge base, codebase, or legal archive expands beyond that boundary, attaching files directly or caching them in active memory is no longer viable.

Gemini encounters an identical structural barrier. Although Gemini models support long context windows capable of ingesting massive token payloads, pinning a large enterprise repository into an explicit context cache is economically unsustainable. Holding millions of tokens in accelerator memory around the clock generates high passive storage charges every month.

The scalable architectural alternative is decoupling persistent document storage from ephemeral LLM inference memory using intelligent workspaces.

Fast.io workspaces provide a cloud workspace platform designed specifically for agentic teams and multi-model workflows. Instead of treating context as an expensive, ephemeral token blob pinned inside LLM memory, files reside in persistent, org-owned workspaces. Workspaces act as a shared substrate where human teammates and autonomous agents collaborate on the same files with granular permissions, per-file version history, and an append-only audit log.

Files enter a Fast.io workspace through direct browser upload, URL import, or scheduled Cloud Sync from existing cloud providers, including Google Drive, Dropbox, Box, and OneDrive. Once files land in a workspace, Intelligence Mode automatically indexes the content for hybrid search, combining full-text search, semantic vector search, and metadata value filtering without requiring a separate vector database or external embedding pipeline. For teams extracting structured information from documents, Fast.io document data extraction provides natural language schema generation and automated table extraction.

Agents running on Gemini, Claude, or open-source models connect to the workspace through the remote Fast.io Model Context Protocol (MCP) server. Interactive clients connect via Streamable HTTP at https://mcp.fast.io/mcp/tools, while terminal and coding agents connect at https://mcp.fast.io/mcp/code. Setup guidance is available in the Fast.io MCP documentation and Fast.io agent onboarding guide. When an agent needs to answer a prompt or inspect a file, it queries the workspace via MCP, retrieves only the relevant excerpts, and injects those precise excerpts into the prompt.

This retrieval-led pattern alters infrastructure economics:

Architectural Dimension Explicit Gemini Context Cache Fast.io Intelligent Workspaces
Storage Cost Basis Hourly memory holding fee ($0.50 to $4.50 per 1M tokens/hour) Flat monthly workspace plan ($9.99 Starter, $49.99 Business, $199.99 Enterprise)
Storage Allowances Billed per cached token 250 GB on Starter, 5 TB on Business, 25 TB on Enterprise
Overage Pricing Continuous hourly fee based on token volume 1.5 cents per GB per month for additional storage
Update Invalidation Complete cache invalidation; must rebuild cache and restart TTL Incremental auto-indexing on file change; full version history preserved
Multi-Model Access Locked to a single Gemini model on a single provider endpoint Model-neutral; accessible by Gemini, Claude, and local agents via MCP
Collaboration Ephemeral API token cache inaccessible to humans Shared org workspaces with Collaborative Notes and granular team permissions

Fast.io workspace storage comes included directly with subscription plans (Starter, Business, and Enterprise) as shown in the table above. Monthly plans start with a 14-day free trial that requires a credit card. Additional storage beyond plan limits is billed at a flat rate per gigabyte per month, completely decoupled from hourly per-token LLM memory fees. Storing a technical manual collection in a workspace costs predictable pennies per month, compared to substantial weekly sums in hourly LLM cache holding fees.

When a file is updated in a Fast.io workspace, the platform automatic indexing updates the search index incrementally. The per-file version history preserves previous iterations for auditability. An agent querying the workspace always retrieves the latest content without requiring developers to tear down, rebuild, or re-pay for an explicit LLM context cache. An explicit Gemini context cache is strictly locked to a single Gemini model on a single provider endpoint. A Fast.io workspace serves as model-neutral ground: a research agent using Gemini and an implementation agent using Claude Code can query, read, and update the exact same workspace files simultaneously through standardized MCP tools.

Fast.io intelligent workspace indexing architecture for multi-agent RAG retrieval

How to Architect Multi-Agent Workflows with Persistent Workspaces

Building resilient production systems requires matching each data access pattern to the appropriate infrastructure component. Rather than treating context caching and persistent workspaces as competing alternatives, high-performance agent architectures combine both approaches to optimize latency, cost, and reliability.

A practical evaluation framework for choosing between context caching and persistent workspace retrieval focuses on three operational variables: token volume, query frequency, and update velocity.

The Architectural Decision Matrix

  • Use Implicit Context Caching when requests share an identical prompt prefix, individual calls naturally exceed 2,048 or 4,096 tokens, traffic is continuous throughout the day, and you are using Gemini 2.5 or Gemini 3 models. Implicit caching provides automatic latency reductions with zero storage fees, making it well suited for high-volume customer-facing chatbots and multi-turn conversations. Further details are available in the Google Cloud Vertex AI documentation.
  • Use Explicit Context Caching when input payloads strictly exceed 32,768 tokens, the workload arrives in concentrated bursts (such as batch document processing or automated code audits running over a 15-to-60 minute window), prompt text remains completely static, and you require guaranteed input token cost reductions.
  • Use Persistent Workspaces (Fast.io via MCP) when the reference corpus is extensive, queries arrive intermittently, documents are edited frequently by human teammates or collaborating agents, or the workflow involves multiple different model providers. Explore options on the Fast.io AI storage page and Fast.io pricing page.

Hybrid Implementation Pattern

In multi-agent production pipelines, developers often implement a layered architecture:

  1. Foundational Protocol Caching: Core system instructions, detailed tool definitions, JSON schemas, and operational guardrails that remain strictly static across all requests are packaged into an explicit Gemini context cache. If this operational scaffold exceeds 32,768 tokens, it is given a task-aligned TTL (such as 30 minutes) during active agent runs.
  2. Dynamic Knowledge Retrieval: Domain documents, customer records, and dynamic reference files are stored in an intelligent Fast.io workspace. The agent uses MCP tools like storage or search to fetch only the relevant passages needed for the specific turn.
  3. Output Persistence and Audit Trails: When the agent finishes its reasoning task, it writes structured artifacts, logs, and summaries back to the workspace. Fast.io per-file version history keeps an auditable record of changes, while human teammates inspect and co-edit outputs using Collaborative Notes.

Production Troubleshooting and Best Practices

When operating Gemini context caching in production environments, monitor for these common operational pitfalls:

  • Handling 400 Bad Request Errors: The Gemini API immediately rejects cache creation requests containing fewer than 32,768 tokens with an INVALID_ARGUMENT response. If your prompt context falls below this floor, do not attempt to pad it with dummy characters. Instead, rely on implicit caching or pass the tokens as standard prompt content.
  • Handling 404 Not Found Cache Expirations: If an agent attempts to query a cache after its TTL has lapsed, the API returns a 404 error. Production applications must implement graceful fallback handling: wrap cache invocations in a try-except block that automatically regenerates the cache object or executes the query as a standard un-cached request if a 404 is received.
  • Data Isolation in Multi-Tenant Environments: Google Cloud isolates cached data by project number, ensuring that cache objects cannot be accessed across different Google Cloud projects. In agentic applications serving multiple end users, ensure that cache keys and contents are strictly segmented by tenant to prevent cross-user context leakage.
  • Explicit Teardown on Job Completion: Never allow batch processing caches to idle until their default 1-hour TTL expires. Include a teardown hook (client.caches.delete) in your application cleanup step to stop hourly storage billing the moment the batch completes.

Sources

References used to verify factual claims in this guide.

  1. Gemini context caching allows developers to cache frequently used input tokens to reduce latency and input processing costs by up to 75% for large contexts.

  2. Context caching on Google Cloud is designed for scenarios where large initial context is referenced repeatedly across requests, supporting models with specific minimum token thresholds.

Frequently Asked Questions

What is the minimum token limit for Gemini context caching?

Explicit context caching requires a minimum threshold of 32,768 tokens per cache. Requests below this threshold return a 400 Bad Request error. For newer models like Gemini 2.5 and Gemini 3, implicit caching operates with lower thresholds: 2,048 tokens for Gemini 2.5 and 4,096 tokens for Gemini 3 family models.

How much does Gemini context caching cost?

Context caching uses a two-part pricing structure: a discounted input rate for cached tokens and an hourly storage fee. On Gemini 3.8 Flash, cached input is discounted from standard rates with an hourly storage fee per million tokens. On Gemini 3.1 Pro Preview and Gemini 2.5 Pro, cached input offers significant per-token discounts balanced against an hourly memory holding fee per million tokens.

How long does a Gemini context cache last?

Explicit context caches have a default time to live (TTL) of 1 hour (3,600 seconds) if no duration is specified. Developers can set custom TTLs starting from a minimum of 1 minute up to several days. The cache automatically expires when the TTL elapses unless updated, or it can be deleted immediately using the API.

What causes a 400 Bad Request error when creating a Gemini context cache?

A 400 Bad Request error typically indicates that the provided content contains fewer tokens than the mandatory 32,768 threshold required for explicit caching. Other causes include referencing an unsupported model version, providing malformed multimodal payloads, or attempting to use explicit caching through the Interactions API.

When is on-demand retrieval more cost-effective than Gemini context caching?

On-demand retrieval from an indexed workspace is more cost-effective when queries arrive sporadically, corpora are large, or documents are updated frequently. Because explicit caching bills continuous hourly storage fees regardless of query traffic, low-frequency access patterns can cost more in holding fees than they save in input token discounts.

Related Resources

Fastio features

Coordinate Large Agent Corpora Without Hourly Cache Fees

Store and index massive document collections in shared workspaces with built-in RAG and MCP tooling. Monthly plans start with a 14-day free trial.