Getting Started with HuggingFace Smolagents

Written by on March 12, 2025 in Agents, OpenAI, Programming, Python with 0 Comments

Agents and Agent frameworks are hot topics these days. LangChain, crewAI, LangGraph, Microsoft Semantic Kernel, and Microsoft Autogen are some of the popular agent frameworks. Smolagents is a relatively new entry in this arena. It is a lightweight agent framework from the well-known HuggingFace platform.

In today’s article, I want to show how easy it is to get started with Smolagents. The example is about interacting with an LLM regarding imaginary universes!

Agents are built on top of “tools” and use the underlying “function calling” feature of the LLM. One of the nice features of Smolagents is “CodeAgent”, which supports the tool calling LLMs in code.

Python functions are converted to tools using the “@tool” decorator. See the figure below:

Defining Tools

Defining Tools

As is customary, we provide appropriate docstrings that describe the inputs and output.

The rest of the code how to create the CodeAgent and execute it. See below:

Creating and Using the CodeAgent

Creating and Using the CodeAgent

Another nice thing about Smolagents is its support for a variety of LLMs, not just those hosted on HuggingFace. In the above code, we can change the LLM by switching between LiteLLMModel and HfApiModel objects.

Here is the output from the program:

Program Output

Program Output

You can see that the relevant tools are correctly called for the “universe” related questions. But for getting the population of Maldives, the “getPopulation” tool is not called!

This is just a basic example of how we can use Smolagents. It has other types of agents such as ToolCallingAgent, ManagedAgent, and MultiStepAgent, each of which has a specific role. I hope to write about those in future articles.

You can download the code here.

Have a nice week!

Tags: , ,

About the Author

About the Author: .

Subscribe

If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *

Top