System prompt, tools, and sampling parameters
Operation that receives the root branch and prefix length. Typically calls runAgents or useAgentPool inside.
The body's return value
const { result, prefixLen } = yield* withSharedRoot(
{ systemPrompt: RESEARCH_PROMPT, tools: toolsJson },
function*(root, prefixLen) {
const result = yield* runAgents({
tasks: questions.map(q => ({
systemPrompt: RESEARCH_PROMPT,
content: q,
tools: toolsJson,
parent: root,
})),
tools: toolMap,
});
return { result, prefixLen };
},
);
Scoped shared root branch with guaranteed cleanup
Creates a root branch, prefills the system prompt, and passes it to the body function. The root is pruned via try/finally when the body returns or throws, regardless of whether children still exist.
Use this for the cold-path pattern where multiple agents share a tokenized system prompt prefix. The
sharedPrefixLengthpassed to the body enables KV savings calculation.