Skip to content

Commit f984063

Browse files
committed
Move note about which tools you have access to into instructions prompt
1 parent 78c79e9 commit f984063

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

packages/agent-runtime/src/__tests__/prompt-caching-subagents.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -472,16 +472,16 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
472472
parentSystemPrompt,
473473
)
474474

475-
// Verify there's a message about subagent tools
476-
const subagentToolsMessage = childMessages.find(
475+
// Verify there's an instructions prompt message that includes subagent tools info
476+
const instructionsMessage = childMessages.find(
477477
(msg) =>
478478
msg.role === 'user' &&
479479
msg.content[0].type === 'text' &&
480480
msg.content[0].text.includes('subagent') &&
481481
msg.content[0].text.includes('read_files') &&
482482
msg.content[0].text.includes('code_search'),
483483
)
484-
expect(subagentToolsMessage).toBeTruthy()
484+
expect(instructionsMessage).toBeTruthy()
485485
})
486486

487487
it('should support both inheritParentSystemPrompt and includeMessageHistory together', async () => {

packages/agent-runtime/src/run-agent-step.ts

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -594,12 +594,17 @@ export async function loopAgentSteps(
594594
agentState.runId = runId
595595

596596
let cachedAdditionalToolDefinitions: CustomToolDefinitions | undefined
597+
// Use parent's tools for prompt caching when inheritParentSystemPrompt is true
598+
const useParentTools =
599+
agentTemplate.inheritParentSystemPrompt && parentTools !== undefined
600+
597601
// Initialize message history with user prompt and instructions on first iteration
598602
const instructionsPrompt = await getAgentPrompt({
599603
...params,
600604
agentTemplate,
601605
promptType: { type: 'instructionsPrompt' },
602606
agentTemplates: localAgentTemplates,
607+
useParentTools,
603608
additionalToolDefinitions: async () => {
604609
if (!cachedAdditionalToolDefinitions) {
605610
cachedAdditionalToolDefinitions = await additionalToolDefinitions({
@@ -634,10 +639,6 @@ export async function loopAgentSteps(
634639
},
635640
})) ?? ''
636641

637-
// Use parent's tools for prompt caching when inheritParentSystemPrompt is true
638-
const useParentTools =
639-
agentTemplate.inheritParentSystemPrompt && parentTools !== undefined
640-
641642
const tools = useParentTools
642643
? parentTools
643644
: await getToolSet({
@@ -653,26 +654,13 @@ export async function loopAgentSteps(
653654
},
654655
})
655656

656-
// Build a message explaining the subagent's tool access when using parent's tools
657-
const subagentToolsMessage = useParentTools
658-
? `You are a subagent that only has access to the following tools: ${agentTemplate.toolNames.join(', ')}. Do not attempt to use any other tools.`
659-
: undefined
660-
661657
const hasUserMessage = Boolean(
662658
prompt || (spawnParams && Object.keys(spawnParams).length > 0),
663659
)
664660

665661
const initialMessages = buildArray<Message>(
666662
...agentState.messageHistory,
667663

668-
// Add subagent tools message before user prompt when using parent's tools for caching
669-
subagentToolsMessage &&
670-
userMessage({
671-
content: withSystemTags(subagentToolsMessage),
672-
tags: ['SUBAGENT_TOOLS'],
673-
keepDuringTruncation: true,
674-
}),
675-
676664
hasUserMessage && [
677665
{
678666
// Actual user message!

packages/agent-runtime/src/templates/strings.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ export async function getAgentPrompt<T extends StringField>(
158158
agentTemplates: Record<string, AgentTemplate>
159159
additionalToolDefinitions: () => Promise<CustomToolDefinitions>
160160
logger: Logger
161+
useParentTools?: boolean
161162
} & ParamsExcluding<
162163
typeof formatPrompt,
163164
'prompt' | 'tools' | 'spawnableAgents'
@@ -173,6 +174,7 @@ export async function getAgentPrompt<T extends StringField>(
173174
agentState,
174175
agentTemplates,
175176
additionalToolDefinitions,
177+
useParentTools,
176178
} = params
177179

178180
let promptValue = agentTemplate[promptType.type]
@@ -198,6 +200,12 @@ export async function getAgentPrompt<T extends StringField>(
198200

199201
// Add tool instructions, spawnable agents, and output schema prompts to instructionsPrompt
200202
if (promptType.type === 'instructionsPrompt' && agentState.agentType) {
203+
// Add subagent tools message when using parent's tools for prompt caching
204+
if (useParentTools) {
205+
addendum +=
206+
`\n\nYou are a subagent that only has access to the following tools: ${agentTemplate.toolNames.join(', ')}. Do not attempt to use any other tools.`
207+
}
208+
201209
addendum +=
202210
'\n\n' +
203211
(await buildSpawnableAgentsDescription({

0 commit comments

Comments
 (0)