Provider-Aware Prompt Caching
Superdav AI Agent v1.12.0 introduces provider-aware prompt caching, which optimizes API costs and latency by caching prompts across different LLM providers. Each provider has different caching mechanisms and configurations.
Overview
Prompt caching allows you to:
- Cache large, frequently-used prompts
- Reduce API costs by avoiding redundant processing
- Improve latency for cached requests
- Manage cache lifecycle explicitly
Different providers implement caching differently:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: Prompt caching with TTL
- OpenRouter: Provider-specific caching
- Vertex Anthropic: Prompt caching with cache control
Google Gemini: cachedContents API
Google Gemini provides explicit cache management via the cachedContents API.
Configuration
$config = [
'provider' => 'google-gemini',
'model' => 'gemini-2.0-flash',
'caching' => [
'enabled' => true,
'ttl' => 3600, // 1 hour in seconds
'max_tokens' => 1000000, // Max tokens to cache
],
];
Creating a Cached Prompt
use Superdav\AI\Providers\GoogleGemini;
$gemini = new GoogleGemini( $config );
$cached_content = $gemini->create_cached_content(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'ttl' => 3600,
]
);
// Returns: ['cache_id' => 'abc123', 'expires_at' => timestamp]
Using a Cached Prompt
$response = $gemini->generate(
[
'cache_id' => 'abc123',
'prompt' => 'User question here',
]
);
Cache Lifecycle
// List cached contents
$caches = $gemini->list_cached_contents();
// Get cache details
$cache = $gemini->get_cached_content( 'abc123' );
// Extend cache TTL
$gemini->update_cached_content(
'abc123',
['ttl' => 7200] // Extend to 2 hours
);
// Delete cache
$gemini->delete_cached_content( 'abc123' );
Best Practices for Gemini
- Set appropriate TTL: Balance cost savings vs. cache staleness
- Cache system prompts: Reuse the same system prompt across requests
- Monitor cache usage: Track which caches are used most
- Clean up expired caches: Periodically delete unused caches
Azure OpenAI: Prompt Caching
Azure OpenAI supports prompt caching with automatic TTL management.
Configuration
$config = [
'provider' => 'azure-openai',
'model' => 'gpt-4-turbo',
'api_version' => '2024-08-01-preview',
'caching' => [
'enabled' => true,
'cache_control' => 'max_age=3600',
],
];
Enabling Caching
use Superdav\AI\Providers\AzureOpenAI;
$azure = new AzureOpenAI( $config );
$response = $azure->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => 'max_age=3600',
]
);
// Response includes cache usage:
// [
// 'content' => '...',
// 'cache_creation_input_tokens' => 1000,
// 'cache_read_input_tokens' => 500,
// ]
Cache Headers
Azure OpenAI uses HTTP headers for cache control:
Cache-Control: max_age=3600
Supported values:
max_age=<seconds>: Cache for specified durationno_cache: Don't cache this requestno_store: Don't cache and don't reuse
Monitoring Cache Usage
$response = $azure->generate( [...] );
$cache_tokens = $response['cache_creation_input_tokens'] ?? 0;
$cache_hits = $response['cache_read_input_tokens'] ?? 0;
echo "Cache creation: $cache_tokens tokens\n";
echo "Cache hits: $cache_hits tokens\n";
Best Practices for Azure OpenAI
- Use consistent prompts: Identical prompts benefit from caching
- Set reasonable TTL: Balance cost vs. freshness
- Monitor cache metrics: Track cache creation vs. hits
- Batch similar requests: Group requests to maximize cache hits
OpenRouter: Provider-Specific Caching
OpenRouter supports caching through underlying providers (OpenAI, Anthropic, etc.).
Configuration
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];