Claude: set cache TTL to 1h

22afd570fcf659e40b66fb3803821edf2e550842

Cohee <18619528+Cohee1207@users.noreply.github.com>

2 files changed, +5 -4Ignore whitespace
src/endpoints/backends/chat-completions.js+4 -3
@@ -174,7 +174,7 @@ async function sendClaudeRequest(request, response) {
174 };174 };
175 if (useSystemPrompt) {175 if (useSystemPrompt) {
176 if (enableSystemPromptCache && Array.isArray(convertedPrompt.systemPrompt) && convertedPrompt.systemPrompt.length) {176 if (enableSystemPromptCache && Array.isArray(convertedPrompt.systemPrompt) && convertedPrompt.systemPrompt.length) {
177 convertedPrompt.systemPrompt[convertedPrompt.systemPrompt.length - 1]['cache_control'] = { type: 'ephemeral' };177 convertedPrompt.systemPrompt[convertedPrompt.systemPrompt.length - 1]['cache_control'] = { type: 'ephemeral', ttl: '1h' };
178 }178 }
179179
180 requestBody.system = convertedPrompt.systemPrompt;180 requestBody.system = convertedPrompt.systemPrompt;
@@ -190,7 +190,7 @@ async function sendClaudeRequest(request, response) {
190 .map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));190 .map(fn => ({ name: fn.name, description: fn.description, input_schema: fn.parameters }));
191191
192 if (enableSystemPromptCache && requestBody.tools.length) {192 if (enableSystemPromptCache && requestBody.tools.length) {
193 requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral' };193 requestBody.tools[requestBody.tools.length - 1]['cache_control'] = { type: 'ephemeral', ttl: '1h' };
194 }194 }
195 }195 }
196196
@@ -199,7 +199,7 @@ async function sendClaudeRequest(request, response) {
199 'type': 'web_search_20250305',199 'type': 'web_search_20250305',
200 'name': 'web_search',200 'name': 'web_search',
201 }];201 }];
202 requestBody.tools = [...(requestBody.tools || []), ...webSearchTool];202 requestBody.tools = [...webSearchTool, ...(requestBody.tools || [])];
203 }203 }
204204
205 if (cachingAtDepth !== -1) {205 if (cachingAtDepth !== -1) {
@@ -208,6 +208,7 @@ async function sendClaudeRequest(request, response) {
208208
209 if (enableSystemPromptCache || cachingAtDepth !== -1) {209 if (enableSystemPromptCache || cachingAtDepth !== -1) {
210 betaHeaders.push('prompt-caching-2024-07-31');210 betaHeaders.push('prompt-caching-2024-07-31');
211 betaHeaders.push('extended-cache-ttl-2025-04-11');
211 }212 }
212213
213 const reasoningEffort = request.body.reasoning_effort;214 const reasoningEffort = request.body.reasoning_effort;
src/prompt-converters.js+1 -1
@@ -869,7 +869,7 @@ export function cachingAtDepthForClaude(messages, cachingAtDepth) {
869 if (messages[i].role !== previousRoleName) {869 if (messages[i].role !== previousRoleName) {
870 if (depth === cachingAtDepth || depth === cachingAtDepth + 2) {870 if (depth === cachingAtDepth || depth === cachingAtDepth + 2) {
871 const content = messages[i].content;871 const content = messages[i].content;
872 content[content.length - 1].cache_control = { type: 'ephemeral' };872 content[content.length - 1].cache_control = { type: 'ephemeral', ttl: '1h' };
873 }873 }
874874
875 if (depth === cachingAtDepth + 2) {875 if (depth === cachingAtDepth + 2) {