Back to AI Recipes

Orchestrator-Workers

Workflow

Workflow

A central orchestrator delegates subtasks to specialized workers.

Summary

The Orchestrator-workers pattern implements a hierarchical workflow where a central orchestrator LLM breaks down complex tasks into subtasks, delegates them to specialized worker LLMs, and then synthesizes their outputs into a coherent final result. This pattern is particularly effective for handling complex, multi-step tasks that require different types of expertise or processing.

How it works

  1. Task Analysis: Orchestrator analyzes the complex request
  2. Subtask Planning: Decomposes into specialized subtasks with dependencies
  3. Worker Dispatch: Each worker receives its assigned subtask
  4. Result Collection: Workers return specialized outputs
  5. Synthesis: Orchestrator combines all results into final response

Coordination patterns

  • Linear: Workers execute sequentially, passing results
  • Hierarchical: Workers report to orchestrator between stages
  • Fan-out/fan-in: Parallel workers then orchestrated merge
  • Dynamic: Orchestrator adapts plan based on worker results

Use cases

  • Complex content generation requiring multiple specialized perspectives
  • Research and analysis tasks that need to be broken down into manageable subtasks
  • Multi-step problem solving where each step requires different expertise
  • Coordinated content creation and refinement workflows
Code View

Orchestrator-Workers Implementation

// Orchestrator-Workers recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Apply the agent recipe to structure the reasoning and produce a useful result. Recipe: Orchestrator-Workers. Description: A central orchestrator delegates subtasks to specialized workers. Focus: Workflow Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

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