Hugging Face Smolagents: Building Powerful AI Agents Made Simple
The artificial intelligence landscape witnessed a significant development in 2024 with Hugging Face's introduction of SmoLAGents (Small Agents).
The world of AI is constantly evolving, and Hugging Face, a leading platform for AI resources, is at the forefront of innovation. Their recent introduction of Smolagents is a game-changer, represents a crucial step forward in making AI agents more accessible and efficient, particularly for developers and organisations working with limited computational resources. This article explores Smolagents, how they work, and why they're a significant step forward for developers and the future of AI.
What are AI Agents?
Before diving into Smolagents, it’s essential to understand the concept of AI agents. Imagine a highly skilled assistant that can not only follow your instructions but also anticipate your needs and take initiative. That’s essentially what an AI agent does. Unlike traditional programs, these intelligent systems leverage powerful AI - such as Large Language Models (LLMs) - to interpret the world around them and take appropriate actions.
AI agents typically consist of several key components:
Perception: The ability to sense and interpret information from the environment.
Decision-Making: The capacity to choose actions based on the perceived information and a set of goals.
Action: The ability to execute chosen actions within the environment.
Learning: The capability to improve performance over time through experience.
These agents can be used for a wide range of tasks, from playing games and controlling robots to automating complex processes and providing personalised assistance.
Introducing Smolagents: Simplicity Meets Power
Smolagents is a minimalist library that enables developers to build powerful agents with just a few lines of code. Its key features include:
Simplicity: The core logic of smolagents comprises approximately 1,000 lines of code, minimizing abstractions and promoting ease of understanding. 
LLM-Agnostic Support: It is compatible with various LLMs, including those hosted on Hugging Face’s Hub, OpenAI, and Anthropic, providing flexibility in model selection. 
Code Agents: Smolagents emphasises agents that write their actions in code, enhancing composability, object management, and generality. This approach leverages the inherent strengths of programming languages to express complex behaviors effectively. 
Hub Integrations: Developers can share and load tools directly from the Hugging Face Hub, fostering community collaboration and resource sharing. 
Error Handling: The package offers a robust error logging system that clearly highlights where the agents encounter issues, making it easier to identify and resolve problems.
Security Risks: Allowing LLMs to execute code poses vulnerabilities, requiring strong safeguards against malicious actions.
When to Use Smolagents
While smolagents provides powerful capabilities, it’s essential to assess whether an agentic approach is necessary for your application. For tasks with well-defined, deterministic workflows, traditional programming may suffice. However, for complex scenarios requiring adaptability and dynamic decision-making, smolagents can offer significant benefits. 
How Do Smolagents Work?
At its core, smolagents allows an LLM to generate code that performs specific actions, such as calling external tools or APIs. This is achieved through a loop where the agent determines the next action, executes it, observes the outcome, and decides on subsequent steps. This iterative process continues until the task is satisfactorily completed. 
To get started with Smolagents, you need to install the package via pip:
pip install smolagents
Next, log in to Hugging Face’s Hub using your access token:
from huggingface_hub import login
# Replace with your actual Hugging Face access token
hf_access_token = "YOUR_ACCESS_TOKEN"
login(hf_token)
You can then define an agent, specify the tools it can use, and execute a task. For instance, let’s create an agent that estimates how long it would take a leopard running at full speed to cross the Pont des Arts bridge in Paris:
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
agent = CodeAgent(tools=[DuckDuckGoSearchTool()], model=HfApiModel())
agent.run("How many seconds would it take for a leopard at full speed to run through Pont des Arts?")
This script sets up a CodeAgent with access to a search tool and an LLM model, then runs a query to compute the desired information. 
Breakdown of the Example:
Tools: The DuckDuckGoSearchTool retrieves relevant information, such as the leopard’s speed and the bridge’s length.
Model: The HfApiModel connects to a language model hosted on the Hugging Face Hub to generate and process actions.
Agent: The CodeAgent iteratively breaks down the problem, fetches data, and computes the result.
Sample Output: The agent output:
A leopard running at full speed (58 km/h) would take approximately 9.6 seconds to cross the Pont des Arts bridge (155 meters).
Why Choose Smolagents?
Smolagents offers several advantages for developers:
Efficiency: By allowing agents to write actions in code, smolagents reduces the number of steps and LLM calls required, enhancing performance.
Security: It supports secure code execution environments, mitigating risks associated with running generated code.
Flexibility: The library’s compatibility with multiple LLMs and tools allows developers to tailor agents to specific needs and contexts.
Real world use case: Resolving Customer Queries in E-Commerce
Consider a scenario where an AI agent automates customer support by providing accurate and personalised answers to queries. For example, the agent retrieves product details from a database, checks shipping status via an API, and calculates estimated delivery times for customers.
Here’s a simplified implementation:
from smolagents import CodeAgent, DuckDuckGoSearchTool, HfApiModel
# Step 1: Define custom tools as functions
def get_shipping_status(order_id: str):
"""Simulate retrieving shipping status from a database or API."""
shipping_data = {
"123": {"status": "Shipped", "carrier": "DHL", "days_left": 3},
"124": {"status": "Processing", "carrier": None, "days_left": None},
}
result = shipping_data.get(order_id, {"status": "Not Found", "carrier": None, "days_left": None})
return result
def calculate_delivery_estimate(shipping_status: str, days_left: int):
"""Calculate delivery estimate based on shipping status."""
if shipping_status == "Shipped" and days_left:
return f"The package is expected to arrive in {days_left} days."
elif shipping_status == "Processing":
return "The order is still being processed and does not have a delivery estimate yet."
else:
return "Delivery status could not be determined."
# Step 2: Create an agent using smolagents
tools = [
DuckDuckGoSearchTool() # To fetch external information (if needed)
]
model = HfApiModel()
agent = CodeAgent(tools=tools, model=model)
# Step 3: Define a customer query workflow
def handle_customer_query(query: str):
# Extract order ID from the query (mock implementation for simplicity)
order_id = "123" if "123" in query else "124" # Replace with NLP-based extraction if needed
# Get shipping status
shipping_status_data = get_shipping_status(order_id)
shipping_status = shipping_status_data["status"]
carrier = shipping_status_data["carrier"]
days_left = shipping_status_data["days_left"]
# Calculate delivery estimate
delivery_estimate = calculate_delivery_estimate(shipping_status, days_left)
# Generate a conversational response using OpenAI tool
response_prompt = (
f"Customer Query: {query}\n"
f"Shipping Status: {shipping_status}\n"
f"Carrier: {carrier}\n"
f"Delivery Estimate: {delivery_estimate}\n"
f"Generate a customer-friendly response based on these details."
)
response = agent.run(response_prompt)
return response
# Step 4: Simulate a customer query
customer_query = "I ordered a phone with order ID 123. Can you let me know the status and delivery time?"
agent_response = handle_customer_query(customer_query)
# Step 5: Display the final response
print(f"\n[Agent's Response]:\n{agent_response}")
How This Works in an industry environment
E-Commerce Integration: This agent can be embedded into customer support systems, helping reduce human workload and improving response times.
Scalability: Tools like ShippingStatusTool can be extended to connect directly with APIs from logistics companies (e.g., FedEx, UPS).
Customisation: The agent can be trained to handle a wide range of customer queries, from product availability to return policies, using appropriate tools and models.
Benefits of Using Smolagents in This Case
Efficiency: Automates repetitive tasks in customer support.
Flexibility: Easily integrates with third-party APIs and internal databases.
Cost-Effective: Reduces dependency on large-scale conversational systems by using targeted tools for specific tasks.
This example demonstrates how smolagents can enable businesses to build lightweight, efficient, and practical AI-driven solutions for real-world challenges.
Conclusion
Hugging Face’s Smolagents represents a significant advancement in simplifying AI agent development. With its focus on minimalism, flexibility, and the power of code, Smolagents empowers developers to build efficient, adaptable systems tailored to diverse applications.
References
Hugging Face: Introducing smolagents: simple agents that write actions in code.
Hugging Face: smolagents: a barebones library for agents.
InfoQ.: Hugging Face Smolagents is a Simple Library to Build LLM-Powered Agents.
Hugging Face: Introduction to Agents
Note: As Smolagents are a very recent development, specific documentation and examples may evolve rapidly. It's always best to refer to the official Hugging Face resources for the most up-to-date information.