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

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

The article presents a hands-on tutorial for building a hierarchical AI agent framework that uses open-source large language models (LLMs) to plan, execute, and refine tasks. Instead of a single monolithic model responding directly to prompts, the system is divided into three distinct roles — a planner agent to break down complex tasks into actionable steps, an executor agent to carry out those steps using reasoning or Python tools, and an aggregator agent to combine the results into a final output. This architecture illustrates how structured multi-agent reasoning can make autonomous AI systems more modular and scalable.

A key part of the implementation is loading and configuring an open-source model (in this case, the Qwen instruct model) and setting up the environment so that it can run efficiently with quantization when possible. The tutorial walks through installing libraries like transformers, initializing the tokenizer and model, and defining core functions to interact with the model. It also explains how the system can execute embedded Python code, enabling the agent to not just reason but also perform real tool actions when needed.

The planner agent uses a structured prompt to generate a JSON plan that decomposes a user’s task into clear steps, each tagged with an associated tool indicator (e.g., “llm” or “python”). The executor agent then interprets each step and either runs Python code or generates reasoning responses based on the tool tag and the current context. Finally, the aggregator agent synthesizes the execution results into a polished answer. This structured, step-by-step process helps ensure that the system’s reasoning and actions are traceable and modular.

In demonstrating the “run_hierarchical_agent” function with a demo logistics task, the article shows how this architecture can produce detailed plans and results, revealing not just the final answer but also the intermediate outputs from each agent role. By leveraging open-source LLMs and clearly defined agent roles, the tutorial provides a practical foundation for building autonomous workflows that combine planning, execution, tool-based actions, and result aggregation — an approach that helps scale AI reasoning to more complex, multi-step problems.

About the author

TOOLHUNT

Effortlessly find the right tools for the job.

TOOLHUNT

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to TOOLHUNT.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.