Image by Author |Ideogram
AI agents have become the hottest topic thanks to their strong LLM reasoning capability. These agents can make decisions independently while following the user’s input.
For you who don’t know what AI agents is, you can define it as a program where LLM (Large Language Model) outputs control workflows. The intuition behind AI agents is that we can delegate tasks to machines that were previously unable to be done.
With the agent trend rising, many AI agent frameworks are emerging. Amid the sea of agent framework, you should know about the smolagents.
So, what are smolagents? And how will it help your work?
Smolagents Introduction
Smolagents is an AI agent framework recently launched by the Hugging Face team to simplify the process of developing AI agents.
It’s a lightweight library that prioritizes practicality. This means it can build AI agents in a few lines of code but focuses more on simple implementation than creating the whole agent system in production.
When you are using smolagents, there are a few features you can expect. They include:
- It is simple to use as it can perform complex agent logic with a few lines of code.
- It developed as a code-centric agent, enabling agents to perform their actions directly with Python code.
- Smolagents are compatible with many LLMs available on the Hugging Face Hub. It also supports various popular models via LiteLLM.
- The framework supports several modality inputs, such as text, vision, or audio.
- Able to use all the tools available in the Hugging Face Hub and Hub Spaces. It’s also compatible with tools that come from frameworks such as LangChain.
Given how valuable smolagents are for developing AI agents, let’s try the code implementation to understand them better.
Code Implementation
Let’s start the coding tutorial by installing the required code.
pip install smolagents huggingface_hub transformers
For this tutorial, you will also need a HuggingFace access token, which you will use to log in to HuggingFace from your notebook.
from huggingface_hub import login
login('YOUR-HF-API-KEY')
Let’s try building simple agents with Smolagent that can access tools. We will use the free inference model from HuggingFace and the web searching tool.
It’s as simple as importing the library and passing the query we want to execute to do the above. We can structure it like the code below.
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
agent.run("What are the best route to travel from New York to Paris without using Airplane and how long does it take?"
The result is shown in the output below.
Step 2: Duration 15.63 seconds| Input tokens: 10,268 | Output tokens: 320]
'Transatlantic cruises from New York to Paris typically take around 10 to 12 days, depending on the cruise line and itinerary. The journey provides a unique opportunity to experience the Atlantic Ocean and visit ports of call along the way
You can see that the smolagents can develop agents that access the web to search for the necessary query and produce the correct output.
The details above show that smolagents obtain the result through code execution. That’s why the default LLM used is the Qwen-2.5-Coder-32B-Instruct, which is decisive for code execution.
To use another LLM model, explore the Hugging Face Hub and find the model ID. Then, you can pass it to the model.
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel(model_id=model_id))
You will then run it like normal, with different models all around.
Access to tools is essential for agents to be much more powerful. With smolagents, you can develop any kind of tool that you need.
For example, here’s how to develop a tool to check whether the number we pass is prime.
from smolagents import Tool
class PrimeCheckTool(Tool):
name = "prime_check"
description = """
This is a tool that checks if a given number is a prime number.
It returns True if the number is prime, and False otherwise.
"""
inputs =
"number":
"type": "integer",
"description": "The number to check for primality.",
output_type = "boolean"
def forward(self, number: int) -> bool:
"""
Check if a number is a prime number.
Args:
number: The number to check.
Returns:
True if number is a prime number, False otherwise.
"""
if number < 2:
return False
for i in range(2, int(number**0.5) + 1):
if number % i == 0:
return False
return True
prime_check_tool = PrimeCheckTool()
We can then pass the tool above into the smolagent for the system to use with the code below.
agent = CodeAgent(tools=[prime_check_tool], model = HfApiModel() )
agent.run("Check if the number 23 is a prime number and provide the result in a nursery rhyme way.")
You can see that the agent can use the tool we pass and the complete log of what happens until we receive the result.
The smolagents Python interpreter also doesn’t allow imports outside the predefined safe list. We need to set which additional libraries the agent can access manually. For example, the code below will enable the agent to make requests and use the bs4 library.
agent = CodeAgent(tools=[], model=HfApiModel(), additional_authorized_imports=['requests', 'bs4'])
agent.run("Could you get me the title of the page at url 'https://www.kdnuggets.com/5-ai-agent-frameworks-compared'?. Don't use tools")
Lastly, smolagents allow the implementation of a Multi-Agent System (MAS) that would enable multiple agents to interact and collaborate to solve much more complex tasks. The system can achieve much better results using several specialized agents.
For example, we can initiate two web search agents and check prime numbers. Then, we set up a manager agent to manage both agents. The whole code implementation is shown below.
from smolagents import CodeAgent, HfApiModel, DuckDuckGoSearchTool, ToolCallingAgent
model = HfApiModel()
web_agent = ToolCallingAgent(tools=[DuckDuckGoSearchTool()],
model=model,
name="web_search",
description="Runs web searches for you. Give it your task as an argument.")
prime_check_agent = ToolCallingAgent(tools=[prime_check_tool],
model=model,
name="prime_chek",
description="Check whether the number is prime or not. Give it your number as an argument.")
manager_agent = CodeAgent(
tools=[], model=model, managed_agents=[web_agent, image_gen_agent],
)
With the smolagents MAS ready, we can pass our query to the manager agent for further processing.
manager_agent.run("Who is the elected president of United States in 2024 and does the age of that president during elected is prime number or not?")
After using all the code, the final output is similar to the below output.
The elected president of the United States in 2024 is Donald Trump, and his age during the election (78) is not a prime number.
You can always add another agent to the tools used for each agent. This powerful yet simple system can help you work.
Conclusion
Hugging Face recently released a lightweight library called smolagents for developing AI agents. This simple yet powerful framework allows for minimal code development of an AI agent system. In this article, we discuss the strengths of the smolagent and the code’s implementation.
I hope this has helped!
Cornellius Yudha Wijaya is a data science assistant manager and data writer. While working full-time at Allianz Indonesia, he loves to share Python and data tips via social media and writing media. Cornellius writes on a variety of AI and machine learning topics.