Claude: set cache TTL to 1h

This commit is contained in:
Cohee
2025-05-23 20:11:10 +03:00
parent 449522c3aa
commit 22afd570fc
2 changed files with 5 additions and 4 deletions

View File

@@ -174,7 +174,7 @@ async function sendClaudeRequest(request, response) {
};
if (useSystemPrompt) {
if (enableSystemPromptCache && Array.isArray(convertedPrompt.systemPrompt) && convertedPrompt.systemPrompt.length) {
convertedPrompt.systemPrompt[convertedPrompt.systemPrompt.length - 1]['cache_control'] = { type: 'ephemeral' };
convertedPrompt.systemPrompt[convertedPrompt.systemPrompt.length - 1]['cache_control'] = { type: 'ephemeral', ttl: '1h' };
}
requestBody.system = convertedPrompt.systemPrompt;
@@ -190,7 +190,7 @@ async function sendClaudeRequest(request, response) {
.map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));
if (enableSystemPromptCache && requestBody.tools.length) {
requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };
requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral', ttl: '1h' };
}
}
@@ -199,7 +199,7 @@ async function sendClaudeRequest(request, response) {
'type': 'web_search_20250305',
'name': 'web_search',
}];
requestBody.tools = [...(requestBody.tools || []), ...webSearchTool];
requestBody.tools = [...webSearchTool, ...(requestBody.tools || [])];
}
if (cachingAtDepth !== -1) {
@@ -208,6 +208,7 @@ async function sendClaudeRequest(request, response) {
if (enableSystemPromptCache || cachingAtDepth !== -1) {
betaHeaders.push('prompt-caching-2024-07-31');
betaHeaders.push('extended-cache-ttl-2025-04-11');
}
const reasoningEffort = request.body.reasoning_effort;

View File

@@ -869,7 +869,7 @@ export function cachingAtDepthForClaude(messages, cachingAtDepth) {
if (messages[i].role !== previousRoleName) {
if (depth === cachingAtDepth || depth === cachingAtDepth + 2) {
const content = messages[i].content;
content[content.length - 1].cache_control = { type: 'ephemeral' };
content[content.length - 1].cache_control = { type: 'ephemeral', ttl: '1h' };
}
if (depth === cachingAtDepth + 2) {