AutoGen Tutorial (TypeScript): running agents in parallel for beginners
This tutorial shows you how to run multiple AutoGen agents in parallel with TypeScript, collect their outputs, and combine the results into one final answer. You need this when one agent is not enough, or when you want to split work across specialized agents and reduce total wait time.
What You'll Need
- •Node.js 18+ installed
- •A TypeScript project with
ts-nodeor a build step - •
npmorpnpm - •An OpenAI API key exported as
OPENAI_API_KEY - •AutoGen packages:
- •
@autogen/core - •
@autogen/openai
- •
- •A model that supports chat completions, such as
gpt-4o-mini
Install the packages:
npm install @autogen/core @autogen/openai
npm install -D typescript ts-node @types/node
Set your API key:
export OPENAI_API_KEY="your-key-here"
Step-by-Step
- •Start by creating a model client and a small helper for running one agent task. The important part here is that each agent gets its own prompt and can be awaited independently.
import { OpenAIChatCompletionClient } from "@autogen/openai";
import { AssistantAgent } from "@autogen/core";
const modelClient = new OpenAIChatCompletionClient({
model: "gpt-4o-mini",
});
async function runAgent(name: string, task: string): Promise<string> {
const agent = new AssistantAgent({
name,
modelClient,
systemMessage: `You are ${name}. Be concise and specific.`,
});
const result = await agent.run([{ role: "user", content: task }]);
return result.messages.at(-1)?.content?.toString() ?? "";
}
- •Next, define the jobs you want to run in parallel. Keep them independent; parallel execution only helps when one task does not depend on another task's output.
const tasks = [
{
name: "risk_analyst",
prompt: "Review this insurance claim for obvious fraud signals. Return 3 bullet points.",
},
{
name: "policy_checker",
prompt: "Check whether this claim description sounds consistent with a standard auto policy.",
},
{
name: "summary_writer",
prompt: "Write a short customer-facing summary of the claim status in plain English.",
},
];
- •Run all agents at the same time with
Promise.all. This is the core pattern: create one promise per agent, then wait for all of them together.
async function main() {
const results = await Promise.all(
tasks.map((task) => runAgent(task.name, task.prompt))
);
results.forEach((output, index) => {
console.log(`\n=== ${tasks[index].name} ===`);
console.log(output);
});
}
main().catch(console.error);
- •If you want a single final answer, add an aggregator step after the parallel agents finish. This is how you turn multiple specialist outputs into one usable result for a downstream workflow.
async function main() {
const results = await Promise.all(
tasks.map((task) => runAgent(task.name, task.prompt))
);
const combined = tasks
.map((task, index) => `${task.name}:\n${results[index]}`)
.join("\n\n");
const finalAnswer = await runAgent(
"aggregator",
`Combine these specialist outputs into one clean report:\n\n${combined}`
);
console.log("\n=== FINAL REPORT ===");
console.log(finalAnswer);
}
main().catch(console.error);
- •In production, add basic failure handling so one bad agent does not take down the whole batch.
Promise.allSettledis usually the safer choice when running independent work in parallel.
async function main() {
const settled = await Promise.allSettled(
tasks.map((task) => runAgent(task.name, task.prompt))
);
settled.forEach((result, index) => {
console.log(`\n=== ${tasks[index].name} ===`);
if (result.status === "fulfilled") {
console.log(result.value);
} else {
console.log(`FAILED: ${result.reason}`);
}
});
}
main().catch(console.error);
Testing It
Run the file with ts-node or compile it with tsc and execute the output with Node. If everything is wired correctly, you should see three agent outputs printed without waiting for each one to finish before starting the next.
To confirm parallelism, add timestamps before and after each call inside runAgent. You should see overlapping execution times instead of strictly sequential completion.
If one request fails, switch from Promise.all to Promise.allSettled and verify that the other agents still return results. That is the version you want once this moves beyond a demo.
Next Steps
- •Add per-agent tools so each worker can query different systems, like policy docs or claims data
- •Introduce rate limiting and retry logic before sending more than a few concurrent requests
- •Wrap this pattern in a queue so your app can fan out work safely under load
Keep learning
- •The complete AI Agents Roadmap — my full 8-step breakdown
- •Free: The AI Agent Starter Kit — PDF checklist + starter code
- •Work with me — I build AI for banks and insurance companies
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