{"id":589,"date":"2025-01-11T15:17:25","date_gmt":"2025-01-11T15:17:25","guid":{"rendered":"https:\/\/smolagents.org\/?post_type=docs&#038;p=589"},"modified":"2025-01-11T15:41:32","modified_gmt":"2025-01-11T15:41:32","password":"","slug":"building-good-smolagents","status":"publish","type":"docs","link":"https:\/\/smolagents.org\/hi\/docs\/building-good-smolagents\/","title":{"rendered":"Building good Smolagents"},"content":{"rendered":"<p>There\u2019s a world of difference between building an agent that works and one that doesn\u2019t. How can we build agents that fall into the latter category? In this guide, we\u2019re going to see best practices for building agents.<\/p>\n\n\n\n<p>If you\u2019re new to building agents, make sure to first read the\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/conceptual_guides\/intro_agents\" target=\"_blank\" rel=\"noopener\">intro<\/a><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/conceptual_guides\/intro_agents\" target=\"_blank\" rel=\"noreferrer noopener nofollow\"> <\/a><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/conceptual_guides\/intro_agents\" target=\"_blank\" rel=\"noopener\">to agents<\/a>\u00a0and the\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/guided_tour\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">guided tour of smolagents<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#the-best-agentic-systems-are-the-simplest-simplify-the-workflow-as-much-as-you-can\" target=\"_blank\" rel=\"noopener\"><\/a>The best agentic systems are the simplest: simplify the workflow as much as you can<\/h3>\n\n\n\n<p>Giving an LLM some agency in your workflow introduces some risk of errors.<\/p>\n\n\n\n<p>Well-programmed agentic systems have good error logging and retry mechanisms anyway, so the LLM engine has a chance to self-correct their mistake. But to reduce the risk of LLM error to the maximum, you should simplify your workflow!<\/p>\n\n\n\n<p>Let\u2019s revisit the example from [intro_agents]: a bot that answers user queries for a surf trip company. Instead of letting the agent do 2 different calls for \u201ctravel distance API\u201d and \u201cweather API\u201d each time they are asked about a new surf spot, you could just make one unified tool \u201creturn_spot_information\u201d, a function that calls both APIs at once and returns their concatenated outputs to the user.<\/p>\n\n\n\n<p>This will reduce costs, latency, and error risk!<\/p>\n\n\n\n<p>The main guideline is: Reduce the number of LLM calls as much as you can.<\/p>\n\n\n\n<p>This leads to a few takeaways:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Whenever possible, group 2 tools in one, like in our example of the two APIs.<\/li>\n\n\n\n<li>Whenever possible, logic should be based on deterministic functions rather than agentic decisions.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#improve-the-information-flow-to-the-llm-engine\" target=\"_blank\" rel=\"noopener\"><\/a>Improve the information flow to the LLM engine<\/h3>\n\n\n\n<p>Remember that your LLM engine is like a ~intelligent~ robot, tapped into a room with the only communication with the outside world being notes passed under a door.<\/p>\n\n\n\n<p>It won\u2019t know of anything that happened if you don\u2019t explicitly put that into its prompt.<\/p>\n\n\n\n<p>So first start with making your task very clear! Since an agent is powered by an LLM, minor variations in your task formulation might yield completely different results.<\/p>\n\n\n\n<p>Then, improve the information flow towards your agent in tool use.<\/p>\n\n\n\n<p>Particular guidelines to follow:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each tool should log (by simply using\u00a0<code>print<\/code>\u00a0statements inside the tool\u2019s\u00a0<code>forward<\/code>\u00a0method) everything that could be useful for the LLM engine.\n<ul class=\"wp-block-list\">\n<li>In particular, logging detail on tool execution errors would help a lot!<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>For instance, here\u2019s a tool that retrieves weather data based on location and date-time:<\/p>\n\n\n\n<p>First, here\u2019s a poor version:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">import datetime\nfrom smolagents import tool\n\ndef get_weather_report_at_coordinates(coordinates, date_time):\n    <em># Dummy function, returns a list of [temperature in \u00b0C, risk of rain on a scale 0-1, wave height in m]<\/em>\n    return [28.0, 0.35, 0.85]\n\ndef get_coordinates_from_location(location):\n    <em># Returns dummy coordinates<\/em>\n    return [3.3, -42.0]\n\n@tool\ndef get_weather_api(location: str, date_time: str) -&gt; str:\n    \"\"\"\n    Returns the weather report.\n\n    Args:\n        location: the name of the place that you want the weather for.\n        date_time: the date and time for which you want the report.\n    \"\"\"\n    lon, lat = convert_location_to_coordinates(location)\n    date_time = datetime.strptime(date_time)\n    return str(get_weather_report_at_coordinates((lon, lat), date_time))<\/pre>\n\n\n\n<p>Why is it bad?<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>there\u2019s no precision of the format that should be used for\u00a0<code>date_time<\/code><\/li>\n\n\n\n<li>there\u2019s no detail on how location should be specified.<\/li>\n\n\n\n<li>there\u2019s no logging mechanism tying to explicit failure cases like location not being in a proper format, or date_time not being properly formatted.<\/li>\n\n\n\n<li>the output format is hard to understand<\/li>\n<\/ul>\n\n\n\n<p>If the tool call fails, the error trace logged in memory can help the LLM reverse engineer the tool to fix the errors. But why leave it with so much heavy lifting to do?<\/p>\n\n\n\n<p>A better way to build this tool would have been the following:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">@tool\ndef get_weather_api(location: str, date_time: str) -&gt; str:\n    \"\"\"\n    Returns the weather report.\n\n    Args:\n        location: the name of the place that you want the weather for. Should be a place name, followed by possibly a city name, then a country, like \"Anchor Point, Taghazout, Morocco\".\n        date_time: the date and time for which you want the report, formatted as '%m\/%d\/%y %H:%M:%S'.\n    \"\"\"\n    lon, lat = convert_location_to_coordinates(location)\n    try:\n        date_time = datetime.strptime(date_time)\n    except Exception as e:\n        raise ValueError(\"Conversion of `date_time` to datetime format failed, make sure to provide a string in format '%m\/%d\/%y %H:%M:%S'. Full trace:\" + str(e))\n    temperature_celsius, risk_of_rain, wave_height = get_weather_report_at_coordinates((lon, lat), date_time)\n    return f\"Weather report for {location}, {date_time}: Temperature will be {temperature_celsius}\u00b0C, risk of rain is {risk_of_rain*100:.0f}%, wave height is {wave_height}m.\"<\/pre>\n\n\n\n<p>In general, to ease the load on your LLM, the good question to ask yourself is: \u201cHow easy would it be for me, if I was dumb and using this tool for the first time ever, to program with this tool and correct my own errors?\u201c.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#give-more-arguments-to-the-agent\" target=\"_blank\" rel=\"noopener\"><\/a>Give more arguments to the agent<\/h3>\n\n\n\n<p>To pass some additional objects to your agent beyond the simple string describing the task, you can use the&nbsp;<code>additional_args<\/code>&nbsp;argument to pass any type of object:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import CodeAgent, HfApiModel\n\nmodel_id = \"meta-llama\/Llama-3.3-70B-Instruct\"\n\nagent = CodeAgent(tools=[], model=HfApiModel(model_id=model_id), add_base_tools=True)\n\nagent.run(\n    \"Why does Mike not know many people in New York?\",\n    additional_args={\"mp3_sound_file_url\":'https:\/\/huggingface.co\/datasets\/huggingface\/documentation-images\/resolve\/main\/transformers\/recording.mp3'}\n)<\/pre>\n\n\n\n<p>For instance, you can use this&nbsp;<code>additional_args<\/code>&nbsp;argument to pass images or strings that you want your agent to leverage.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#how-to-debug-your-agent\" target=\"_blank\" rel=\"noopener\"><\/a>How to debug your agent<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#1-use-a-stronger-llm\" target=\"_blank\" rel=\"noopener\"><\/a>1. Use a stronger LLM<\/h3>\n\n\n\n<p>In an agentic workflows, some of the errors are actual errors, some other are the fault of your LLM engine not reasoning properly. For instance, consider this trace for an&nbsp;<code>CodeAgent<\/code>&nbsp;that I asked to create a car picture:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">==================================================================================================== New task ====================================================================================================\nMake me a cool car picture\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 New step \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nAgent is executing the code below: \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nimage_generator(prompt=\"A cool, futuristic sports car with LED headlights, aerodynamic design, and vibrant color, high-res, photorealistic\")\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\nLast output from code snippet: \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\/var\/folders\/6m\/9b1tts6d5w960j80wbw9tx3m0000gn\/T\/tmpx09qfsdd\/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png\nStep 1:\n\n- Time taken: 16.35 seconds\n- Input tokens: 1,383\n- Output tokens: 77\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500 New step \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nAgent is executing the code below: \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nfinal_answer(\"\/var\/folders\/6m\/9b1tts6d5w960j80wbw9tx3m0000gn\/T\/tmpx09qfsdd\/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png\")\n\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\nPrint outputs:\n\nLast output from code snippet: \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\n\/var\/folders\/6m\/9b1tts6d5w960j80wbw9tx3m0000gn\/T\/tmpx09qfsdd\/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png\nFinal answer:\n\/var\/folders\/6m\/9b1tts6d5w960j80wbw9tx3m0000gn\/T\/tmpx09qfsdd\/652f0007-3ee9-44e2-94ac-90dae6bb89a4.png<\/pre>\n\n\n\n<p>The user sees, instead of an image being returned, a path being returned to them. It could look like a bug from the system, but actually the agentic system didn\u2019t cause the error: it\u2019s just that the LLM brain did the mistake of not saving the image output into a variable. Thus it cannot access the image again except by leveraging the path that was logged while saving the image, so it returns the path instead of an image.<\/p>\n\n\n\n<p>The first step to debugging your agent is thus \u201cUse a more powerful LLM\u201d. Alternatives like&nbsp;<code>Qwen2\/5-72B-Instruct<\/code>&nbsp;wouldn\u2019t have made that mistake.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#2-provide-more-guidance--more-information\" target=\"_blank\" rel=\"noopener\"><\/a>2. Provide more guidance \/ more information<\/h3>\n\n\n\n<p>You can also use less powerful models, provided you guide them more effectively.<\/p>\n\n\n\n<p>Put yourself in the shoes of your model: if you were the model solving the task, would you struggle with the information available to you (from the system prompt + task formulation + tool description) ?<\/p>\n\n\n\n<p>Would you need some added clarifications?<\/p>\n\n\n\n<p>To provide extra information, we do not recommend to change the system prompt right away: the default system prompt has many adjustments that you do not want to mess up except if you understand the prompt very well. Better ways to guide your LLM engine are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If it \u2018s about the task to solve: add all these details to the task. The task could be 100s of pages long.<\/li>\n\n\n\n<li>If it\u2019s about how to use tools: the description attribute of your tools.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#3-change-the-system-prompt-generally-not-advised\" target=\"_blank\" rel=\"noopener\"><\/a>3. Change the system prompt (generally not advised)<\/h3>\n\n\n\n<p>If above clarifications above are not sufficient, you can change the system prompt.<\/p>\n\n\n\n<p>Let\u2019s see how it works. For example, let us check the default system prompt for the\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/agents#smolagents.CodeAgent\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">CodeAgent<\/a>\u00a0(below version is shortened by skipping zero-shot examples).<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">print(agent.system_prompt_template)<\/pre>\n\n\n\n<p>Here is what you get:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">You are an expert assistant who can solve any task using code blobs. You will be given a task to solve as best you can.\nTo do so, you have been given access to a list of tools: these tools are basically Python functions which you can call with code.\nTo solve the task, you must plan forward to proceed in a series of steps, in a cycle of 'Thought:', 'Code:', and 'Observation:' sequences.\n\nAt each step, in the 'Thought:' sequence, you should first explain your reasoning towards solving the task and the tools that you want to use.\nThen in the 'Code:' sequence, you should write the code in simple Python. The code sequence must end with '&lt;end_code&gt;' sequence.\nDuring each intermediate step, you can use 'print()' to save whatever important information you will then need.\nThese print outputs will then appear in the 'Observation:' field, which will be available as input for the next step.\nIn the end you have to return a final answer using the `final_answer` tool.\n\nHere are a few examples using notional tools:\n---\n{examples}\n\nAbove example were using notional tools that might not exist for you. On top of performing computations in the Python code snippets that you create, you only have access to these tools:\n\n{{tool_descriptions}}\n\n{{managed_agents_descriptions}}\n\nHere are the rules you should always follow to solve your task:\n1. Always provide a 'Thought:' sequence, and a 'Code:\\n```py' sequence ending with '```&lt;end_code&gt;' sequence, else you will fail.\n2. Use only variables that you have defined!\n3. Always use the right arguments for the tools. DO NOT pass the arguments as a dict as in 'answer = wiki({'query': \"What is the place where James Bond lives?\"})', but use the arguments directly as in 'answer = wiki(query=\"What is the place where James Bond lives?\")'.\n4. Take care to not chain too many sequential tool calls in the same code block, especially when the output format is unpredictable. For instance, a call to search has an unpredictable return format, so do not have another tool call that depends on its output in the same block: rather output results with print() to use them in the next block.\n5. Call a tool only when needed, and never re-do a tool call that you previously did with the exact same parameters.\n6. Don't name any new variable with the same name as a tool: for instance don't name a variable 'final_answer'.\n7. Never create any notional variables in our code, as having these in your logs might derail you from the true variables.\n8. You can use imports in your code, but only from the following list of modules: {{authorized_imports}}\n9. The state persists between code executions: so if in one step you've created variables or imported modules, these will all persist.\n10. Don't give up! You're in charge of solving the task, not providing directions to solve it.\n\nNow Begin! If you solve the task correctly, you will receive a reward of $1,000,000.<\/pre>\n\n\n\n<p>As you can see, there are placeholders like&nbsp;<code>\"{{tool_descriptions}}\"<\/code>: these will be used upon agent initialization to insert certain automatically generated descriptions of tools or managed agents.<\/p>\n\n\n\n<p>So while you can overwrite this system prompt template by passing your custom prompt as an argument to the&nbsp;<code>system_prompt<\/code>&nbsp;parameter, your new system prompt must contain the following placeholders:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>\"{{tool_descriptions}}\"<\/code>\u00a0to insert tool descriptions.<\/li>\n\n\n\n<li><code>\"{{managed_agents_description}}\"<\/code>\u00a0to insert the description for managed agents if there are any.<\/li>\n\n\n\n<li>For\u00a0<code>CodeAgent<\/code>\u00a0only:\u00a0<code>\"{{authorized_imports}}\"<\/code>\u00a0to insert the list of authorized imports.<\/li>\n<\/ul>\n\n\n\n<p>Then you can change the system prompt as follows:<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents.prompts import CODE_SYSTEM_PROMPT\n\nmodified_system_prompt = CODE_SYSTEM_PROMPT + \"\\nHere you go!\" <em># Change the system prompt here<\/em>\n\nagent = CodeAgent(\n    tools=[], \n    model=HfApiModel(), \n    system_prompt=modified_system_prompt\n)<\/pre>\n\n\n\n<p>This also works with the\u00a0<a href=\"https:\/\/huggingface.co\/docs\/smolagents\/v1.2.2\/en\/reference\/agents#smolagents.ToolCallingAgent\" target=\"_blank\" rel=\"noreferrer noopener nofollow\">ToolCallingAgent<\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/huggingface.co\/docs\/smolagents\/tutorials\/building_good_agents#4-extra-planning\" target=\"_blank\" rel=\"noopener\"><\/a>4. Extra planning<\/h3>\n\n\n\n<p>We provide a model for a supplementary planning step, that an agent can run regularly in-between normal action steps. In this step, there is no tool call, the LLM is simply asked to update a list of facts it knows and to reflect on what steps it should take next based on those facts.<\/p>\n\n\n\n<p>Copied<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">from smolagents import load_tool, CodeAgent, HfApiModel, DuckDuckGoSearchTool\nfrom dotenv import load_dotenv\n\nload_dotenv()\n\n<em># Import tool from Hub<\/em>\nimage_generation_tool = load_tool(\"m-ric\/text-to-image\", trust_remote_code=True)\n\nsearch_tool = DuckDuckGoSearchTool()\n\nagent = CodeAgent(\n    tools=[search_tool],\n    model=HfApiModel(\"Qwen\/Qwen2.5-72B-Instruct\"),\n    planning_interval=3 <em># This is where you activate planning!<\/em>\n)\n\n<em># Run it!<\/em>\nresult = agent.run(\n    \"How long would a cheetah at full speed take to run the length of Pont Alexandre III?\",\n)<\/pre>","protected":false},"excerpt":{"rendered":"<p>There\u2019s a world of difference between building an agent that works and one that doesn\u2019t. How can we build agents that fall into the latter category? In this guide, we\u2019re going to see best practices for building agents. If you\u2019re new to building agents, make sure to first read the\u00a0intro to agents\u00a0and the\u00a0guided tour of&#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":[8],"doc_tag":[],"class_list":["post-589","docs","type-docs","status-publish","hentry","doc_category-tutorials"],"year_month":"2026-04","word_count":2268,"total_views":"5853","reactions":{"happy":"2","normal":"1","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":"Tutorials","term_url":"https:\/\/smolagents.org\/hi\/docs-category\/tutorials\/"}],"doc_tag_info":[],"_links":{"self":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/589","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=589"}],"version-history":[{"count":1,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/589\/revisions"}],"predecessor-version":[{"id":590,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/docs\/589\/revisions\/590"}],"wp:attachment":[{"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/media?parent=589"}],"wp:term":[{"taxonomy":"doc_category","embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/doc_category?post=589"},{"taxonomy":"doc_tag","embeddable":true,"href":"https:\/\/smolagents.org\/hi\/wp-json\/wp\/v2\/doc_tag?post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}