LLMs have now exploded of their use throughout numerous domains. They’re now not restricted to chatbots hosted on the net however are being built-in into enterprises, authorities companies, and past. A key innovation on this panorama is constructing customized instruments for AI brokers utilizing smolagents, permitting these techniques to increase their capabilities. Utilizing smolagents, AI brokers can leverage instruments, take actions in outlined environments, and even name different brokers.
This workflow permits LLM-powered AI techniques to function with larger autonomy, making them extra dependable for reaching full end-to-end process completion.
Studying Targets
- Study what AI brokers are, how they differ from conventional LLMs, and their function in fashionable AI purposes with customized instruments for LLM brokers.
- Uncover why AI brokers want customized instruments for LLM brokers to fetch real-time information, execute actions, and improve decision-making.
- Acquire hands-on expertise in integrating and deploying AI brokers utilizing smolagents for real-world purposes.
- Perceive the right way to create and combine customized instruments that AI brokers can invoke for enhanced performance utilizing smolagents.
- Learn to host and work together with an AI agent that makes use of the instruments you constructed, enabling a extra interactive and clever chatbot expertise.
This text was printed as part of the Information Science Blogathon.
Stipulations
That is an article meant for the intermediate-level builders and information professionals who’re nicely versed in utilizing fundamental LLMs. The next are anticipated:
- You know the way to code in Python in intermediate stage
- You realize the fundamentals of utilizing LLMs in your code
- You’re acquainted with the broader GenAI ecosystem
- You realize the very fundamentals of the Hugging Face platform and the `transformers` library in Python
These are the naked minimal that’s anticipated of you to study from this tutorial, however listed here are additional beneficial background so that you can profit totally from this tutorial:
- You need to use LLM libraries comparable to LangChain, Ollama, and so forth.
- You realize the fundamentals of Machine Studying principle
- You need to use an API in your code, and clear up issues utilizing API responses
Fundamentals of Brokers in Generative AI
You’re most likely acquainted with ChatGPT. You’ll be able to ask inquiries to it, and it solutions your questions. It might probably additionally write code for you, let you know a joke, and so forth.
As a result of it may well code, and it may well reply your questions, you would possibly wish to use it to finish duties for you, too. The place you demand one thing from it, and it completes a full process for you.
Whether it is obscure for you proper now, don’t fear. Let me provide you with an instance. You realize LLMs can search the online, and so they can cause utilizing data as enter. So, you may mix these capabilities collectively, and ask an LLM to create a full journey itinerary for you. Proper?
Sure. You’ll ask one thing like, “Hey AI, I’m planning a trip from 1st April to seventh April. I wish to go to the state of Himachal Pradesh. I actually like snow, snowboarding, rope-ways, and plush inexperienced panorama. Can you propose an itinerary for me? Additionally discover the bottom flight prices for me from the Kolkata airport.”
Taking on this data an agent ought to be capable to discover and evaluate all flight prices of these days inclusive, together with return journey, and which locations you need to go to given your standards, and lodges and prices for every place.
Right here, the AI mannequin is utilizing your given standards to work together with the actual world to seek for flights, lodges, buses, and so forth., and in addition recommend you locations to go to.
That is what we name agentic strategy in AI. And let’s study extra about it.
Workflow of an Agent
The agent is predicated on an LLM and LLM can work together with the exterior world utilizing solely textual content. Textual content in, textual content out.

So, after we ask an agent to do one thing, it takes that enter as textual content information, and it causes utilizing textual content/language, and it may well solely output textual content.
It’s within the center half or the final half the place using instruments are available. The instruments return some desired values, and utilizing these values the agent returns the response in textual content. It might probably additionally do one thing very totally different, like making a transaction on the inventory market, or generate a picture.

The workflow of an AI agent ought to be understood like this:
Perceive –> Purpose –> Work together
That is one step of an agentic workflow, and when a number of steps are concerned, like in most use instances, it ought to be seen as:
Thought –> Motion –> Commentary
Utilizing the command given to the agent, it thinks in regards to the process at hand, analyzes what must be achieved (Thought), after which it acts in the direction of the completion of the duty (Motion), after which it observes if any additional actions are wanted to be carried out, or how full the entire process is (Commentary).
On this tutorial, we’ll code up a chat agent the place we’ll ask it to greet the consumer in accordance with the consumer’s time zone. So, when a consumer says, “I’m in Kolkata, greet me!”, the agent will take into consideration the request, and parse it fastidiously. Then it would fetch the present time in accordance with the timezone, that is the motion. After which, it would observe for additional process, whether or not the consumer have requested a picture. If not, then it would go on and greet the consumer. In any other case, it would additional take motion invoking the picture technology mannequin.
Components of an AI Agent
To this point, we had been speaking in conceptual phrases, and workflow. Now lets take a dive into the concrete parts of an AI agent.

