@@ -941,14 +941,19 @@ function buildParams(
941941 options ?: AnthropicOptions ,
942942) : MessageCreateParamsStreaming {
943943 const { cacheControl } = getCacheControl ( model , options ?. cacheRetention ) ;
944- const systemCacheControlCount = ! cacheControl
945- ? 0
946- : isOAuthTokenResult
947- ? 1 + ( context . systemPrompt ? 1 : 0 )
948- : context . systemPrompt
949- ? 1
950- : 0 ;
951- const toolCacheControlCount = cacheControl && context . tools ?. length ? 1 : 0 ;
944+ const system = buildAnthropicSystemBlocks ( context . systemPrompt , isOAuthTokenResult , cacheControl ) ;
945+ const compat = context . tools ?. length ? getAnthropicCompat ( model ) : undefined ;
946+ const tools =
947+ context . tools && compat
948+ ? convertTools (
949+ context . tools ,
950+ isOAuthTokenResult ,
951+ compat . supportsEagerToolInputStreaming ,
952+ compat . supportsCacheControlOnTools ? cacheControl : undefined ,
953+ )
954+ : undefined ;
955+ const systemCacheControlCount = countNativeCacheControlMarkers ( system ) ;
956+ const toolCacheControlCount = countNativeCacheControlMarkers ( tools ) ;
952957 const messageCacheControlLimit = Math . max (
953958 0 ,
954959 ANTHROPIC_CACHE_CONTROL_LIMIT - systemCacheControlCount - toolCacheControlCount ,
@@ -966,20 +971,8 @@ function buildParams(
966971 stream : true ,
967972 } ;
968973
969- // For OAuth tokens, we MUST include Claude Code identity
970- if ( isOAuthTokenResult ) {
971- params . system = [
972- {
973- type : "text" ,
974- text : "You are Claude Code, Anthropic's official CLI for Claude." ,
975- ...( cacheControl ? { cache_control : cacheControl } : { } ) ,
976- } ,
977- ] ;
978- if ( context . systemPrompt ) {
979- params . system . push ( ...buildSystemPromptBlocks ( context . systemPrompt , cacheControl ) ) ;
980- }
981- } else if ( context . systemPrompt ) {
982- params . system = buildSystemPromptBlocks ( context . systemPrompt , cacheControl ) ;
974+ if ( system ) {
975+ params . system = system ;
983976 }
984977
985978 // Temperature is incompatible with extended thinking (adaptive or budget-based).
@@ -991,14 +984,8 @@ function buildParams(
991984 params . stop_sequences = options . stop ;
992985 }
993986
994- if ( context . tools && context . tools . length > 0 ) {
995- const compat = getAnthropicCompat ( model ) ;
996- params . tools = convertTools (
997- context . tools ,
998- isOAuthTokenResult ,
999- compat . supportsEagerToolInputStreaming ,
1000- compat . supportsCacheControlOnTools ? cacheControl : undefined ,
1001- ) ;
987+ if ( tools && tools . length > 0 ) {
988+ params . tools = tools ;
1002989 }
1003990
1004991 // Configure thinking mode: adaptive (Opus 4.6+ and Sonnet 4.6),
@@ -1256,6 +1243,33 @@ function convertMessages(
12561243 return params ;
12571244}
12581245
1246+ function applyContentBlockCacheControl (
1247+ block : ContentBlockParam ,
1248+ cacheControl : CacheControlEphemeral ,
1249+ ) : void {
1250+ ( block as ContentBlockParam & { cache_control ?: CacheControlEphemeral } ) . cache_control =
1251+ cacheControl ;
1252+ }
1253+
1254+ function buildAnthropicSystemBlocks (
1255+ systemPrompt : string | undefined ,
1256+ isOAuthTokenResult : boolean ,
1257+ cacheControl : CacheControlEphemeral | undefined ,
1258+ ) : TextBlockParam [ ] | undefined {
1259+ const blocks : TextBlockParam [ ] = [ ] ;
1260+ if ( isOAuthTokenResult ) {
1261+ blocks . push ( {
1262+ type : "text" ,
1263+ text : "You are Claude Code, Anthropic's official CLI for Claude." ,
1264+ ...( cacheControl ? { cache_control : cacheControl } : { } ) ,
1265+ } ) ;
1266+ }
1267+ if ( systemPrompt ) {
1268+ blocks . push ( ...buildSystemPromptBlocks ( systemPrompt , cacheControl ) ) ;
1269+ }
1270+ return blocks . length > 0 ? blocks : undefined ;
1271+ }
1272+
12591273function buildSystemPromptBlocks (
12601274 systemPrompt : string ,
12611275 cacheControl : CacheControlEphemeral | undefined ,
@@ -1291,12 +1305,18 @@ function buildSystemPromptBlocks(
12911305 return blocks . length > 0 ? blocks : [ { type : "text" , text : "" } ] ;
12921306}
12931307
1294- function applyContentBlockCacheControl (
1295- block : ContentBlockParam ,
1296- cacheControl : CacheControlEphemeral ,
1297- ) : void {
1298- ( block as ContentBlockParam & { cache_control ?: CacheControlEphemeral } ) . cache_control =
1299- cacheControl ;
1308+ function countNativeCacheControlMarkers ( blocks : unknown ) : number {
1309+ if ( ! Array . isArray ( blocks ) ) {
1310+ return 0 ;
1311+ }
1312+
1313+ let count = 0 ;
1314+ for ( const block of blocks ) {
1315+ if ( block && typeof block === "object" && "cache_control" in block ) {
1316+ count += 1 ;
1317+ }
1318+ }
1319+ return count ;
13001320}
13011321
13021322function shouldUseFineGrainedToolStreamingBeta (
0 commit comments