Today we are launching smolagents, a very simple library that unlocks agentic capabilities for language models. Here’s a glimpse:.

Sources indicate that any efficient system using AI will need to provide LLMs some kind of access to the real world: for instance the possibility to call a search tool to get external information, or to act on certain programs in order to solve a task. In other words, LLMs should have agency. Agentic programs are the gateway to the outside world for LLMs.

AI Agents are programs where LLM outputs control the workflow. (Related: Tiny Agents: an MCP-powered agent in 50 lines of code...)

Any system leveraging LLMs will integrate the LLM outputs into code. The influence of the LLM's input on the code workflow is the level of agency of LLMs in the system.

Note that with this definition, "agent" is not a discrete, 0 or 1 definition: instead, "agency" evolves on a continuous spectrum, as you give more or less power to the LLM on your workflow. (Related: Aligning to What? Rethinking Agent Generalization in Mi...)

The table below illustrates how agency varies across systems:.

The multi-step agent has this code structure:.

So this system runs in a loop, executing a upcoming 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:.

Agents are useful when you need an LLM to determine the workflow of an app. But they’re often overkill. The question is: do I really need flexibility in the workflow to efficiently solve the task at hand? If the pre-determined workflow falls short too often, that means you need more flexibility. Let's take an example: say you're making an app that handles customer requests on a surfing trip website.

You could know in advance that the requests will belong to either of 2 buckets (based on user choice), and you have a predefined workflow for each of these 2 cases.

Industry observers note that if that deterministic workflow fits all queries, by all means just code everything! This will give you a 100% reliable system with no risk of error introduced by letting unpredictable LLMs meddle in your workflow. For the sake of simplicity and robustness, it's advised to regularize towards not using any agentic behaviour.

But what if the workflow can't be determined that well in advance?

For instance, a user wants to ask : "I can come on Monday, but I forgot my passport so risk being delayed to Wednesday, is it possible to take me and my stuff to surf on Tuesday morning, with a cancellation insurance?" This question hinges on many factors, and probably none of the predetermined criteria above will suffice for this request.

If the pre-determined workflow falls short too often, that means you need more flexibility.

That is where an agentic setup helps.

In the above example, you could just make a multi-step agent that has access to a weather API for weather forecasts, Google Maps API to compute travel distance, an employee availability dashboard and a RAG system on your knowledge base.

Until recently, computer programs were restricted to pre-determined workflows, trying to handle complexity by piling up if/else switches. They focused on extremely narrow tasks, like "compute the sum of these numbers" or "find the shortest path in this graph". But actually, most real-life tasks, like our trip example above, do not fit in pre-determined workflows. Agentic systems open up the vast world of real-world tasks to programs!

In a multi-step agent, at each step, the LLM can write an action, in the form of some calls to external tools. A common format (used by Anthropic, OpenAI, and many others) for writing these actions is generally different shades of "writing actions as a JSON of tools names and arguments to use, which you then parse to know which tool to execute and with which arguments".

Multiple research papers have shown that having the tool calling LLMs in code is much better.

Recent findings suggest that 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:.

Writing actions in code rather than JSON-like snippets provides better:.

We built smolagents with these objectives:.

✨ Simplicity: the logic for agents fits in ~thousands lines of code (see this file). We kept abstractions to their minimal shape above raw code!

🧑‍💻 First-class support for Code Agents, i.e. agents that write their actions in code (as opposed to "agents being used to write code"). To make it secure, we support executing in sandboxed environments via E2B.

🤗 Hub integrations: you can share and load tools to/from the Hub, and more is to come!

🌐 Support for any LLM: it supports models hosted on the Hub loaded in their transformers version or through our inference API, but also supports models from OpenAI, Anthropic and many others via our LiteLLM integration.

smolagents is the successor to transformers.agents, and will be replacing it as transformers.agents gets deprecated in the future.

To build an agent, you need at least two elements:.

For the model, you can use any LLM, either open models using our HfApiModel class, that leverages Hugging Face's free inference API (as shown in the leopard example above), or you can use LiteLLMModel to leverage litellm and pick from a list of 100+ different cloud LLMs.

For the tool, you can just make a function with type hints on inputs and outputs, and docstrings giving descriptions for inputs, and use the @tool decorator to make it a tool.

Here’s how to make a custom tool that gets travel times from Google Maps, and how to use it into a travel planner agent:.

After a few steps of gathering travel times and running calculations, the agent returns this final proposition:.

After building a tool, sharing it to the Hub is as simple as:.

In a recent development, you can see the result under this space. You can check the logic for the tool under the file tool.py in the space. As you can see, the tool was actually exported to a class inheriting from class Tool, which is the underlying structure for all our tools.

We've created CodeAgent instances with some leading models, and compared them on this benchmark that gathers questions from a few different benchmarks to propose a varied blend of challenges.

Find the benchmark here for more detail on the agentic setup used, and see a comparison of code agents versus tool calling agents (spoilers: code works better).

This comparison demonstrates that open source models can now take on the best closed models!

thanks for clarifying, what actually agents are,this is a great post btw, using agents to solve real-world problems is going to be interesting.

Sources indicate that /me went ahead and made a simple agent that fetches a random XKCD comic. Give it a look! 😄 👉 http://github.com/hemanth/notebooks/blob/main/notebooks/smolagents.ipynb.

Hi Great Project by the way but i am having a small problem,.

Sources indicate that I'm using smolagents with a QWEN 32B model (via CodeAgent) and have run into an glitch with the generated code. Sometimes, the code produced by the agent includes multiple calls to final_answer, with one appearing in the middle of the script. Because final_answer is designed to signal the end of execution, any code following its first occurrence (for example, calls like task_to_do()) doesn't get executed.

In a recent development, question:Is there an agent configuration, workaround, or best practice within smolagents that can allow all code to execute even if final_answer is called early? Any guidance or suggestions would be greatly appreciated.

This is great and got to know this by doing Agents course.

Thank you for the introduction to this area in a way that leaves the rabbit hole ready to go down!

It is so easy for learners, and they can polish their skill set accordingly.

Yes, InferenceClientModel has replaced the HfApiModel in newer versions of smolagents.You can use HfApiModel with older versions of smolagents.

→ Read original article