Creates a cached content resource that stores context (like documents, conversation history, or system instructions) for reuse across multiple requests. This reduces latency and costs for repeated queries against the same content.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/googleapis/python-genai/llms.txt
Use this file to discover all available pages before exploring further.
Method Signature
Parameters
The model to use for this cached content.Example:
'gemini-2.0-flash' or 'gemini-1.5-pro'Configuration for the cached content.Available options:
contents: The content to cache (documents, conversation history, etc.)system_instruction: System instructions to cachetools: Tools configuration to cachetool_config: Tool configuration settingsttl: Time-to-live as a duration string (e.g.,"3600s"for 1 hour)expire_time: Specific expiration timestampdisplay_name: Human-readable name for the cachekms_key_name: KMS encryption key (Vertex AI only)
Returns
A CachedContent object containing:
name: The resource name (e.g.,"cachedContents/abc123")model: The model namedisplay_name: Human-readable namecreate_time: When the cache was createdupdate_time: Last update timeexpire_time: When the cache will expireusage_metadata: Token usage information
Examples
Basic Cache Creation
Cache with Document
Cache with System Instructions
Use Cached Content in Generation
Cache Multiple Documents
Set Specific Expiration Time
Cache with Tools
Async Cache Creation
Cache Conversation History
Cost Optimization
Caching is cost-effective when:
- You make multiple requests with the same large context
- The cached content is reused more than once
- The context is large (thousands of tokens)
TTL vs Expire Time
You can specify expiration using either:
ttl: Duration from now (e.g.,"3600s"for 1 hour)expire_time: Specific timestamp
ttl for relative expiration, expire_time for absolute expiration. Don’t specify both.Error Handling
API Availability
This method is available in both Gemini API and Vertex AI.Vertex AI differences:
- Supports
kms_key_namefor encryption - Some tool options may differ
Related Methods
- caches.get - Retrieve cache metadata
- caches.update - Update cache expiration
- caches.delete - Delete a cache
- caches.list - List all caches