Advanced Orchestration Engines ⏱ 11 min read 📁 examples/15_delegation.mjs

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

withTaskAgents(client, profiles)
Registers specialized subagent personas (researcher, writer, reviewer) callable via task tool.
Structured Briefs
Rigorous task instructions specifying input context, acceptance criteria, and deliverable format.
withObjective(objective)
Threads top-level goals into every child subagent invocation.
save_artifact tool
Allows subagents to persist large outputs by reference without context bloat.

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.mjs

Read the full example: examples/15_delegation.mjs →

Expected Output: Orchestrator decomposes request, delegates to security auditor and writer subagents, and compiles final deliverable.