LLMs have now exploded of their use throughout varied domains. They’re not restricted to chatbots hosted on the net however are being built-in into enterprises, authorities businesses, 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 allows LLM-powered AI techniques to function with larger autonomy, making them extra dependable for attaining full end-to-end activity completion.
Studying Targets
- Study what AI brokers are, how they differ from conventional LLMs, and their function in fashionable AI functions with customized instruments for LLM brokers.
- Uncover why AI brokers want customized instruments for LLM brokers to fetch real-time knowledge, execute actions, and improve decision-making.
- Acquire hands-on expertise in integrating and deploying AI brokers utilizing smolagents for real-world functions.
- Perceive the 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 Knowledge Science Blogathon.
Conditions
That is an article meant for the intermediate-level builders and knowledge professionals who’re nicely versed in utilizing fundamental LLMs. The next are anticipated:
- You understand how to code in Python in intermediate degree
- You already know the fundamentals of utilizing LLMs in your code
- You’re accustomed to the broader GenAI ecosystem
- You already know 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 be taught from this tutorial, however listed below are additional really useful background so that you can profit absolutely from this tutorial:
- You should use LLM libraries equivalent to LangChain, Ollama, and so on.
- You already know the fundamentals of Machine Studying concept
- You should use an API in your code, and resolve issues utilizing API responses
Fundamentals of Brokers in Generative AI
You’re in all probability accustomed to ChatGPT. You’ll be able to ask inquiries to it, and it solutions your questions. It could additionally write code for you, inform you a joke, and so on.
As a result of it will possibly code, and it will possibly reply your questions, you would possibly need to use it to finish duties for you, too. The place you demand one thing from it, and it completes a full activity for you.
Whether it is obscure for you proper now, don’t fear. Let me provide you with an instance. You already know LLMs can search the online, and so they can purpose utilizing info as enter. So, you’ll be able to 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 want 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 info 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 must go to given your standards, and accommodations and prices for every place.
Right here, the AI mannequin is utilizing your given standards to work together with the true world to seek for flights, accommodations, buses, and so on., and in addition counsel you locations to go to.
That is what we name agentic method in AI. And let’s be taught extra about it.
Workflow of an Agent
The agent relies 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 knowledge, and it causes utilizing textual content/language, and it will possibly solely output textual content.
It’s within the center half or the final half the place using instruments are available in. The instruments return some desired values, and utilizing these values the agent returns the response in textual content. It could additionally do one thing very completely different, like making a transaction on the inventory market, or generate a picture.

