@@ -81,7 +81,11 @@ export class AnthropicProvider implements Provider {
81
81
content = m . content . map ( ( c ) : Anthropic . ContentBlockParam => {
82
82
switch ( c . type ) {
83
83
case "text" :
84
- return c ;
84
+ // important to create a new object here so when we attach ephemeral
85
+ // cache_control markers we won't mutate the content.
86
+ return {
87
+ ...c ,
88
+ } ;
85
89
case "tool_use" :
86
90
return {
87
91
id : c . request . id ,
@@ -630,15 +634,13 @@ export function placeCacheBreakpoints(messages: MessageParam[]): number {
630
634
// however, since we are not accounting for tools or the system prompt, and generally code and technical writing
631
635
// tend to have a lower coefficient of string length to tokens (about 3.5 average sting length per token), this means
632
636
// that the first cache control should be past the 1024 mark and should be cached.
633
- const powers = highestPowersOfTwo ( tokens , 3 ) . filter ( ( n ) => n >= 1024 ) ;
634
- if ( powers . length ) {
635
- for ( const power of powers ) {
636
- const targetLength = power * STR_CHARS_PER_TOKEN ; // power is in tokens, but we want string chars instead
637
- // find the first block where we are past the target power
638
- const blockEntry = blocks . find ( ( b ) => b . acc > targetLength ) ;
639
- if ( blockEntry ) {
640
- blockEntry . block . cache_control = { type : "ephemeral" } ;
641
- }
637
+ const powers = highestPowersOfTwo ( tokens , 4 ) . filter ( ( n ) => n >= 1024 ) ;
638
+ for ( const power of powers ) {
639
+ const targetLength = power * STR_CHARS_PER_TOKEN ; // power is in tokens, but we want string chars instead
640
+ // find the first block where we are past the target power
641
+ const blockEntry = blocks . find ( ( b ) => b . acc > targetLength ) ;
642
+ if ( blockEntry ) {
643
+ blockEntry . block . cache_control = { type : "ephemeral" } ;
642
644
}
643
645
}
644
646
0 commit comments