Provider-Aware Prompt Caching
Superdav AI Agent v1.12.0 માં provider-aware prompt caching રજૂ કરવામાં આવ્યું છે, જે વિવિધ LLM પ્રોવાઇડર્સમાં prompts ને કેશ કરીને API ખર્ચ અને લેટન્સી (latency) ને ઓપ્ટિમાઇઝ કરે છે. દરેક પ્રોવાઇડરની કેશિંગ મિકેનિઝમ્સ અને કન્ફિગરેશન્સ અલગ હોય છે.
Overview
Prompt caching તમને નીચેની બાબતો કરવાની મંજૂરી આપે છે:
- મોટા, વારંવાર ઉપયોગમાં લેવાતા prompts ને કેશ કરવો
- બિનજરૂરી પ્રોસેસિંગ ટાળીને API ખર્ચ ઘટાડવો
- કેશ કરેલા requests માટે લેટન્સી સુધારવી
- કેશના જીવનચક્ર (cache lifecycle) ને સ્પષ્ટ રીતે મેનેજ કરવો
વિવિધ પ્રોવાઇડર્સ કેશિંગ અલગ રીતે અમલમાં મૂકે છે:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: TTL સાથે Prompt caching
- OpenRouter: પ્રોવાઇડર-વિશિષ્ટ કેશિંગ
- Vertex Anthropic: કેશ કંટ્રોલ સાથે Prompt caching
Google Gemini: cachedContents API
Google Gemini, 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: ખર્ચ બચત અને કેશ જૂની થવાની સ્થિતિ વચ્ચે સંતુલન જાળવો
- Cache system prompts: requests માં સમાન system prompt નો પુન:ઉપયોગ કરો
- Monitor cache usage: કયા કેશનો સૌથી વધુ ઉપયોગ થાય છે તે ટ્રેક કરો
- Clean up expired caches: નિયમિતપણે બિનઉપયોગી કેશને ડિલીટ કરો
Azure OpenAI: Prompt Caching
Azure OpenAI ઓટોમેટિક TTL મેનેજમેન્ટ સાથે prompt caching ને સપોર્ટ કરે છે.
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 કેશ કંટ્રોલ માટે HTTP headers નો ઉપયોગ કરે છે:
Cache-Control: max_age=3600
સપોર્ટેડ values:
max_age=<seconds>: નિર્ધારિત સમયગાળા માટે કેશ કરોno_cache: આ request ને કેશ ન કરોno_store: કેશ ન કરો અને ફરીથી ઉપયોગ ન કરો
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: સમાન prompts કેશિંગથી ફાયદો લે છે
- Set reasonable TTL: ખર્ચ અને તાજગી વચ્ચે સંતુલન જાળવો
- Monitor cache metrics: કેશ ક્રિએશન વિરુદ્ધ હિટ્સને ટ્રેક કરો
- Batch similar requests: કેશ હિટ્સને મહત્તમ કરવા માટે requests ને ગ્રુપ કરો
OpenRouter: Provider-Specific Caching
OpenRouter મૂળભૂત પ્રોવાઇડર્સ (OpenAI, Anthropic, વગેરે) દ્વારા કેશિંગને સપોર્ટ કરે છે.
Configuration
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];
Using OpenRouter Caching
use Superdav\AI\Providers\OpenRouter;
$router = new OpenRouter( $config );
$response = $router->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => 'max_age=3600',
]
);
Provider-Specific Options
વિવિધ પ્રોવાઇડર્સમાં કેશિંગ મિકેનિઝમ્સ અલગ હોય છે:
// OpenAI-compatible caching
$response = $router->generate(
[
'model' => 'openai/gpt-4-turbo',
'cache_control' => 'max_age=3600',
]
);
// Anthropic-compatible caching
$response = $router->generate(
[
'model' => 'anthropic/claude-3-opus',
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
]
);
Best Practices for OpenRouter
- Know your provider's caching: દરેક પ્રોવાઇડરના અલગ મિકેનિઝમ્સ હોય છે
- Test caching behavior: ખાતરી કરો કે કેશિંગ તમારા પસંદ કરેલા પ્રોવાઇડર સાથે કામ કરે છે
- Monitor costs: કેશિંગમાંથી થતી બચતને ટ્રેક કરો
- Use consistent models: મોડેલ બદલવાથી કેશ હિટ્સ તૂટી જાય છે
Vertex Anthropic: Prompt Caching with Cache Control
Vertex Anthropic (Google Cloud) સ્પષ્ટ કેશ કંટ્રોલ સાથે prompt caching ને સપોર્ટ કરે છે.
Configuration
$config = [
'provider' => 'vertex-anthropic',
'model' => 'claude-3-opus',
'project_id' => 'your-gcp-project',
'region' => 'us-central1',
'caching' => [
'enabled' => true,
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
],
];
Using Vertex Anthropic Caching
use Superdav\AI\Providers\VertexAnthropic;
$vertex = new VertexAnthropic( $config );
$response = $vertex->generate(
[
'system_prompt' => 'You are a helpful assistant...',
'context' => 'Large context document...',
'prompt' => 'User question here',
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
]
);
// Response includes cache metrics:
// [
// 'content' => '...',
// 'usage' => [
// 'input_tokens' => 1000,
// 'cache_creation_input_tokens' => 500,
// 'cache_read_input_tokens' => 300,
// ],
// ]
Cache Control Types
- ephemeral: request ના સમયગાળા માટે કેશ (default)
- persistent: બહુવિધ requests માં કેશ (જો સપોર્ટેડ હોય તો)
Monitoring Cache Usage
$response = $vertex->generate( [...] );
$usage = $response['usage'];
$cache_created = $usage['cache_creation_input_tokens'] ?? 0;
$cache_read = $usage['cache_read_input_tokens'] ?? 0;
echo "Cache created: $cache_created tokens\n";
echo "Cache read: $cache_read tokens\n";
Best Practices for Vertex Anthropic
- Use ephemeral caching: single-session caching માટે સારું છે
- Set max_tokens appropriately: કેશના કદ અને ખર્ચ વચ્ચે સંતુલન જાળવો
- Monitor cache metrics: કેશની અસરકારકતા ટ્રેક કરો
- Test with your workload: ખાતરી કરો કે કેશિંગ તમારા ઉપયોગના કિસ્સામાં ફાયદો કરે છે
Cross-Provider Caching Strategy
Unified Configuration
$config = [
'caching' => [
'enabled' => true,
'default_ttl' => 3600,
'providers' => [
'google-gemini' => [
'ttl' => 3600,
'max_tokens' => 1000000,
],
'azure-openai' => [
'cache_control' => 'max_age=3600',
],
'vertex-anthropic' => [
'cache_control' => [
'type' => 'ephemeral',
'max_tokens' => 1000000,
],
],
],
],
];
Provider Detection
$provider = $config['provider'];
$cache_config = $config['caching']['providers'][ $provider ]
?? $config['caching'];
// Use provider-specific caching configuration
Fallback Strategy
try {
// Try caching with primary provider
$response = $primary_provider->generate( $request );
} catch ( CacheException $e ) {
// Fall back to non-cached request
$response = $primary_provider->generate(
array_merge( $request, ['cache_control' => 'no_cache'] )
);
}
Cost Optimization
Calculate Savings
$cache_created_tokens = $response['cache_creation_input_tokens'] ?? 0;
$cache_read_tokens = $response['cache_read_input_tokens'] ?? 0;
$regular_tokens = $response['input_tokens'] ?? 0;
// Typical pricing (varies by provider):
$cache_creation_cost = $cache_created_tokens * 0.00001; // 10x cheaper
$cache_read_cost = $cache_read_tokens * 0.000001; // 100x cheaper
$regular_cost = $regular_tokens * 0.00001;
$total_cost = $cache_creation_cost + $cache_read_cost + $regular_cost;
$savings = ($regular_tokens * 0.00001) - $total_cost;
echo "Estimated savings: \$$savings\n";
Optimization Tips
- Cache large system prompts: સૌથી વધુ ખર્ચ બચત
- Reuse context: વારંવાર ઉપયોગમાં લેવાતા context documents ને કેશ કરો
- Batch requests: કેશ હિટ્સને મહત્તમ કરવા માટે requests ને ગ્રુપ કરો
- Monitor cache effectiveness: વાસ્તવિક બચતને ટ્રેક કરો
- Adjust TTL: ખર્ચ અને તાજગી વચ્ચે સંતુલન જાળવો
Troubleshooting
Cache not being used
- ખાતરી કરો કે કન્ફિગરેશનમાં કેશિંગ enable થયેલ છે
- તપાસો કે prompts સમાન છે (કેશિંગ માટે exact match જરૂરી છે)
- ખાતરી કરો કે ક ેશ સમાપ્ત (expired) નથી થયો
- પ્રોવાઇડર-વિશિષ્ટ કેશ મર્યાદાઓ તપાસો
Cache creation failing
- ખાતરી કરો કે કેશનું કદ પ્રોવાઇડરની મર્યાદામાં છે
- તપાસો કે cache control syntax સાચો છે
- ખાતરી કરો કે પ્રોવાઇડર તમારા મોડેલ માટે કેશિંગને સપોર્ટ કરે છે
- મર્યાદાઓ માટે પ્રોવાઇડર દસ્તાવેજીકરણની સમીક્ષા કરો
Unexpected costs
- કેશ ક્રિએશન વિરુદ્ધ કેશ રીડ ટોકન્સ પર નજર રાખો
- ખાતરી કરો કે કેશ ખરેખર ઉપયોગમાં લેવાઈ રહ્યું છે
- prompt માં ફેરફારને કારણે કેશ મિસ (cache misses) માટે તપાસ કરો
- TTL અથવા કેશ વ્યૂહરચનાને સમાયોજિત કરવાનું વિચારો
Provider Comparison
| Feature | Gemini | Azure OpenAI | OpenRouter | Vertex Anthropic |
|---|---|---|---|---|
| Cache API | cachedContents | HTTP headers | Provider-specific | Cache control |
| TTL control | Explicit | Via headers | Provider-dependent | Ephemeral/persistent |
| Max cache size | 1M tokens | Provider-dependent | Provider-dependent | 1M tokens |
| Cost reduction | 90% | 90% | Provider-dependent | 90% |
| Monitoring | Detailed | Via metrics | Provider-dependent | Via usage |
Next Steps
- Choose your provider: તમારી જરૂરિયાતોના આધારે પસંદ કરો
- Configure caching: પ્રોવાઇડર-વિશિષ્ટ કેશિંગ સેટ કરો
- Test caching: ખાતરી કરો કે તે તમારા prompts સાથે કામ કરે છે
- Monitor usage: કેશ હિટ્સ અને ખર્ચ બચતને ટ્રેક કરો
- Optimize: પરિણામોના આધારે TTL અને કેશ વ્યૂહરચનાને એડજસ્ટ કરો