Agentic AITypeScriptverifiedVerified

Multi-Agent Orchestration Pattern in TypeScript

Coordinate a network of specialised AI agents under an orchestrator, where each agent owns a distinct capability or domain and agents communicate through structured messages.

How to Implement the Multi-Agent Orchestration Pattern in TypeScript

1Step 1: Define the message and specialist agent interfaces

interface AgentMessage {
  id: string;
  from: string;
  to: string;
  type: "task" | "result" | "error";
  payload: unknown;
}

interface SpecialistAgent {
  id: string;
  capabilities: string[];
  handle(message: AgentMessage): Promise<AgentMessage>;
}

2Step 2: Build the Orchestrator with agent registration and routing

class Orchestrator {
  private agents = new Map<string, SpecialistAgent>();

  register(agent: SpecialistAgent): void {
    this.agents.set(agent.id, agent);
  }

  findAgent(capability: string): SpecialistAgent | undefined {
    for (const agent of this.agents.values()) {
      if (agent.capabilities.includes(capability)) return agent;
    }
  }

3Step 3: Implement task dispatch and pipeline execution

  async dispatch(
    task: string,
    capability: string,
    payload: unknown
  ): Promise<unknown> {
    const agent = this.findAgent(capability);
    if (!agent) throw new Error(`No agent for capability: ${capability}`);

    const message: AgentMessage = {
      id: crypto.randomUUID(),
      from: "orchestrator",
      to: agent.id,
      type: "task",
      payload: { task, data: payload },
    };

    const response = await agent.handle(message);
    if (response.type === "error") throw new Error(String(response.payload));
    return response.payload;
  }

  async runPipeline(
    steps: Array<{ capability: string; payload: unknown }>
  ): Promise<unknown[]> {
    const results: unknown[] = [];
    for (const step of steps) {
      const result = await this.dispatch(step.capability, step.capability, step.payload);
      results.push(result);
    }
    return results;
  }
}

Multi-Agent Orchestration Pattern Architecture

hourglass_empty

Rendering diagram...

lightbulb

Multi-Agent Orchestration Pattern in the Real World

A film director (Orchestrator) does not personally operate the camera, compose the score, or design costumes. Instead they delegate to specialist department heads — cinematographer, composer, costume designer — each expert in their domain. The director collects their work, gives feedback, and integrates it into a coherent film.