You’ll be able to say that an AI agent has two elements:
- the mind of the agent
- the instruments of that agent
The mind of the agent is a standard LLM mannequin like llama3, phi4, GPT4, and so forth. Utilizing this, the agent thinks and causes.
The instruments are externally coded instruments that the agent can invoke. It might probably name an API for a inventory value or the present temperature of a spot. Even have one other agent that it may well invoke. It may also be a easy calculator.
Utilizing `smolagents` framework, you may create any perform in Python with any AI mannequin that has been tuned for perform calling.
In our instance, we can have a software to inform the consumer a enjoyable reality a couple of canine, fetch the present timezone, and generate a picture. The mannequin will likely be a Qwen LLM mannequin. Extra on the mannequin later.
They’re not merely used as text-completion instruments and answering questions in Q&A codecs. They’re now used as small however however essential cogs in a lot bigger techniques the place many components of these techniques aren’t primarily based on Generative AI.
Under is an summary idea picture:

On this summary system graph, we see that GenAI parts usually should take necessary inputs from non-Generative AI conventional system parts.
We want instruments to work together with these part and never the reply that’s current in an LLM’s information base.
As now we have seen that LLM fashions function the “mind” of the agent, the agent will inherit all of the faults of LLMs as nicely. A few of them are:
- Many LLMs have a information closing date, and also you would possibly want up to date data like present climate, and inventory value information. Otherwise you would possibly want details about geopolitical developments.
- LLMs usually hallucinate information. For deployed purposes, you want your brokers to be 100% right about any reply. LLMs usually fail to reply some easy Math issues.
- LLMs usually refuse to reply about questions for non-obvious causes, like, “As a Giant Language Mannequin, I can’t reply this query”
- LLMs that may do a web-search use their picks of internet sites, however as an skilled in a site, you would possibly favor outcomes from some web sites over others.
The above are just some causes to make use of deterministic instruments.
The `smolagents` Library
`smolagents` is a library used as a framework for utilizing brokers in your LLM software. It’s developed by HuggingFace, and it’s Open Supply.
There are different frameworks comparable to LlamaIndex, LangGraph, and so forth. that you should utilize for a similar function. However, for this tutorial, we’ll deal with smolagents alone.
There are some libraries that create brokers that output JSON, and there are some libraries that output Python code instantly. Analysis has proven this strategy to be far more sensible and environment friendly. smolagents is a library that creates brokers that output Python code instantly.
Our Codebase
All code can be found on the GitHub repository for the undertaking. I cannot undergo all of the code there, however I’ll spotlight crucial items of that codebase.
- The Gradio_UI.py file holds the code for the UI library Gradio utilizing which the agent interacts with the consumer.
- The agent.json file has the configuration of the file
- necessities.txt has the necessities of the undertaking.
- The prompts.yaml file has the instance prompts and instance required for the agent to carry out actions. We’ll speak extra about it later.
- The core of the app lies within the app.py file. We’ll focus on largely about this file.
The prompts.yaml file include many instance duties and responses codecs we anticipate the mannequin to see. It additionally makes use of Jinja templating. It will get added to the immediate that we finally ship to the mannequin. We’ll later see that the prompts are added to the `CodeAgent` class.
A Fast Observe on Code Agent
Instrument-calling brokers can work in two ways- they will both return a JSON blob, or they will instantly write code.
It’s obvious that if the tool-calling agent makes use of code instantly, it’s significantly better in follow. It additionally saves you the overhead of getting the system to parse the JSON within the center.
`smolagents` library falls within the second class of LLM brokers, i.e. it makes use of code instantly.
The app.py file
That is the file the place we create the agent class, and that is the place we outline our personal instruments.
These are the imports:
from smolagents import CodeAgent,DuckDuckGoSearchTool, HfApiModel,load_tool,software
import datetime
import requests
import pytz
import yaml
from instruments.final_answer import FinalAnswerTool
We’re importing `CodeAgent` class from the `smolagents` library. Additionally importing `load_tool` and `software` lessons. We’ll use these in time.
We wish to name an API that has saved cool details about canine. It’s hosted on https://dogapi.canine. You’ll be able to go to the web site and skim the docs about utilizing the API. It’s utterly free.
To make a Python perform usable by the AI agent, it’s a must to:
- add the `@software` decorator to a perform
- have a really clear docstring describing the perform with clear descriptions of the arguments
- add kind annotations to the perform, for each inputs and return kind of the perform
- clearly return one thing
- add as a lot feedback as you may
@software
def get_amazing_dog_fact()-> str:
"""A software that tells you a tremendous reality about canine utilizing a public API.
Args: None
"""
# URL for the general public API
url = "https://dogapi.canine/api/v2/details?restrict=1"
# case when there's a response from the API
strive:
#
response = requests.get(url)
if response.status_code == 200: # excpected, okay standing code
# parsing standing code
cool_dog_fact = response.json()['data'][0]['attributes']['body']
return cool_dog_fact
else:
# in case of an unfavorable standing code
return "A canine reality couldn't be fetched."
besides requests.exceptions.RequestException as e:
return "A canine reality couldn't be fetched."
Observe that we’re returning a correctly parsed string as the ultimate reply.

