@@ -226,6 +226,7 @@ public actor ClaudeChatCompletionsService: ChatCompletionsStreamAPI, ChatComplet
226226 request. httpBody = try encoder. encode ( requestBody)
227227 request. setValue ( " application/json " , forHTTPHeaderField: " Content-Type " )
228228 request. setValue ( " 2023-06-01 " , forHTTPHeaderField: " anthropic-version " )
229+ request. setValue ( " prompt-caching-2024-07-31 " , forHTTPHeaderField: " anthropic-beta " )
229230 if !apiKey. isEmpty {
230231 request. setValue ( apiKey, forHTTPHeaderField: " x-api-key " )
231232 }
@@ -358,7 +359,7 @@ extension ClaudeChatCompletionsService.StreamDataChunk {
358359 } ( ) ,
359360 finishReason: delta? . stop_reason,
360361 usage: . init(
361- promptTokens: usage? . input_tokens ,
362+ promptTokens: usage? . input_tokens,
362363 completionTokens: usage? . output_tokens,
363364 cachedTokens: usage? . cache_read_input_tokens,
364365 otherUsage: {
@@ -376,6 +377,13 @@ extension ClaudeChatCompletionsService.StreamDataChunk {
376377extension ClaudeChatCompletionsService . RequestBody {
377378 init ( _ body: ChatCompletionsRequestBody ) {
378379 model = body. model
380+ let supportsPromptCache = if model. hasPrefix ( " claude-3-5-sonnet " ) || model
381+ . hasPrefix ( " claude-3-opus " ) || model. hasPrefix ( " claude-3-haiku " )
382+ {
383+ true
384+ } else {
385+ false
386+ }
379387
380388 var systemPrompts = [ SystemPrompt] ( )
381389 var nonSystemMessages = [ Message] ( )
@@ -409,7 +417,7 @@ extension ClaudeChatCompletionsService.RequestBody {
409417 switch message. role {
410418 case . system:
411419 systemPrompts. append ( . init( text: message. content, cache_control: {
412- if message. cacheIfPossible {
420+ if message. cacheIfPossible, supportsPromptCache {
413421 return . init( )
414422 } else {
415423 return nil
@@ -425,7 +433,7 @@ extension ClaudeChatCompletionsService.RequestBody {
425433 case . padMessageAndAppendToList, . joinMessage:
426434 nonSystemMessages [ nonSystemMessages. endIndex - 1 ] . content. append (
427435 . init( type: . text, text: message. content, cache_control: {
428- if message. cacheIfPossible {
436+ if message. cacheIfPossible, supportsPromptCache {
429437 return . init( )
430438 } else {
431439 return nil
@@ -443,7 +451,7 @@ extension ClaudeChatCompletionsService.RequestBody {
443451 case . padMessageAndAppendToList, . joinMessage:
444452 nonSystemMessages [ nonSystemMessages. endIndex - 1 ] . content. append (
445453 . init( type: . text, text: message. content, cache_control: {
446- if message. cacheIfPossible {
454+ if message. cacheIfPossible, supportsPromptCache {
447455 return . init( )
448456 } else {
449457 return nil
0 commit comments