@@ -16,7 +16,8 @@ import { IAutomodeService } from '../../../platform/endpoint/node/automodeServic
1616import { IEnvService } from '../../../platform/env/common/envService' ;
1717import { ILogService } from '../../../platform/log/common/logService' ;
1818import { IEditLogService } from '../../../platform/multiFileEdit/common/editLogService' ;
19- import { CUSTOM_TOOL_SEARCH_NAME , isAnthropicCustomToolSearchEnabled } from '../../../platform/networking/common/anthropic' ;
19+ import { CUSTOM_TOOL_SEARCH_NAME , isAnthropicCustomToolSearchEnabled , isAnthropicToolSearchEnabled } from '../../../platform/networking/common/anthropic' ;
20+ import { IToolDeferralService } from '../../../platform/networking/common/toolDeferralService' ;
2021import { IChatEndpoint } from '../../../platform/networking/common/networking' ;
2122import { modelsWithoutResponsesContextManagement } from '../../../platform/networking/common/openai' ;
2223import { INotebookService } from '../../../platform/notebook/common/notebookService' ;
@@ -363,6 +364,7 @@ export class AgentIntentInvocation extends EditCodeIntentInvocation implements I
363364 @IExperimentationService private readonly expService : IExperimentationService ,
364365 @IAutomodeService private readonly automodeService : IAutomodeService ,
365366 @IOTelService override readonly otelService : IOTelService ,
367+ @IToolDeferralService private readonly toolDeferralService : IToolDeferralService ,
366368 ) {
367369 super ( intent , location , endpoint , request , intentOptions , instantiationService , codeMapperService , envService , promptPathRepresentationService , endpointProvider , workspaceService , toolsService , configurationService , editLogService , commandService , telemetryService , notebookService , otelService ) ;
368370 }
@@ -388,7 +390,15 @@ export class AgentIntentInvocation extends EditCodeIntentInvocation implements I
388390 }
389391
390392 const tools = promptContext . tools ?. availableTools ;
391- const toolTokens = tools ?. length ? await this . endpoint . acquireTokenizer ( ) . countToolTokens ( tools ) : 0 ;
393+ // When Anthropic tool search is enabled, deferred tools are sent with
394+ // defer_loading: true and don't count against the context window until
395+ // the model loads them via tool_search. Only count non-deferred tools
396+ // so the budget isn't artificially reduced.
397+ const toolSearchEnabled = isAnthropicToolSearchEnabled ( this . endpoint , this . configurationService ) ;
398+ const effectiveTools = tools && toolSearchEnabled
399+ ? tools . filter ( t => this . toolDeferralService . isNonDeferredTool ( t . name ) )
400+ : tools ;
401+ const toolTokens = effectiveTools ?. length ? await this . endpoint . acquireTokenizer ( ) . countToolTokens ( effectiveTools ) : 0 ;
392402
393403 const summarizeThresholdOverride = this . configurationService . getConfig < number | undefined > ( ConfigKey . Advanced . SummarizeAgentConversationHistoryThreshold ) ;
394404 if ( typeof summarizeThresholdOverride === 'number' && summarizeThresholdOverride < 100 && summarizeThresholdOverride > 0 ) {
@@ -414,7 +424,7 @@ export class AgentIntentInvocation extends EditCodeIntentInvocation implements I
414424 const safeBudget = useTruncation ? Number . MAX_SAFE_INTEGER : messageBudget ;
415425 const endpoint = toolTokens > 0 ? this . endpoint . cloneWithTokenOverride ( safeBudget ) : this . endpoint ;
416426
417- this . logService . debug ( `AgentIntent: rendering with budget=${ safeBudget } (baseBudget: ${ baseBudget } , toolTokens: ${ toolTokens } ), summarizationEnabled=${ summarizationEnabled } ` ) ;
427+ this . logService . debug ( `AgentIntent: rendering with budget=${ safeBudget } (baseBudget: ${ baseBudget } , toolTokens: ${ toolTokens } ${ toolSearchEnabled ? `, totalTools: ${ tools ?. length ?? 0 } , nonDeferredTools: ${ effectiveTools ?. length ?? 0 } ` : '' } ), summarizationEnabled=${ summarizationEnabled } ` ) ;
418428 let result : RenderPromptResult ;
419429 const props : AgentPromptProps = {
420430 endpoint,
0 commit comments