Lesson 20: Subagent Orchestration and Briefs
Implement recursive agentic delegation with the task tool, specialized worker profiles, structured briefs, withObjective, and artifact persistence.
1. Why This Feature Exists
General-purpose models perform best when delegating to tightly scoped specialist subagents with dedicated system prompts, distinct models, and explicit acceptance briefs.
2. Key Symbols and Exports
3. Implementation Walkthrough
import {
ProviderType,
resolveProvider,
initializeClient,
loadCredentials,
getDefaultModel,
getToolModel,
withTaskAgents
} from 'merci-sdk';
const PROVIDER = resolveProvider();
const { client } = await initializeClient({ provider: PROVIDER, options: loadCredentials({ provider: PROVIDER }) });
// Register specialist profiles
withTaskAgents(client, [
{
name: 'security_auditor',
model: getToolModel({ provider: PROVIDER }),
systemPrompt: 'You are an expert AppSec auditor. Analyze code for vulnerabilities and output JSON findings.'
},
{
name: 'technical_writer',
model: getToolModel({ provider: PROVIDER }),
systemPrompt: 'You are a technical documentation writer. Write crisp, structured Markdown.'
}
], { maxDepth: 2 });
const session = client.session(getDefaultModel({ provider: PROVIDER }))
.withObjective('Perform end-to-end security audit and publish documentation.');
const result = await session.run('Audit the authentication handler and generate a remediation guide.');
console.log(result);4. Wire and Telemetry Mechanics
The orchestrator session invokes the synthetic task tool with subagent_type and prompt parameters. The SDK spawns a child ChatSession with matching parentRunId and returns the subagent output.
5. Run the Example
Execute the runnable example directly from the repository root:
node examples/15_delegation.mjsRead the full example: examples/15_delegation.mjs →