Instrument to get present time
Under is a software to get the present time in a timezone of your alternative:
@software
def get_current_time_in_timezone(timezone: str) -> str:
"""A software that fetches the present native time in a specified timezone.
Args:
timezone: A string representing a sound timezone (e.g., 'America/New_York').
"""
strive:
# Create timezone object
tz = pytz.timezone(timezone)
# Get present time in that timezone
local_time = datetime.datetime.now(tz).strftime("%Y-%m-%d %H:%M:%S")
return f"The present native time in {timezone} is: {local_time}"
besides Exception as e:
return f"Error fetching time for timezone '{timezone}': {str(e)}"
You may as well use different instruments which might be different AI fashions, like this:
image_generation_tool = load_tool("agents-course/text-to-image", trust_remote_code=True)
Now, these are the instruments on the agent’s disposal. What in regards to the mannequin? We’re going to use the Qwen2.5-Coder-32B-Instruct mannequin. You must apply to have the ability to use this mannequin. They’re fairly open about granting entry.
That is the way you create the mannequin object:
mannequin = HfApiModel(
max_tokens=2096,
temperature=0.5,
model_id='Qwen/Qwen2.5-Coder-32B-Instruct',# it's potential that this mannequin could also be overloaded
custom_role_conversions=None,
)
We now have so as to add the prompts that we talked about earlier:
with open("prompts.yaml", 'r') as stream:
prompt_templates = yaml.safe_load(stream)
Now, our last process is to create the agent object.
agent = CodeAgent(
mannequin=mannequin,
instruments=[final_answer, get_current_time_in_timezone, get_amazing_dog_fact,
image_generation_tool], ## add your instruments right here (do not take away last reply)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
title=None,
description=None,
prompt_templates=prompt_templates
)
Observe the essential argument `instruments`. Right here we add all of the names of the capabilities that we created or outlined to a listing. This is essential. That is how the agent is aware of in regards to the instruments which might be accessible to its disposal.
Different arguments to this perform are a number of hyperparameters that we are going to not focus on or change on this tutorial. You’ll be able to check with the documentation for extra data.
For the total code, go forward and go to the repository and the app.py file from the place the above code is.
I’ve defined all of the core ideas and all the required code. HuggingFace offered the template of the undertaking right here.
Last Step: Internet hosting the Venture
You’ll be able to go forward proper now, and use the chat interface the place you should utilize the instruments that I’ve talked about.
Right here is my HuggingFace house, referred to as greetings_gen. You must clone the undertaking, and set an acceptable title, and in addition change the visibility to public if you wish to make the agent accessible to mates and public.


And make modifications `app.py` file and add your new instruments, take away mine- no matter you would like.
Listed here are some examples the place you may see the inputs and outputs of the agent:



Conclusion
Brokers can reliably carry out duties utilizing a number of instruments giving them extra autonomy, and permits them to finish extra advanced duties with deterministic inputs and outputs, whereas giving extra ease to the consumer.
You discovered in regards to the fundamentals of agentic AI, the fundamentals of utilizing smolagents library, and also you additionally discovered to create instruments of your personal that an AI agent can use, alongside internet hosting a chat mannequin in HuggingFace areas the place you may work together with an agent that makes use of the instruments that you just created!
Be at liberty to observe me on the Fediverse, X/Twitter, and LinkedIn. And make sure to go to my web site.
Key Takeaways
- AI brokers improve LLMs by integrating customized instruments for real-time information retrieval and decision-making.
- The
smolagents
library simplifies AI agent creation by offering an easy-to-use framework. - Customized instruments allow AI brokers to execute actions past customary language mannequin capabilities.
- Deploying AI brokers on Hugging Face Areas permits for straightforward sharing and interplay.
- Integrating AI brokers with customized instruments improves automation and effectivity in real-world purposes.
Often Requested Questions
A. An AI agent is an LLM-powered system that may work together with customized instruments to carry out particular duties past textual content technology.
A. Customized instruments assist AI brokers fetch real-time information, execute instructions, and carry out actions they will’t deal with on their very own.
smolagents
library?
A. smolagents
is a light-weight framework by Hugging Face that helps builders create AI brokers able to utilizing customized instruments.
A. You’ll be able to outline capabilities as customized instruments and combine them into your AI agent to increase its capabilities.
A. You’ll be able to deploy AI brokers on platforms like Hugging Face Areas for straightforward entry and interplay.
The media proven on this article shouldn’t be owned by Analytics Vidhya and is used on the Creator’s discretion.