提供商感知提示词缓存
Superdav AI Agent v1.12.0 引入了提供商感知提示词缓存(provider-aware prompt caching)功能。该功能通过跨不同 LLM 提供商缓存提示词,从而优化 API 成本和延迟。每个提供商都有不同的缓存机制和配置。
概述
提示词缓存允许您:
- 缓存大型、经常使用的提示词
- 通过避免重复处理来降低 API 成本
- 提高缓存请求的延迟性能
- 显式管理缓存的生命周期
不同的提供商实现缓存的方式不同:
- Google Gemini:
cachedContentsAPI - Azure OpenAI: 带 TTL 的提示词缓存
- OpenRouter: 提供商特定的缓存
- Vertex Anthropic: 带缓存控制的提示词缓存
Google Gemini: cachedContents API
Google Gemini 通过 cachedContents API 提供显式的缓存管理。
配置
$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
],
];
创建缓存提示词
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]
使用缓存提示词
$response = $gemini->generate(
[
'cache_id' => 'abc123',
'prompt' => 'User question here',
]
);
缓存生命周期
// 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' );
Gemini 的最佳实践
- 设置合适的 TTL: 平衡成本节约与缓 存过时程度
- 缓存系统提示词: 在多个请求中重复使用相同的系统提示词
- 监控缓存使用情况: 跟踪哪些缓存使用频率最高
- 清理过期的缓存: 定期删除未使用的缓存
Azure OpenAI: 提示词缓存
Azure OpenAI 支持带自动 TTL 管理的提示词缓存。
配置
$config = [
'provider' => 'azure-openai',
'model' => 'gpt-4-turbo',
'api_version' => '2024-08-01-preview',
'caching' => [
'enabled' => true,
'cache_control' => 'max_age=3600',
],
];
启用缓存
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,
// ]
缓存头
Azure OpenAI 使用 HTTP 头进行缓存控制:
Cache-Control: max_age=3600
支持的值:
max_age=<seconds>: 缓存指定持续时间no_cache: 不缓存此请求no_store: 不缓存且不重用
监控缓存使用情况
$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";
Azure OpenAI 的最佳实践
- 使用一致的提示词: 相同的提示词能从缓存中受益
- 设置合理的 TTL: 平衡成本与新鲜度
- 监控缓存指标: 跟踪缓存创建量与命中量
- 批量处理相似请求: 将请求分组以最大化缓存命中率
OpenRouter: 提供商特定缓存
OpenRouter 通过底层提供商(OpenAI、Anthropic 等)支持缓存。
配置
$config = [
'provider' => 'openrouter',
'model' => 'openai/gpt-4-turbo',
'caching' => [
'enabled' => true,
'provider_cache' => 'openai', // Use OpenAI's caching
],
];
使用 OpenRouter 缓存
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',
]
);
提供商特定选项
不同的提供商有不同的缓存机制:
// 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,
],
]
);
OpenRouter 的最佳实践
- 了解提供商的缓存机制: 每个提供商的机制都不同
- 测试缓存行为: 验证缓存是否能与您选择的提供商一起工作
- 监控成本: 跟踪缓存带来的节省
- 使用一致的模型: 切换模型会中断缓存命中
Vertex Anthropic: 带缓存控制的提示词缓存
Vertex Anthropic (Google Cloud) 支持带显式缓存控制的提示词缓存。
配置
$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,
],
],
];
使用 Vertex Anthropic 缓存
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,
// ],
// ]
缓存控制类型
- ephemeral: 仅在请求期间缓存(默认)
- persistent: 跨多个请求缓存(如果支持)
监控缓存使用情况
$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";
Vertex Anthropic 的最佳实践
- 使用临时缓存 (ephemeral): 适用于单次会话缓存
- 合理设置 max_tokens: 平衡缓存大小与成本
- 监控缓存指标: 跟踪缓存的有效性
- 使用您的工作负载进行测试: 验证缓存是否能为您的用例带来益处
跨提供商缓存策略
统一配置
$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,
],
],
],
],
];