From b53aab5318836b04d1bafafc61b4b9a5e3f98b05 Mon Sep 17 00:00:00 2001 From: "zhaomingrui.solomon" Date: Wed, 20 Aug 2025 15:28:52 +0800 Subject: [PATCH 1/2] feat: support anthropic claude cache control config. issues 897 --- chat.go | 18 +++++++++++++----- common.go | 1 + 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/chat.go b/chat.go index 9719f6b92..34db9149f 100644 --- a/chat.go +++ b/chat.go @@ -88,10 +88,17 @@ const ( ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" ) +// CacheControlConfig for anthropic prompt cache control +type CacheControlConfig struct { + Type string `json:"type,omitempty"` + TTL string `json:"ttl,omitempty"` +} + type ChatMessagePart struct { - Type ChatMessagePartType `json:"type,omitempty"` - Text string `json:"text,omitempty"` - ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` + Type ChatMessagePartType `json:"type,omitempty"` + Text string `json:"text,omitempty"` + ImageURL *ChatMessageImageURL `json:"image_url,omitempty"` + CacheControl *CacheControlConfig `json:"cache_control,omitempty"` } type ChatCompletionMessage struct { @@ -344,8 +351,9 @@ const ( ) type Tool struct { - Type ToolType `json:"type"` - Function *FunctionDefinition `json:"function,omitempty"` + Type ToolType `json:"type"` + Function *FunctionDefinition `json:"function,omitempty"` + CacheControl *CacheControlConfig `json:"cache_control,omitempty"` } type ToolChoice struct { diff --git a/common.go b/common.go index d1936d656..267f33076 100644 --- a/common.go +++ b/common.go @@ -9,6 +9,7 @@ type Usage struct { TotalTokens int `json:"total_tokens"` PromptTokensDetails *PromptTokensDetails `json:"prompt_tokens_details"` CompletionTokensDetails *CompletionTokensDetails `json:"completion_tokens_details"` + CacheReadInputTokens int `json:"cache_read_input_tokens"` } // CompletionTokensDetails Breakdown of tokens used in a completion. From 11edd230e7405ea8f4ded54734cd4a22c8ece714 Mon Sep 17 00:00:00 2001 From: "zhaomingrui.solomon" Date: Wed, 20 Aug 2025 16:59:38 +0800 Subject: [PATCH 2/2] fix: sanity check --- chat.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chat.go b/chat.go index 34db9149f..8ec747889 100644 --- a/chat.go +++ b/chat.go @@ -88,7 +88,7 @@ const ( ChatMessagePartTypeImageURL ChatMessagePartType = "image_url" ) -// CacheControlConfig for anthropic prompt cache control +// CacheControlConfig for anthropic prompt cache control. type CacheControlConfig struct { Type string `json:"type,omitempty"` TTL string `json:"ttl,omitempty"`