Artificial Intelligence is evolving, and so are the ways we integrate it into real-world applications. One key advancement is the rise of AI agents—programs that allow language models to interact dynamically with the external world. Today, we introduce Smolagents, a lightweight yet powerful library designed to simplify agentic capabilities for LLMs.
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?")
AI agents provide a way for language models to act rather than just respond. Traditional AI workflows follow predefined paths, but real-world problems often demand flexibility. Agents introduce a level of autonomy, allowing LLMs to execute functions, call external APIs, and even make iterative decisions based on their outputs.
Rather than treating AI as a mere tool for processing text, agentic systems enable it to:
Fetch real-time data via external sources (e.g., search engines, APIs)
Automate workflows by making informed decisions
Execute tasks iteratively until an objective is achieve
Unlike traditional AI models that follow static, predefined rules, AI agents introduce flexibility and decision-making capabilities. They interact dynamically with their environment, making them ideal for tasks that require:
Real-time data retrieval (e.g., search engines, APIs)
Automated, multi-step workflows
Iterative task execution based on evolving inputs
Smolagents provides a streamlined interface to implement these capabilities with ease.
Agency Level | Description | How that's called | Example Pattern |
---|---|---|---|
☆☆☆ | LLM output has no impact on program flow | Simple processor | process_llm_output(llm_response) |
★☆☆ | LLM output determines basic control flow | Router | if llm_decision(): path_a() else: path_b() |
★★☆ | LLM output determines function execution | Tool call | run_function(llm_chosen_tool, llm_chosen_args) |
★★★ | LLM output controls iteration and program continuation | Multi-step Agent | while llm_should_continue(): execute_next_step() |
★★★ | One agentic workflow can start another agentic workflow | Multi-Agent | if llm_trigger(): execute_agent() |
The multi-step agent has this code structure:
# memory = [user_defined_task] while llm_should_continue(memory): # this loop is the multi-step part action = llm_get_next_action(memory) # this is the tool-calling part observations = execute_action(action) memory += [action, observations]
So this system runs in a loop, executing a new action at each step (the action can involve calling some pre-determined tools that are just functions), until its observations make it apparent that a satisfactory state has been reached to solve the given task. Here’s an example of how a multi-step agent can solve a simple math question:
Before diving into Smolagents, it’s important to determine when an agentic approach makes sense.
Use an AI agent when a task requires dynamic, context-driven decisions. Some examples include:
#Automated customer support: Handling complex, multi-step queries that can't be pre-categorized.
#AI-powered research assistants: Gathering information from various sources and synthesizing insights.
#Intelligent workflow automation: Executing different processes based on variable inputs.
For fixed, rule-based processes, traditional systems are often more efficient. Examples include:
Simple data lookups (e.g., FAQ retrieval)
Static form submissions
Basic data transformations
In such cases, adding agency would be overkill and could introduce unnecessary complexity.
The reason for this is simply that we crafted our code languages specifically to be the best possible way to express actions performed by a computer. If JSON snippets were a better expression, JSON would be the top programming language and programming would be hell on earth.
The figure below, taken from Executable Code Actions Elicit Better LLM Agents, illustrates some advantages of writing actions in code:
To create an AI agent using Smolagents, you need:
A model: The LLM that powers decision-making.
Tools: External functions or APIs the agent can call.
Here’s how to build a simple travel planning agent using Smolagents:
# from typing import Optional from smolagents import CodeAgent, HfApiModel, tool import googlemaps import os from datetime import datetime @tool def get_travel_duration(start_location: str, destination_location: str, transportation_mode: Optional[str] = "driving") -> str: """Calculates travel time between two locations.""" gmaps = googlemaps.Client(os.getenv("GMAPS_API_KEY")) directions_result = gmaps.directions( start_location, destination_location, mode=transportation_mode, departure_time=datetime(2025, 6, 6, 11, 0) ) if not directions_result: return "No route found." return directions_result[0]["legs"][0]["duration"]["text"] agent = CodeAgent(tools=[get_travel_duration], model=HfApiModel(), additional_authorized_imports=["datetime"]) result = agent.run("Plan a one-day trip around Paris with estimated travel times by bicycle.") print(result)
This agent fetches travel durations via Google Maps API and generates an optimized itinerary dynamically.
Smolagents supports tool sharing via the Hugging Face Hub. To share your custom tool, simply push it:
get_travel_duration.push_to_hub("{your_username}/get-travel-duration-tool")
# Smolagents supports tool sharing via the Hugging Face Hub. To share your custom tool, simply push it: get_travel_duration.push_to_hub("{your_username}/get-travel-duration-tool")
This enables easy reuse and collaboration across different AI applications!
To evaluate Smolagents' effectiveness, we benchmarked different models in multi-step agentic tasks. The results indicate that open-source models are now capable of rivaling proprietary LLMs in structured decision-making tasks.
For more details, check out the Smolagents Benchmark, where we compare different model performances in agent-driven environments.
This comparison shows that open source models can now take on the best closed models!
Ready to build your own AI agents? Here’s where to begin:
Try the guided tour – A hands-on introduction to Smolagents.
Explore tutorials – Learn best practices for tool integration and workflow optimization.
Experiment with examples – Implement agentic solutions like AI-powered text-to-SQL or multi-agent orchestration.
For further reading, check out Anthropic’s excellent blog post on AI agents or explore the latest research papers in the field.
This excellent blog post by gives Anthropic general knowledge.
This collection gathers the most impactful research papers on agents.
Smolagents is a step towards making AI more adaptable, intelligent, and useful in real-world applications. By bridging the gap between LLMs and action-driven workflows, it unlocks new possibilities for automation, research, and decision-making.
Let’s build the future—one agent at a time! 🚀