smolagents is a lightweight Python framework developed by Hugging Face for building AI agents. An AI agent can use a Large Language Model (LLM) to understand a task, choose actions, use tools, and work toward a result.
For example, a plant shop chatbot could use an AI agent to look up plant prices and recommend suitable plants to customers.
smolagents offers several advantages:
It is a good choice when you want to experiment with AI agents without building a complex system from scratch.
smolagents supports two main agent types.
CodeAgent generates Python code to perform actions. For example, it can write a loop to retrieve prices from multiple countries and find the cheapest option.
ToolCallingAgent generates structured tool calls, typically using JSON, to request specific actions. For example, it can call a get_plant_price tool to retrieve a plant’s price.
The main difference is how they represent actions: CodeAgent uses Python code, while ToolCallingAgent uses structured tool calls. Both can use tools — they differ in how actions are expressed.
smolagents supports different ways to connect language models, including:
TransformersModel for models running through Transformers.InferenceClientModel for Hugging Face inference services.LiteLLMModel for connecting to multiple model providers.OpenAIServerModel for services with an OpenAI-compatible API.This flexibility allows developers to choose a model based on their requirements and budget.
Tools allow an agent to interact with external systems or perform specific tasks. In smolagents, you can define a tool using the @tool decorator. For example:
from smolagents import tool@tooldef get_plant_price(plant_name: str) -> str:"""Get the price of a plant."""return f"The price of {plant_name} is 500000 VND."
An agent can use this function when it needs to retrieve a plant’s price. In a real application, the function could call an API or query a database instead.
smolagents is useful when you want to:
However, applications with complex workflows may require additional architecture and orchestration.