The workflow of an AI agent must be understood like this:
Perceive –> Motive –> Work together
That is one step of an agentic workflow, and when a number of steps are concerned, like in most use circumstances, it must be seen as:
Thought –> Motion –> Remark
Utilizing the command given to the agent, it thinks in regards to the activity at hand, analyzes what must be performed (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 activity is (Remark).
On this tutorial, we are going to code up a chat agent the place we are going to ask it to greet the person in keeping with the person’s time zone. So, when a person says, “I’m in Kolkata, greet me!”, the agent will take into consideration the request, and parse it rigorously. Then it would fetch the present time in keeping with the timezone, that is the motion. After which, it would observe for additional activity, whether or not the person have requested a picture. If not, then it would go on and greet the person. In any other case, it would additional take motion invoking the picture technology mannequin.
Components of an AI Agent
To this point, we have been speaking in conceptual phrases, and workflow. Now lets take a dive into the concrete elements 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 on. Utilizing this, the agent thinks and causes.
The instruments are externally coded instruments that the agent can invoke. It could name an API for a inventory value or the present temperature of a spot. Even have one other agent that it will possibly invoke. It may also be a easy calculator.
Utilizing `smolagents` framework, you’ll be able to create any perform in Python with any AI mannequin that has been tuned for perform calling.
In our instance, we could have a device to inform the person a enjoyable truth a couple of canine, fetch the present timezone, and generate a picture. The mannequin might 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 parts of these techniques will not be primarily based on Generative AI.
Beneath is an summary idea picture:

On this summary system graph, we see that GenAI elements typically must take essential inputs from non-Generative AI conventional system elements.
We’d like instruments to work together with these element and never the reply that’s current in an LLM’s data base.
As we’ve got 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 data deadline, and also you would possibly want up to date info like present climate, and inventory value knowledge. Otherwise you would possibly want details about geopolitical developments.
- LLMs typically hallucinate knowledge. For deployed functions, you want your brokers to be 100% right about any reply. LLMs typically fail to reply some easy Math issues.
- LLMs typically refuse to reply about questions for non-obvious causes, like, “As a Massive Language Mannequin, I can not reply this query”
- LLMs that may do a web-search use their picks of internet sites, however as an professional in a website, 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 utility. It’s developed by HuggingFace, and it’s Open Supply.
There are different frameworks equivalent to LlamaIndex, LangGraph, and so on. that you should utilize for a similar goal. However, for this tutorial, we are going to 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 method to be rather 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 challenge. I cannot undergo all of the code there, however I’ll spotlight a very powerful items of that codebase.
- The Gradio_UI.py file holds the code for the UI library Gradio utilizing which the agent interacts with the person.
- The agent.json file has the configuration of the file
- necessities.txt has the necessities of the challenge.
- The prompts.yaml file has the instance prompts and instance required for the agent to carry out actions. We are going to discuss extra about it later.
- The core of the app lies within the app.py file. We are going to focus on largely about this file.
The prompts.yaml file comprise 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 in the end ship to the mannequin. We are going to 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’ll both return a JSON blob, or they’ll instantly write code.
It’s obvious that if the tool-calling agent makes use of code instantly, it’s a lot 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,device
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 `device` lessons. We are going to use these in time.
We need to name an API that has saved cool information 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 `@device` 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’ll be able to
@device
def get_amazing_dog_fact()-> str:
"""A device that tells you an incredible truth about canine utilizing a public API.
Args: None
"""
# URL for the general public API
url = "https://dogapi.canine/api/v2/information?restrict=1"
# case when there's a response from the API
attempt:
#
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 truth couldn't be fetched."
besides requests.exceptions.RequestException as e:
return "A canine truth couldn't be fetched."
Observe that we’re returning a correctly parsed string as the ultimate reply.

Instrument to get present time
Beneath is a device to get the present time in a timezone of your selection:
@device
def get_current_time_in_timezone(timezone: str) -> str:
"""A device that fetches the present native time in a specified timezone.
Args:
timezone: A string representing a sound timezone (e.g., 'America/New_York').
"""
attempt:
# 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 can even use different instruments which can 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 need to 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 ultimate activity 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 ultimate reply)
max_steps=6,
verbosity_level=1,
grammar=None,
planning_interval=None,
identify=None,
description=None,
prompt_templates=prompt_templates
)
Observe the essential argument `instruments`. Right here we add all of the names of the features 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 can be obtainable 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 info.
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 challenge right here.
Remaining Step: Internet hosting the Undertaking
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, known as greetings_gen. It is best to clone the challenge, and set an appropriate identify, and in addition change the visibility to public if you wish to make the agent obtainable to associates and public.


And make adjustments `app.py` file and add your new instruments, take away mine- no matter you want.
Listed below are some examples the place you’ll be able to 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 allows them to finish extra complicated duties with deterministic inputs and outputs, whereas giving extra ease to the person.
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’ll be able to work together with an agent that makes use of the instruments that you simply created!
Be at liberty to observe me on the Fediverse, X/Twitter, and LinkedIn. And you should definitely go to my web site.
Key Takeaways
- AI brokers improve LLMs by integrating customized instruments for real-time knowledge 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 commonplace 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 functions.
Continuously 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 knowledge, execute instructions, and carry out actions they’ll’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 features 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 just isn’t owned by Analytics Vidhya and is used on the Creator’s discretion.