Tenzro Testnet is live —request testnet TNZO
← Back to Tutorials
AI AgentsAdvanced

Build an Agent Swarm Orchestrator

Create a multi-agent swarm that distributes tasks across specialized worker agents. The orchestrator spawns workers, delegates research tasks in parallel, collects results, and produces a synthesized output -- all coordinated on the Tenzro network with TNZO micropayments.

What We're Building

Step 1: Create the Orchestrator Agent

import { TenzroClient, TESTNET_CONFIG } from 'tenzro-sdk';

const client = new TenzroClient(TESTNET_CONFIG);

// Register the orchestrator agent
const orchestrator = await client.agent.register(
  'research-orchestrator',
  'Research Orchestrator',
  ['orchestration', 'task-management']
);
console.log('Orchestrator:', orchestrator.agent_id);

Step 2: Spawn Worker Agents

// Spawn specialized workers
const nlpWorker = await client.agent.spawnAgent(
  orchestrator.agent_id, // parent
  'nlp-analyst',
  ['nlp', 'sentiment-analysis'],
);

const codeWorker = await client.agent.spawnAgent(
  orchestrator.agent_id,
  'code-reviewer',
  ['code', 'security-audit'],
);

const searchWorker = await client.agent.spawnAgent(
  orchestrator.agent_id,
  'web-researcher',
  ['web-search', 'summarization'],
);

console.log('Workers spawned:', [nlpWorker, codeWorker, searchWorker].map(w => w.agent_id));

Step 3: Create the Swarm

// Create a coordinated swarm
const swarm = await client.agent.createSwarm(
  orchestrator.agent_id,
  [
    { name: 'nlp-analyst', capabilities: ['nlp', 'sentiment-analysis'] },
    { name: 'code-reviewer', capabilities: ['code', 'security-audit'] },
    { name: 'web-researcher', capabilities: ['web-search', 'summarization'] },
  ]
);
console.log('Swarm ID:', swarm.swarm_id);

Step 4: Delegate Tasks to Workers

// Delegate research tasks in parallel
const tasks = await Promise.all([
  client.agent.delegateTask(
    nlpWorker.agent_id,
    'Analyze sentiment of recent TNZO market discussions'
  ),
  client.agent.delegateTask(
    codeWorker.agent_id,
    'Review the latest smart contract audit findings for DeFi protocols'
  ),
  client.agent.delegateTask(
    searchWorker.agent_id,
    'Research the top 5 cross-chain bridge innovations in 2026'
  ),
]);

console.log('Tasks delegated:', tasks.map(t => t.id));

Step 5: Monitor Swarm Status

// Check swarm status
const status = await client.agent.getSwarmStatus(swarm.swarm_id);
console.log('Swarm status:', status.status);
console.log('Members:', status.member_count);

// Poll until all tasks complete
let allDone = false;
while (!allDone) {
  const taskStatuses = await Promise.all(
    tasks.map(t => client.task.get(t.task_id))
  );
  allDone = taskStatuses.every(t => t.status === 'completed' || t.status === 'failed');
  if (!allDone) await new Promise(r => setTimeout(r, 5000));
}

Step 6: Collect and Synthesize Results

// Gather results from all workers
const results = await Promise.all(
  tasks.map(t => client.task.get(t.task_id))
);

for (const result of results) {
  console.log('Task:', result.title);
  console.log('Status:', result.status);
  console.log('Result:', result.result?.substring(0, 200));
  console.log('---');
}

// Use inference to synthesize findings
const synthesis = await client.inference.request(
  'qwen3.5-0.8b',
  'Synthesize these research findings into a brief report: ' +
  results.map(r => r.result).join(' | '),
  512
);
console.log('Synthesis:', synthesis.output);

Step 7: Terminate Swarm

// Clean up when done
await client.agent.terminateSwarm(swarm.swarm_id);
console.log('Swarm terminated');

Use Cases

Related