{"id":600,"date":"2025-01-11T15:33:15","date_gmt":"2025-01-11T15:33:15","guid":{"rendered":"https:\/\/smolagents.org\/?post_type=docs&#038;p=600"},"modified":"2025-01-11T15:40:57","modified_gmt":"2025-01-11T15:40:57","password":"","slug":"orchestrate-a-multi-agent-system-%f0%9f%a4%96%f0%9f%a4%9d%f0%9f%a4%96","status":"publish","type":"docs","link":"https:\/\/smolagents.org\/hi\/docs\/orchestrate-a-multi-agent-system-\ud83e\udd16\ud83e\udd1d\ud83e\udd16\/","title":{"rendered":"Orchestrate a multi-agent system \ud83e\udd16\ud83e\udd1d\ud83e\udd16"},"content":{"rendered":"<p>In this notebook we will make a&nbsp;<strong>multi-agent web browser: an agentic system with several agents collaborating to solve problems using the web!<\/strong><\/p>\n\n\n\n<p>It will be a simple hierarchy, using a&nbsp;<code>ManagedAgent<\/code>&nbsp;object to wrap the managed web search agent:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">              +----------------+\n              | Manager agent  |\n              +----------------+\n                       |\n        _______________|______________\n       |                              |\n  Code interpreter   +--------------------------------+\n       tool          |         Managed agent          |\n                     |      +------------------+      |\n                     |      | Web Search agent |      |\n                     |      +------------------+      |\n                     |         |            |         |\n                     |  Web Search tool     |         |\n                     |             Visit webpage tool |\n                     +--------------------------------+<\/pre>\n\n\n\n<p>Let\u2019s set up this system.<\/p>\n\n\n\n<p>Run the line below to install the required dependencies:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">!pip install markdownify duckduckgo-search smolagents --upgrade -q<\/pre>\n\n\n\n<p>Let\u2019s login in order to call the HF Inference API:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from huggingface_hub import login\n\nlogin()<\/pre>\n\n\n\n<p>\u26a1\ufe0f Our agent will be powered by\u00a0<a href=\"https:\/\/huggingface.co\/Qwen\/Qwen2.5-Coder-32B-Instruct\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">Qwen\/Qwen2.5-Coder-32B-Instruct<\/a>\u00a0using\u00a0<code>HfApiModel<\/code>\u00a0class that uses HF\u2019s Inference API: the Inference API allows to quickly and easily run any OS model.<\/p>\n\n\n\n<p><em>Note:<\/em>\u00a0The Inference API hosts models based on various criteria, and deployed models may be updated or replaced without prior notice. Learn more about it\u00a0<a href=\"https:\/\/huggingface.co\/docs\/api-inference\/supported-models\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">here<\/a>.<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">model_id = \"Qwen\/Qwen2.5-Coder-32B-Instruct\"<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/examples\/multiagents#-create-a-web-search-tool\" target=\"_blank\" rel=\"noopener\"><\/a>\ud83d\udd0d Create a web search tool<\/h2>\n\n\n\n<p>For web browsing, we can already use our pre-existing\u00a0<a href=\"https:\/\/github.com\/huggingface\/smolagents\/blob\/main\/src\/smolagents\/default_tools\/search.py\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"><code>DuckDuckGoSearchTool<\/code><\/a>\u00a0tool to provide a Google search equivalent.<\/p>\n\n\n\n<p>But then we will also need to be able to peak into the page found by the&nbsp;<code>DuckDuckGoSearchTool<\/code>. To do so, we could import the library\u2019s built-in&nbsp;<code>VisitWebpageTool<\/code>, but we will build it again to see how it\u2019s done.<\/p>\n\n\n\n<p>So let\u2019s create our&nbsp;<code>VisitWebpageTool<\/code>&nbsp;tool from scratch using&nbsp;<code>markdownify<\/code>.<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import re\nimport requests\nfrom markdownify import markdownify\nfrom requests.exceptions import RequestException\nfrom smolagents import tool\n\n\n@tool\ndef visit_webpage(url: str) -&gt; str:\n    \"\"\"Visits a webpage at the given URL and returns its content as a markdown string.\n\n    Args:\n        url: The URL of the webpage to visit.\n\n    Returns:\n        The content of the webpage converted to Markdown, or an error message if the request fails.\n    \"\"\"\n    try:\n        <em># Send a GET request to the URL<\/em>\n        response = requests.get(url)\n        response.raise_for_status()  <em># Raise an exception for bad status codes<\/em>\n\n        <em># Convert the HTML content to Markdown<\/em>\n        markdown_content = markdownify(response.text).strip()\n\n        <em># Remove multiple line breaks<\/em>\n        markdown_content = re.sub(r\"\\n{3,}\", \"\\n\\n\", markdown_content)\n\n        return markdown_content\n\n    except RequestException as e:\n        return f\"Error fetching the webpage: {str(e)}\"\n    except Exception as e:\n        return f\"An unexpected error occurred: {str(e)}\"<\/pre>\n\n\n\n<p>Ok, now let\u2019s initialize and test our tool!<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(visit_webpage(\"https:\/\/en.wikipedia.org\/wiki\/Hugging_Face\")[:500])<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/examples\/multiagents#build-our-multi-agent-system-\" target=\"_blank\" rel=\"noopener\"><\/a>Build our multi-agent system \ud83e\udd16\ud83e\udd1d\ud83e\udd16<\/h2>\n\n\n\n<p>Now that we have all the tools&nbsp;<code>search<\/code>&nbsp;and&nbsp;<code>visit_webpage<\/code>, we can use them to create the web agent.<\/p>\n\n\n\n<p>Which configuration to choose for this agent?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Web browsing is a single-timeline task that does not require parallel tool calls, so JSON tool calling works well for that. We thus choose a\u00a0<code>JsonAgent<\/code>.<\/li>\n\n\n\n<li>Also, since sometimes web search requires exploring many pages before finding the correct answer, we prefer to increase the number of\u00a0<code>max_steps<\/code>\u00a0to 10.<\/li>\n<\/ul>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import (\n    CodeAgent,\n    ToolCallingAgent,\n    HfApiModel,\n    ManagedAgent,\n    DuckDuckGoSearchTool,\n    LiteLLMModel,\n)\n\nmodel = HfApiModel(model_id)\n\nweb_agent = ToolCallingAgent(\n    tools=[DuckDuckGoSearchTool(), visit_webpage],\n    model=model,\n    max_steps=10,\n)<\/pre>\n\n\n\n<p>We then wrap this agent into a&nbsp;<code>ManagedAgent<\/code>&nbsp;that will make it callable by its manager agent.<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">managed_web_agent = ManagedAgent(\n    agent=web_agent,\n    name=\"search\",\n    description=\"Runs web searches for you. Give it your query as an argument.\",\n)<\/pre>\n\n\n\n<p>Finally we create a manager agent, and upon initialization we pass our managed agent to it in its&nbsp;<code>managed_agents<\/code>&nbsp;argument.<\/p>\n\n\n\n<p>Since this agent is the one tasked with the planning and thinking, advanced reasoning will be beneficial, so a&nbsp;<code>CodeAgent<\/code>&nbsp;will be the best choice.<\/p>\n\n\n\n<p>Also, we want to ask a question that involves the current year and does additional data calculations: so let us add&nbsp;<code>additional_authorized_imports=[\"time\", \"numpy\", \"pandas\"]<\/code>, just in case the agent needs these packages.<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">manager_agent = CodeAgent(\n    tools=[],\n    model=model,\n    managed_agents=[managed_web_agent],\n    additional_authorized_imports=[\"time\", \"numpy\", \"pandas\"],\n)<\/pre>\n\n\n\n<p>That\u2019s all! Now let\u2019s run our system! We select a question that requires both some calculation and research:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">answer = manager_agent.run(\"If LLM training continues to scale up at the current rhythm until 2030, what would be the electric power in GW required to power the biggest training runs by 2030? What would that correspond to, compared to some countries? Please provide a source for any numbers used.\")<\/pre>\n\n\n\n<p>We get this report as the answer:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Based on current growth projections and energy consumption estimates, if LLM trainings continue to scale up at the \ncurrent rhythm until 2030:\n\n1. The electric power required to power the biggest training runs by 2030 would be approximately 303.74 GW, which \ntranslates to about 2,660,762 GWh\/year.\n\n2. Comparing this to countries' electricity consumption:\n   - It would be equivalent to about 34% of China's total electricity consumption.\n   - It would exceed the total electricity consumption of India (184%), Russia (267%), and Japan (291%).\n   - It would be nearly 9 times the electricity consumption of countries like Italy or Mexico.\n\n3. Source of numbers:\n   - The initial estimate of 5 GW for future LLM training comes from AWS CEO Matt Garman.\n   - The growth projection used a CAGR of 79.80% from market research by Springs.\n   - Country electricity consumption data is from the U.S. Energy Information Administration, primarily for the year \n2021.<\/pre>\n\n\n\n<p>Seems like we\u2019ll need some sizeable powerplants if the\u00a0<a href=\"https:\/\/gwern.net\/scaling-hypothesis\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">scaling hypothesis<\/a>\u00a0continues to hold true.<\/p>\n\n\n\n<p>Our agents managed to efficiently collaborate towards solving the task! \u2705<\/p>\n\n\n\n<p>\ud83d\udca1 You can easily extend this orchestration to more agents: one does the code execution, one the web search, one handles file loadings\u2026<\/p>","protected":false},"excerpt":{"rendered":"<p>In this notebook we will make a&nbsp;multi-agent web browser: an agentic system with several agents collaborating to solve problems using the web! It will be a simple hierarchy, using a&nbsp;ManagedAgent&nbsp;object to wrap the managed web search agent: Copied +&#8212;&#8212;&#8212;&#8212;&#8212;-+ | Manager agent | +&#8212;&#8212;&#8212;&#8212;&#8212;-+ | _______________|______________ | | Code interpreter +&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;+ tool | Managed agent&#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"_kadence_starter_templates_imported_post":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"footnotes":""},"doc_category":[9],"doc_tag":[],"class_list":["post-600","docs","type-docs","status-publish","hentry","doc_category-conceptual-guides"],"year_month":"2026-04","word_count":932,"total_views":"5097","reactions":{"happy":"1","normal":"0","sad":"1"},"author_info":{"name":"smolagents","author_nicename":"wd-gstargmail-com","author_url":"https:\/\/smolagents.org\/hi\/author\/wd-gstargmail-com\/"},"doc_category_info":[{"term_name":"Conceptual guides","term_url":"https:\/\/smolagents.org\/hi\/docs-category\/conceptual-guides\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/600","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs"}],"about":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/types\/docs"}],"author":[{"embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/comments?post=600"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/600\/revisions"}],"predecessor-version":[{"id":601,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/600\/revisions\/601"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/media?parent=600"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/doc_category?post=600"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/doc_tag?post=600"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}