AutoGen Tutorial (TypeScript): running agents in parallel for intermediate developers

By Cyprian AaronsUpdated 2026-04-21
autogenrunning-agents-in-parallel-for-intermediate-developerstypescript

This tutorial shows how to run multiple AutoGen agents in parallel from TypeScript, collect their outputs, and merge the results into one final answer. You need this when one agent call is too slow, or when you want independent specialists to work on separate parts of the same task.

What You'll Need

  • Node.js 18+ and npm
  • A TypeScript project with ts-node or a build step
  • @autogenai/autogen installed
  • An OpenAI API key exported as OPENAI_API_KEY
  • Basic familiarity with AutoGen agents, messages, and model clients

Step-by-Step

  1. Start with a minimal TypeScript project and install the dependencies. This example uses the official AutoGen TypeScript package plus dotenv for local environment loading.
mkdir autogen-parallel-demo
cd autogen-parallel-demo
npm init -y
npm install @autogenai/autogen dotenv
npm install -D typescript ts-node @types/node
  1. Create a simple tsconfig.json so TypeScript can run cleanly under Node ESM-style imports. Keep it strict enough to catch mistakes early.
{
  "compilerOptions": {
    "target": "ES2022",
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "outDir": "dist"
  },
  "include": ["src/**/*.ts"]
}
  1. Add your API key in a .env file and create the main script. The important part here is that each agent gets its own async task, and we use Promise.all() to run them concurrently.
OPENAI_API_KEY=your_key_here
import "dotenv/config";
import { AssistantAgent, OpenAIChatCompletionClient } from "@autogenai/autogen";

const client = new OpenAIChatCompletionClient({
  model: "gpt-4o-mini",
});

const researchAgent = new AssistantAgent({
  name: "research_agent",
  modelClient: client,
  systemMessage: "You are a concise research analyst.",
});

const riskAgent = new AssistantAgent({
  name: "risk_agent",
  modelClient: client,
  systemMessage: "You are a cautious risk reviewer.",
});
  1. Define two independent tasks and execute them in parallel. Each agent gets a different prompt so you can compare outputs side by side instead of waiting for one response before starting the next.
async function runParallelTasks() {
  const topic = "Should a bank allow customers to freeze debit cards from a mobile app?";

  const researchTask = researchAgent.run([
    { role: "user", content: `List three customer benefits of this feature.` },
  ]);

  const riskTask = riskAgent.run([
    { role: "user", content: `List three operational or security risks of this feature.` },
  ]);

  const [researchResult, riskResult] = await Promise.all([researchTask, riskTask]);

  return {
    benefits: researchResult.messages.at(-1)?.content ?? "",
    risks: riskResult.messages.at(-1)?.content ?? "",
    topic,
  };
}
  1. Add a third step that synthesizes both outputs into one final response. In production, this is the pattern you want: parallel specialists first, then one aggregator agent that resolves conflicts and writes the final answer.
const synthesisAgent = new AssistantAgent({
  name: "synthesis_agent",
  modelClient: client,
  systemMessage: "You combine inputs into one clear recommendation for product teams.",
});

async function synthesize(result: { topic: string; benefits: string; risks: string }) {
  const prompt = `
Topic:
${result.topic}

Benefits:
${result.benefits}

Risks:
${result.risks}

Write a short recommendation for a bank product team.
`;

  const response = await synthesisAgent.run([{ role: "user", content: prompt }]);
  return response.messages.at(-1)?.content ?? "";
}
  1. Wire everything together and print the result. Close the loop with an actual executable entry point so you can run it immediately with ts-node.
async function main() {
  const parallelResult = await runParallelTasks();
  const finalAnswer = await synthesize(parallelResult);

  console.log("=== BENEFITS ===");
  console.log(parallelResult.benefits);
  console.log("\n=== RISKS ===");
  console.log(parallelResult.risks);
  console.log("\n=== FINAL RECOMMENDATION ===");
  console.log(finalAnswer);
}

main().catch((error) => {
  console.error(error);
  process.exit(1);
});

Testing It

Run the script with npx ts-node src/index.ts. If everything is wired correctly, you should see both specialist outputs printed before the final recommendation appears.

The key thing to verify is timing behavior. Both agent calls should start together, so total runtime should be closer to the slower of the two calls rather than the sum of both calls.

If one agent fails, Promise.all() will reject immediately. That is useful during development because it surfaces broken prompts or auth issues fast; later you can switch to Promise.allSettled() if you want partial results.

Next Steps

  • Add timeout handling around each agent call so one slow specialist does not block the whole workflow.
  • Replace Promise.all() with Promise.allSettled() when partial completion is acceptable.
  • Move from two fixed agents to dynamic fan-out based on input classification or document chunking.

Keep learning

By Cyprian Aarons, AI Consultant at Topiax.

Want the complete 8-step roadmap?

Grab the free AI Agent Starter Kit — architecture templates, compliance checklists, and a 7-email deep-dive course.

Get the Starter Kit

Related Guides