Hierarchical Pattern

Agents are organized in a tree-like structure, with higher-level agents (supervisors) coordinating specialist agents.

Overview

Hierarchies shine when you need structured decision flows, explicit quality checks, and clear ownership of each step. Supervisors break work into pieces, delegate to the right specialists, and assemble the final answer.

Demo Scenario: Multi-Level Research System

This runnable example builds a hierarchical research workflow using sub-agent delegation:

  1. Coordinator agent – manages the research and compilation workflow

  2. Research agent – performs web searches using the google_serper tool

  3. Compiler agent – formats raw findings into a polished summary

The coordinator delegates tasks to research and compiler agents, reviews their outputs, and can make decisions based on their responses. This cyclic decision-making capability is why we use sub-agent delegation instead of gllm-pipeline.

Diagram

spinner

Implementation Steps

  1. Create specialist agents

    from glaip_sdk import Agent
    from glaip_sdk.tools import Tool
    
    web_search_tool = Tool.from_native("google_serper")
    
    research_agent = Agent(
        name="research_agent",
        instruction="Perform web searches and provide comprehensive information...",
        tools=[web_search_tool],
        model="openai/gpt-5.2"
    )
    
    compiler_agent = Agent(
        name="compiler_agent",
        instruction="Transform raw research into summaries...",
        model="openai/gpt-5.2"
    )
  2. Create coordinator agent with sub-agents

    coordinator_agent = Agent(
        name="coordinator_agent",
        instruction="""Manage research and compilation tasks.
        Delegate to 'research_agent' to gather information,
        then delegate to 'compiler_agent' to create summaries...""",
        agents=[research_agent, compiler_agent],  # Sub-agent delegation
        model="openai/gpt-5.2"
    )
  3. Run the coordinator

    result = coordinator_agent.run(
        "Research this topic and provide a compiled summary: [topic]",
        verbose=False
    )

Full implementation: See hierarchical/main.py for complete code with detailed instructions.

Why sub-agents? This pattern uses sub-agent delegation (not gllm-pipeline) because the coordinator needs to make decisions based on sub-agent responses and potentially loop back if needed.

How to Run

From the glaip/examples/multi-agent-system-patterns directory in the GL SDK Cookbookarrow-up-right:

Ensure your .env contains:

Note: You'll need a Serper API keyarrow-up-right for the google_serper tool to work.

Output

Notes

  • This example uses sub-agent delegation (not gllm-pipeline) because the coordinator needs to make autonomous decisions based on sub-agent responses.

  • Add reviewers or domain specialists by including them in the coordinator's agents list.

  • The coordinator agent can implement feedback loops, quality checks, and conditional delegation based on sub-agent outputs.

  • For linear workflows without decision-making or loops, prefer patterns that use gllm-pipeline (Sequential, Parallel, Router, Aggregator) which leverage the AgentComponent wrapper.

  • For iterative optimization with feedback loops, see the Loop pattern.

Last updated