Skip to content

Commit ec7caf7

Browse files
authored
Subagent prompt caching (+ work on orchestrator agent) (#330)
1 parent 90c1b90 commit ec7caf7

38 files changed

+931
-340
lines changed

.agents/editor/editor.ts

Lines changed: 9 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { Message } from 'types/util-types'
22
import { publisher } from '../constants'
3-
import {
4-
PLACEHOLDER,
5-
type SecretAgentDefinition,
6-
} from '../types/secret-agent-definition'
3+
import { type SecretAgentDefinition } from '../types/secret-agent-definition'
74

85
const editor: SecretAgentDefinition = {
96
id: 'editor',
@@ -28,7 +25,6 @@ const editor: SecretAgentDefinition = {
2825
},
2926
},
3027
outputMode: 'structured_output',
31-
includeMessageHistory: true,
3228
toolNames: [
3329
'read_files',
3430
'write_file',
@@ -42,7 +38,10 @@ const editor: SecretAgentDefinition = {
4238
],
4339
spawnableAgents: ['file-explorer', 'web-researcher', 'docs-researcher'],
4440

45-
systemPrompt: `You are an expert code editor with deep understanding of software engineering principles.
41+
includeMessageHistory: true,
42+
inheritParentSystemPrompt: true,
43+
44+
instructionsPrompt: `You are an expert code editor with deep understanding of software engineering principles.
4645
4746
# Core Mandates
4847
@@ -66,62 +65,18 @@ const editor: SecretAgentDefinition = {
6665
- Remove unused variables, functions, and files as a result of your changes.
6766
- If you added files or functions meant to replace existing code, then you should also remove the previous code.
6867
- **Edit multiple files at once:** When you edit files, you must make as many tool calls as possible in a single message. This is faster and much more efficient than making all the tool calls in separate messages. It saves users thousands of dollars in credits if you do this!
69-
<example>
70-
Assistant: I will now implement feature X.
71-
72-
<codebuff_tool_call>
73-
{
74-
"toolName": "str_replace",
75-
"input": {
76-
"filePath": "src/components/Button.tsx",
77-
"oldContent": "...",
78-
"newContent": "...",
79-
}
80-
}
81-
</codebuff_tool_call>
82-
83-
<codebuff_tool_call>
84-
{
85-
"toolName": "str_replace",
86-
"input": {
87-
"filePath": "src/components/Button.tsx",
88-
"oldContent": "...",
89-
"newContent": "...",
90-
}
91-
}
92-
</codebuff_tool_call>
93-
94-
// ... 8 more str_replace tool calls ...
95-
96-
Let's see what the code looks like now.
97-
98-
User: <tool_result>
99-
<tool>str_replace</tool>
100-
<result>...</result>
101-
</tool_result>
102-
103-
<tool_result>
104-
<tool>str_replace</tool>
105-
<result>...</result>
106-
</tool_result>
107-
108-
// ... 8 more tool_result blocks ...
109-
</example>
11068
- **Summarize with set_output:** You must use the set_output tool before finishing and include a clear explanation of the changes made or an answer to the user prompt. Do not write a separate summary outside of the set_output tool.
111-
112-
${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
113-
114-
instructionsPrompt: `Implement the requested changes, using your judgment as needed, but referring to the original <user-message> as the most important source of information.
69+
Implement the requested changes, using your judgment as needed, but referring to the original <user-message> as the most important source of information.
11570
11671
# Instructions
11772
118-
- It's helpful to spawn a file explorer to discover all the relevant files for implementing the plan. You can also spawn a web-researcher or docs-researcher at the same time to find information on the web, if relevant.
119-
- You must read all relevant files to understand the current state. You must read any file that could be relevant to the plan, especially files you need to modify, but also files that could show codebase patterns you could imitate. Try to read a lot of files in a single tool call. E.g. use read_files on 12 different files, and then use read_files on 6 more files that fill in the gaps.
73+
- Read any relevant files that have not already been read. Or, spawn a file-explorer to find any other relevant parts of the codebase.
12074
- Implement changes using str_replace or write_file.
75+
- Verify your changes by running tests, typechecking, etc. Keep going until you are sure the changes are correct.
12176
- You must use the set_output tool before finishing and include the following in your summary:
12277
- An answer to the user prompt (if they asked a question).
12378
- An explanation of the changes made.
124-
- A note on any checks you ran to verify the changes, such as tests, typechecking, etc.
79+
- A note on any checks you ran to verify the changes, such as tests, typechecking, etc., and the results of those checks.
12580
- Do not include a section on the benefits of the changes, as we're most interested in the changes themselves and what still needs to be done.
12681
- Do not write a summary outside of the one that you include in the set_output tool.
12782
- As soon as you use set_output, you must end your turn using the end_turn tool.

.agents/orchestrator/orchestrator.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@ const definition: SecretAgentDefinition = {
2828
},
2929
outputMode: 'last_message',
3030
includeMessageHistory: true,
31-
toolNames: ['spawn_agents', 'read_files', 'str_replace', 'write_file'],
31+
toolNames: ['spawn_agents', 'read_files'],
3232
spawnableAgents: [
3333
'read-only-commander',
3434
'researcher-file-explorer',
3535
'researcher-web',
3636
'researcher-docs',
3737
'decomposing-planner',
3838
'editor',
39-
'reviewer-lite',
39+
'reviewer-max',
4040
'context-pruner',
4141
],
4242

@@ -55,33 +55,38 @@ const definition: SecretAgentDefinition = {
5555
- **Proactiveness:** Fulfill the user's request thoroughly, including reasonable, directly implied follow-up actions.
5656
- **Confirm Ambiguity/Expansion:** Do not take significant actions beyond the clear scope of the request without confirming with the user. If asked *how* to do something, explain first, don't just do it.
5757
58+
${PLACEHOLDER.FILE_TREE_PROMPT_SMALL}
59+
${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}
60+
5861
# Starting Git Changes
5962
6063
The following is the state of the git repository at the start of the conversation. Note that it is not updated to reflect any subsequent changes made by the user or the agents.
6164
6265
${PLACEHOLDER.GIT_CHANGES_PROMPT}
6366
`,
6467

65-
instructionsPrompt: `Orchestrate the completion of the coding task using your specialized sub-agents.
68+
instructionsPrompt: `Orchestrate the completion of the user's request using your specialized sub-agents.
6669
67-
## Simple workflow
70+
## Example workflow
6871
6972
Use this workflow to solve a medium or complex coding task:
7073
1. Spawn a researcher
71-
2. Spawn a decomposing planner to come up with a plan.
72-
3. Spawn an editor to implement the plan.
73-
4. Spawn a reviewer to review the code. If changes are needed, go back to step 3, but only once.
74-
5. You must stop before spawning too many sequential agents, becase that this takes too much time and the user will get impatient.
74+
2. Read all the relevant files using the read_files tool.
75+
3. Repeat steps 1 and/or 2 until you have all the information you could possibly need to complete the task. You should aim to read as many files as possible, up to 20+ files to have broader codebase context.
76+
4. Spawn a decomposing planner to come up with a plan.
77+
5. Spawn an editor to implement the plan. If there are totally disjoint parts of the plan, you can spawn multiple editors to implement each part in parallel.
78+
6. Spawn a reviewer to review the code. If changes are needed, go back to step 5, but no more than once.
79+
7. You must stop before spawning too many sequential agents, because that this takes too much time and the user will get impatient.
7580
7681
Feel free to modify this workflow as needed. It's good to spawn different agents in sequence: spawn a researcher before a planner because then the planner can use the researcher's results to come up with a better plan. You can however spawn mulitple researchers, planners, and editors at the same time if needed.
7782
7883
## Guidelines
7984
80-
- You can spawn agents to help you complete the task. Iterate by spawning more agents as needed.
85+
- Spawn agents to help you complete the task. Iterate by spawning more agents as needed.
8186
- Don't mastermind the task. Rely on your agents' judgement to research, plan, edit, and review the code.
82-
- Give as many instructions upfront as possible to each agent so you're less likely to need to spawn them again.
8387
- You should feel free to stop and ask the user for guidance if you're stuck or don't know what to try next, or need a clarification.
84-
- When prompting an agent, realize that many agents can already see the entire conversation history, so you can be brief in prompting them without needing to include much context.
88+
- Give as many instructions upfront as possible to each agent so you're less likely to need to spawn them again.
89+
- When prompting an agent, realize that many agents can already see the entire conversation history, so you can be brief in prompting them without needing to include context.
8590
- Be careful about instructing subagents to run terminal commands that could be destructive or have effects that are hard to undo (e.g. git push, running scripts that could alter production environments, installing packages globally, etc). Don't do any of these unless the user explicitly asks you to.
8691
`,
8792

.agents/planners/decomposing-planner-lite.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const definition: SecretAgentDefinition = {
99
spawnerPrompt:
1010
'Creates a better implementation plan by decomposing the task into smaller plans in parallel and synthesizing them into a final plan. Includes full code changes.',
1111
spawnableAgents: ['researcher-file-explorer', 'implementation-planner-lite'],
12-
includeMessageHistory: false,
1312
}
1413

1514
export default definition

.agents/planners/decomposing-planner.ts

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
11
import { publisher } from '../constants'
2-
import {
3-
PLACEHOLDER,
4-
type SecretAgentDefinition,
5-
} from '../types/secret-agent-definition'
2+
import { type SecretAgentDefinition } from '../types/secret-agent-definition'
63

74
const definition: SecretAgentDefinition = {
85
id: 'decomposing-planner',
96
publisher,
107
model: 'anthropic/claude-sonnet-4.5',
11-
displayName: 'Decomposing Planner',
8+
displayName: 'Peter Plan',
129
spawnerPrompt:
1310
'Creates the best possible implementation plan by decomposing the task into smaller plans in parallel and synthesizing them into a final plan. Includes full code changes.',
1411
inputSchema: {
@@ -19,31 +16,27 @@ const definition: SecretAgentDefinition = {
1916
},
2017
},
2118
outputMode: 'last_message',
22-
includeMessageHistory: true,
23-
toolNames: ['spawn_agents', 'read_files'],
24-
spawnableAgents: ['file-explorer', 'implementation-planner'],
19+
toolNames: ['spawn_agents'],
20+
spawnableAgents: ['implementation-planner'],
2521

26-
systemPrompt: `You are an expert programmer, architect, and problem solver who excels at breaking down complex tasks.
22+
includeMessageHistory: true,
23+
inheritParentSystemPrompt: true,
2724

28-
${PLACEHOLDER.FILE_TREE_PROMPT}
29-
${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
25+
instructionsPrompt: `You are an expert programmer, architect, and problem solver who excels at breaking down complex tasks.
3026
31-
instructionsPrompt: `Instructions:
27+
Instructions:
3228
33-
Step 1: Task Decomposition
34-
- Spawn a file-explorer agent to explore the codebase and read all the relevant files
29+
Step 1: Task Decomposition & Parallel Planning
3530
- Carefully analyze the user's request
36-
- Break it down into 3-5 focused subtasks that:
31+
- Break it down into 2-10 focused subtasks that:
3732
- Cover different aspects of the implementation (e.g., data layer, business logic, UI, testing)
3833
- Are specific and actionable
3934
- Together address the complete requirements
40-
41-
Step 2: Parallel Planning
42-
- Spawn 3-5 implementation-planner agents in parallel (one spawn_agents call with multiple agents)
35+
- Spawn 2-10 implementation-planner agents in parallel (one spawn_agents call with multiple agents)
4336
- Give each agent a focused subtask from your decomposition
4437
- Each subtask prompt should be specific about what that agent should focus on
4538
46-
Step 3: Synthesis
39+
Step 2: Synthesis
4740
- Review all the plans from the spawned agents
4841
- Create a unified implementation plan that:
4942
- Combines insights from all subtask plans

.agents/planners/implementation-planner.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
import { publisher } from '../constants'
2-
import {
3-
PLACEHOLDER,
4-
type SecretAgentDefinition,
5-
} from '../types/secret-agent-definition'
2+
import { type SecretAgentDefinition } from '../types/secret-agent-definition'
63

74
const definition: SecretAgentDefinition = {
85
id: 'implementation-planner',
@@ -20,25 +17,18 @@ const definition: SecretAgentDefinition = {
2017
},
2118
outputMode: 'last_message',
2219
includeMessageHistory: true,
20+
inheritParentSystemPrompt: true,
2321
toolNames: ['spawn_agents', 'read_files'],
24-
spawnableAgents: [
25-
'file-explorer',
26-
'web-researcher',
27-
'docs-researcher',
28-
],
22+
spawnableAgents: ['file-explorer', 'web-researcher', 'docs-researcher'],
2923

30-
systemPrompt: `You are an expert programmer, architect, researcher, and general problem solver.
24+
instructionsPrompt: `You are an expert programmer, architect, researcher, and general problem solver.
3125
You spawn agents to help you gather information, and then describe a full change to the codebase that will accomplish the task.
3226
3327
You do not have access to tools to modify files (e.g. the write_file or str_replace tools). You are describing all the code changes that should be made as a full implementation.
3428
35-
${PLACEHOLDER.FILE_TREE_PROMPT}
36-
${PLACEHOLDER.KNOWLEDGE_FILES_CONTENTS}`,
37-
38-
instructionsPrompt: `Instructions:
29+
Instructions:
3930
- Spawn file-explorer twice to find all the relevant parts of the codebase. Use different prompts for each file-explorer to ensure you get all the relevant parts of the codebase. In parallel as part of the same spawn_agents tool call, you may also spawn a web-researcher or docs-researcher to search the web or technical documentation for relevant information.
40-
- Read all the file paths that are relevant using the read_files tool.
41-
- Read more and more files to get any information that could possibly help you make the best plan. It's good to read 20+ files.
31+
- Read any relevant files that have not already been read.
4232
- Think about the best way to accomplish the task.
4333
- Finally, describe the full change to the codebase that will accomplish the task (or other steps, e.g. terminal commands to run). Use markdown code blocks to describe the changes for each file.
4434

.agents/reviewer/reviewer-factory.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import { AGENT_PERSONAS } from '@codebuff/common/constants/agents'
2-
import { closeXml } from '@codebuff/common/util/xml'
3-
42
import type { SecretAgentDefinition } from '../types/secret-agent-definition'
53
import type { Model } from '@codebuff/common/old-constants'
64

@@ -15,11 +13,11 @@ export const reviewer = (model: Model): Omit<SecretAgentDefinition, 'id'> => ({
1513
},
1614
},
1715
outputMode: 'last_message',
18-
includeMessageHistory: true,
19-
toolNames: ['end_turn', 'run_file_change_hooks'],
16+
toolNames: ['run_file_change_hooks'],
2017
spawnableAgents: [],
2118

22-
systemPrompt: `You are an expert programmer who can articulate very clear feedback on code changes.`,
19+
inheritParentSystemPrompt: true,
20+
includeMessageHistory: true,
2321

2422
instructionsPrompt: `Your task is to provide helpful feedback on the last file changes made by the assistant.
2523
@@ -42,7 +40,5 @@ Next, you should critique the code changes made recently in the above conversati
4240
- Make sure no sections were deleted that weren't supposed to be deleted.
4341
- Make sure the new code matches the style of the existing code.
4442
45-
Be concise and to the point. After providing all your feedback, use the end_turn tool to end your response.`,
46-
47-
stepPrompt: `IMPORTANT: Don't forget to end your response with the end_turn tool: <end_turn>${closeXml('end_turn')}`,
43+
Be concise and to the point.`,
4844
})

.agents/types/agent-definition.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export interface AgentDefinition {
9595
/** Whether to include conversation history from the parent agent in context.
9696
*
9797
* Defaults to false.
98-
* Use this if the agent needs to know all the previous messages in the conversation.
98+
* Use this when the agent needs to know all the previous messages in the conversation.
9999
*/
100100
includeMessageHistory?: boolean
101101

@@ -121,6 +121,14 @@ export interface AgentDefinition {
121121
* This field is key if the agent is intended to be spawned by other agents. */
122122
spawnerPrompt?: string
123123

124+
/** Whether to inherit the parent agent's system prompt instead of using this agent's own systemPrompt.
125+
*
126+
* Defaults to false.
127+
* Use this when you want to enable prompt caching by preserving the same system prompt prefix.
128+
* Cannot be used together with the systemPrompt field.
129+
*/
130+
inheritParentSystemPrompt?: boolean
131+
124132
/** Background information for the agent. Fairly optional. Prefer using instructionsPrompt for agent instructions. */
125133
systemPrompt?: string
126134

@@ -289,7 +297,7 @@ export type ModelName =
289297
| 'openai/gpt-5-nano'
290298

291299
// Anthropic
292-
| 'anthropic/claude-sonnet-4'
300+
| 'anthropic/claude-sonnet-4.5'
293301
| 'anthropic/claude-opus-4.1'
294302

295303
// Gemini
@@ -326,8 +334,8 @@ export type ModelName =
326334
// Other open source models
327335
| 'moonshotai/kimi-k2'
328336
| 'moonshotai/kimi-k2:nitro'
329-
| 'z-ai/glm-4.5'
330-
| 'z-ai/glm-4.5:nitro'
337+
| 'z-ai/glm-4.6'
338+
| 'z-ai/glm-4.6:nitro'
331339
| (string & {})
332340

333341
export type { Tools }

0 commit comments

Comments
 (0)