Agentic AIPythonverifiedVerified

Multi-Agent Orchestration Pattern in Python

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 Python

1Step 1: Define the message and specialist agent interfaces

import uuid
from dataclasses import dataclass, field
from typing import Any, Protocol, Literal


@dataclass
class AgentMessage:
    id: str
    sender: str
    receiver: str
    type: Literal["task", "result", "error"]
    payload: Any


class SpecialistAgent(Protocol):
    id: str
    capabilities: list[str]
    async def handle(self, message: AgentMessage) -> AgentMessage: ...

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

class Orchestrator:
    def __init__(self) -> None:
        self._agents: dict[str, SpecialistAgent] = {}

    def register(self, agent: SpecialistAgent) -> None:
        self._agents[agent.id] = agent

    def find_agent(self, capability: str) -> SpecialistAgent | None:
        for agent in self._agents.values():
            if capability in agent.capabilities:
                return agent
        return None

3Step 3: Implement task dispatch and pipeline execution

    async def dispatch(
        self, task: str, capability: str, payload: Any
    ) -> Any:
        agent = self.find_agent(capability)
        if agent is None:
            raise ValueError(f"No agent for capability: {capability}")

        message = AgentMessage(
            id=str(uuid.uuid4()),
            sender="orchestrator",
            receiver=agent.id,
            type="task",
            payload={"task": task, "data": payload},
        )
        response = await agent.handle(message)
        if response.type == "error":
            raise RuntimeError(str(response.payload))
        return response.payload

    async def run_pipeline(
        self, steps: list[dict[str, Any]]
    ) -> list[Any]:
        results: list[Any] = []
        for step in steps:
            result = await self.dispatch(
                step["capability"], step["capability"], step["payload"]
            )
            results.append(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.