A Coding Implementation to Build a Hierarchical Planner AI Agent Using Open-Source LLMs with Tool Execution and Structured Multi-Agent Reasoning

Building a Hierarchical Planner AI Agent Using Open-Source LLMs: A Coding Implementation

By Amr Abdeldaym, Founder of Thiqa Flow

Artificial intelligence (AI) automation continues to revolutionize business processes by enabling scalable and modular solutions to complex tasks. One of the forefront approaches in AI agent design is the hierarchical multi-agent framework, which decomposes high-level goals into manageable sub-tasks executed systematically. In this tutorial, we explore a comprehensive coding implementation of a hierarchical planner AI agent leveraging open-source large language models (LLMs), integrated tool execution, and structured multi-agent reasoning to drive business efficiency.

Introduction to Hierarchical Multi-Agent AI Architecture

Modern AI systems increasingly demand autonomy in planning, executing, and synthesizing results for intricate workflows. The solution lies in breaking down complex problems via a hierarchical planner agent, complemented by specialized executor and aggregator agents. This division of labor not only enhances clarity but also promotes scalability and modularity in AI-driven automation.

Agent Component Role Key Functions
Planner Agent Decompose complex goals Generates actionable steps in structured JSON format
Executor Agent Execute steps using reasoning or tools Runs Python code tools or LLM reasoning
Aggregator Agent Synthesizes results into final output Combines step outputs into coherent, actionable responses

Implementing the Hierarchical Planner Agent with Open-Source LLMs

Leveraging the Qwen Instruct Model for AI Automation

We adopt the open-source Qwen/Qwen2.5-1.5B-Instruct model, renowned for its fine-tuning to instruction prompts and robust reasoning capabilities. To optimize performance, the model is configured to run efficiently on available GPUs using 4-bit quantization, enabling scalable deployments in cloud environments such as Google Colab—an ideal approach for cost-effective business automation.

Key Functional Modules

  • llm_chat: Core interface for communicating with the LLM, generating structured responses guided by system/user prompts.
  • run_python: Tool integration allowing dynamic execution of Python code generated by the executor, supporting data processing and calculations.
  • extract_json_block: Robust JSON parsing method to reliably extract hierarchical plans from model outputs, ensuring structured downstream processing.

Structured Multi-Agent Reasoning Workflow

The hierarchical agent system operates via three specialized prompts corresponding to planner, executor, and aggregator roles:

  • Planner Prompt: Instructs the agent to break down complex goals into 3–8 independent, actionable steps, specifying tool usage and expected outputs in JSON format.
  • Executor Prompt: Drives execution of each step, deciding whether to use reasoning, Python code, or simple LLM outputs depending on the task requirements.
  • Aggregator Prompt: Synthesizes all intermediate results, producing polished, actionable recommendations tailored for business deployment.

Hierarchical Agent Orchestration Table

Stage Input Process Output
Planning User Task Decompose into stepwise JSON plan Structured step list with instructions and tool tags
Execution Individual steps and context Run reasoning or Python code per step Step-specific results with execution logs
Aggregation Complete plan and step results Combine and polish into final output Coherent, actionable response for end users/businesses

Enhancing Business Efficiency Through AI Automation

This hierarchical planner AI agent architecture empowers businesses to:

  • Automate complex workflows: By structurally breaking down tasks, reducing manual planning overhead.
  • Leverage tool integration: Seamlessly incorporate code execution to handle data-intensive or computational sub-tasks.
  • Ensure modular scalability: Enable independent development of planner, executor, and aggregator components for continuous improvement.
  • Provide clear analytics: The modular output allows for detailed auditing and decision tracking in automated pipelines.

Such a framework significantly improves operational efficiency by reducing human intervention and accelerating task completion with intelligent AI assistance.

Conclusion

In this tutorial, we demonstrated an end-to-end coding implementation of a hierarchical planner AI agent system using open-source instruct LLMs. By integrating a structured multi-agent setup—planner, executor, and aggregator—we forged a powerful autonomous framework capable of complex reasoning, Python tool execution, and result synthesis. This system serves as a foundational blueprint for advanced AI automation tailored to business needs, driving enhanced efficiency, accuracy, and scalability.

For organizations seeking to leverage AI automation to streamline workflows and empower decision-making, this approach presents a practical and extensible pathway.


Looking for custom AI automation for your business? Connect with me at https://amr-abdeldaym.netlify.app/