Skip to main content
Context caching allows you to save frequently used content (like large documents, files, or system instructions) and reuse them across multiple requests. This improves response times and reduces token usage for repeated content.

Benefits

  • Faster responses: Cached content doesn’t need to be reprocessed
  • Lower costs: You’re not charged for cached tokens in subsequent requests
  • Better performance: Ideal for long documents, knowledge bases, and system instructions

Creating Cached Content

Create a cache with content you want to reuse:

Cache Configuration

When creating a cache, you can specify:
  • contents: The content to cache (documents, files, text, etc.)
  • system_instruction: Optional system instructions included in the cache
  • display_name: A human-readable name for the cache
  • ttl: Time-to-live for the cache (e.g., ‘3600s’ for 1 hour)

Time-to-Live (TTL)

The TTL determines how long the cache remains available:
  • Format: String with time unit (e.g., ‘3600s’, ‘60m’, ‘1h’, ‘1d’)
  • Minimum: 60 seconds
  • Maximum: 7 days
  • After expiration: The cache is automatically deleted

Retrieving Cached Content

Get a cached content object by its name:

Using Cached Content

Reference the cached content in your generate content requests:
The model will use the cached content as context without reprocessing it.

Multiple Requests with Same Cache

You can reuse the same cached content across multiple requests:
Each request benefits from the cached content without reprocessing the PDFs.

Listing Caches

View all your cached content:

Updating Cache TTL

Extend the lifetime of a cache:

Deleting Cached Content

Manually delete a cache before it expires:

Best Practices

  • Cache large content: Only cache content that’s large enough to benefit from caching (typically > 10K tokens)
  • Set appropriate TTL: Balance between cache availability and cost
  • Reuse caches: Use the same cache across multiple requests to maximize benefits
  • Monitor expiration: Track cache expiration times and recreate as needed
  • Cache stable content: Best for content that doesn’t change frequently

Common Use Cases

Long Documents

Cache large documents for Q&A:

System Instructions

Cache system instructions for consistent behavior:

Knowledge Base

Cache multiple documents as a knowledge base:

Cost Optimization

Caching reduces costs significantly for repeated content:
  • First request: Normal token pricing for cached content
  • Subsequent requests: Discounted pricing for cached tokens
  • Threshold: Caching is cost-effective when content is reused 2+ times
Cache tokens are counted separately from prompt and output tokens. Cached content typically costs much less than regular prompt tokens.