Agentic AIPHPverifiedVerified

Multi-Agent Orchestration Pattern in PHP

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 PHP

1Step 1: Define the agent interface and message types

class Message
{
    public function __construct(
        public readonly string $from,
        public readonly string $to,
        public readonly string $content,
    ) {}
}

interface Agent
{
    public function getName(): string;
    public function handle(Message $message): ?Message;
}

2Step 2: Implement a simple multi-agent orchestrator

class Orchestrator
{
    /** @var array<string, Agent> */
    private array $agents = [];

    public function register(Agent $agent): void
    {
        $this->agents[$agent->getName()] = $agent;
    }

    public function dispatch(Message $message): ?Message
    {
        $target = $this->agents[$message->to] ?? null;
        if ($target === null) {
            throw new \RuntimeException("Agent not found: {$message->to}");
        }
        return $target->handle($message);
    }

3Step 3: Run a conversation loop between agents

    public function runConversation(Message $initial, int $maxTurns = 10): array
    {
        $history = [];
        $current = $initial;

        for ($i = 0; $i < $maxTurns; $i++) {
            $response = $this->dispatch($current);
            $history[] = $current;

            if ($response === null) break;

            $current = $response;
        }

        return $history;
    }
}

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.