Context Cache API Reference
ContextCache Traitβ
The ContextCache trait provides a provider-agnostic interface for context caching.
Methodsβ
create_cacheβ
async fn create_cache(&self, content: &str, ttl: Duration) -> Result<CacheHandle, CacheError>
Creates a new cached content resource.
Parameters:
content: The content to cache (prompt, system message, etc.)ttl: Time-to-live duration for the cache
Returns:
Ok(CacheHandle): Cache handle for future operationsErr(CacheError): Error if cache creation fails
Note: For Claude and OpenAI, this is a no-op or minimal operation since caching is handled automatically.
get_cacheβ
async fn get_cache(&self, handle: &CacheHandle) -> Result<Option<CachedContext>, CacheError>
Retrieves cache metadata by handle.
Parameters:
handle: The cache handle to look up
Returns:
Ok(Some(CachedContext)): Cache metadata if foundOk(None): Cache not foundErr(CacheError): Error retrieving cache
Note: For Claude and OpenAI, this returns ProviderNotSupported since they don't expose cache metadata.
refresh_cacheβ
async fn refresh_cache(&self, handle: &CacheHandle) -> Result<(), CacheError>
Extends cache TTL before expiration.
Parameters:
handle: The cache handle to refresh
Returns:
Ok(()): Refresh successfulErr(CacheError): Error refreshing cache
delete_cacheβ
async fn delete_cache(&self, handle: &CacheHandle) -> Result<(), CacheError>
Explicitly removes cached content.
Parameters:
handle: The cache handle to delete
Returns:
Ok(()): Deletion successfulErr(CacheError): Error deleting cache
Typesβ
CacheHandleβ
Provider-specific cache identifier:
pub enum CacheHandle {
Claude { cache_control: serde_json::Value },
OpenAI,
Gemini { cache_name: String },
}
CachedContextβ
Cache metadata including creation time, expiration, and token count:
pub struct CachedContext {
pub handle: CacheHandle,
pub created_at: SystemTime,
pub expires_at: SystemTime,
pub token_count: u32,
}
CacheErrorβ
Error types for cache operations:
pub enum CacheError {
CacheCreationFailed { provider: String, reason: String },
CacheExpired { handle: CacheHandle },
CacheNotFound { handle: CacheHandle },
InvalidCacheConfiguration { reason: String },
ProviderNotSupported { provider: String },
NetworkError { message: String },
ParseError { message: String },
}
Provider Implementationsβ
ClaudeContextCacheβ
Minimal implementation - caching is handled via cache_control blocks in requests.
OpenAIContextCacheβ
Minimal implementation - caching is automatic, no explicit control.
GeminiContextCacheβ
Full implementation with cachedContent API support:
let cache = GeminiContextCache::new(api_key, Some(base_url));
let handle = cache.create_cache("Large context...", Duration::from_secs(3600)).await?;