Our previous article discussed the first Agentic AI design pattern from the Reflection, Tool Use, Planning, and Multi-Agent Patterns list. In this third article, we will talk about the Tool Use Pattern in Agentic AI.
Firstly, let us reiterate the Reflection Pattern article: That article sheds light on how LLMs can use an iterative generation process and self-assessment to improve output quality. The core idea here is that the AI, much like a course creator revising lesson plans, generates content, critiques it, and refines it iteratively, improving with each cycle. The Reflection Pattern is particularly useful in complex tasks like text generation, problem-solving, and code development. In the reflection pattern, the AI plays dual roles: creator and critic. This cycle repeats, often until a certain quality threshold or stopping criterion is met, such as a fixed number of iterations or an acceptable level of quality. The comprehensive article is here: What is Agentic AI Reflection Pattern?
Now, let’s talk about the Tool Use Pattern in Agentic AI, a crucial mechanism that enables AI to interact with external systems, APIs, or resources beyond its internal capabilities.
Also, here are the 4 Agentic AI Design Pattern: Top 4 Agentic AI Design Patterns for Architecting AI Systems.
Tool Use is the robust technology that represents a key design pattern transforming how large language models (LLMs) operate. This innovation enables LLMs to transcend their natural limitations by interacting with external functions to gather information, perform actions, or manipulate data. Through Tool Use, LLMs are not just confined to producing text responses from their pre-trained knowledge; they can now access external resources like web searches, code execution environments, and productivity tools. This has opened up vast possibilities for AI-driven agentic workflows.
The initial development of LLMs focused on using pre-trained transformer models to predict the next token in a sequence. While this was groundbreaking, the model’s knowledge was limited to its training data, which became outdated and constrained its ability to interact dynamically with real-world data.
For instance, take the query: “What are the latest stock market trends for 2024?” A language model trained solely on static data would likely provide outdated information. However, by incorporating a web search tool, the model can perform a live search, gather recent data, and deliver an up-to-the-minute analysis. This highlights a key shift from merely referencing preexisting knowledge to accessing and integrating fresh, real-time information into the AI’s workflow.
The above-given diagram represents a conceptual Agentic AI tool use pattern, where an AI system interacts with multiple specialised tools to efficiently process user queries by accessing various information sources. This approach is part of an evolving methodology in AI called Agentic AI, designed to enhance the AI’s ability to handle complex tasks by leveraging external resources.
End-to-End Process:
This diagram captures a central feature of agentic AI, where models can autonomously decide which external tools to use based on the user’s query. Instead of merely providing a static response, the LLM acts as an agent that dynamically selects tools, formats data, and returns processed results, which is a core part of tool-use patterns in agentic AI systems. This type of tool integration allows LLMs to extend their capabilities far beyond simple language processing, making them more versatile agents capable of performing structured tasks efficiently.
We will implement the tool use pattern in 3 ways:
We are building a Blog Research and Content Generation Agent (BRCGA) that automates the process of researching the latest trends in the AI industry and crafting high-quality blog posts. This agent leverages specialised tools to gather information from web searches, directories, and files, ultimately producing engaging and informative content.
The BRCGA is divided into two core roles:
import os
from crewai import Agent, Task, Crew
# Importing crewAI tools
from crewai_tools import (
DirectoryReadTool,
FileReadTool,
SerperDevTool,
WebsiteSearchTool
)
os.environ["SERPER_API_KEY"] = "Your Key" # serper.dev API key
os.environ["OPENAI_API_KEY"] = "Your Key"
These lines set environment variables for the API keys of the Serper API and OpenAI API. These keys are necessary for accessing external services (e.g., for web searches or using GPT models).
# Instantiate tools
docs_tool = DirectoryReadTool(directory='/home/xy/VS_Code/Ques_Ans_Gen/blog-posts')
file_tool = FileReadTool()
search_tool = SerperDevTool()
web_rag_tool = WebsiteSearchTool()
# Create agents
researcher = Agent(
role='Market Research Analyst',
goal='Provide 2024 market analysis of the AI industry',
backstory='An expert analyst with a keen eye for market trends.',
tools=[search_tool, web_rag_tool],
verbose=True
)
Researcher: This agent conducts market research. It uses the search_tool (for online searches) and the web_rag_tool (for specific website queries). The verbose=True setting ensures that the agent provides detailed logs during task execution.
writer = Agent(
role='Content Writer',
goal='Craft engaging blog posts about the AI industry',
backstory='A skilled writer with a passion for technology.',
tools=[docs_tool, file_tool],
verbose=True
)
Writer: This agent is responsible for creating content. It uses the docs_tool (to gather inspiration from previous blog posts) and the file_tool (to access files). Like the researcher, it is set to verbose=True for detailed task output.
# Define tasks
research = Task(
description='Research the latest trends in the AI industry and provide a
summary.',
expected_output='A summary of the top 3 trending developments in the AI industry
with a unique perspective on their significance.',
agent=researcher
)
Research task: The researcher agent is tasked with identifying the latest trends in AI and producing a summary of the top three developments.
write = Task(
description='Write an engaging blog post about the AI industry, based on the
research analyst’s summary. Draw inspiration from the latest blog
posts in the directory.',
expected_output='A 4-paragraph blog post formatted in markdown with engaging,
informative, and accessible content, avoiding complex jargon.',
agent=writer,
output_file='blog-posts/new_post.md' # The final blog post will be saved here
)
Write task: The writer agent is responsible for writing a blog post based on the researcher’s findings. The final post will be saved to ‘blog-posts/new_post.md’.
# Assemble a crew with planning enabled
crew = Crew(
agents=[researcher, writer],
tasks=[research, write],
verbose=True,
planning=True, # Enable planning feature
)
Crew: This is the team of agents (researcher and writer) tasked with completing the research and writing jobs. The planning=True option suggests that the crew will autonomously plan the order and approach to completing the tasks.
# Execute tasks
result = crew.kickoff()
This line kicks off the execution of the tasks. The crew (agents) will carry out their assigned tasks in the order they planned, with the researcher doing the research first and the writer crafting the blog post afterwards.
The agent we’re building, SentimentAI, is designed to act as a powerful assistant that analyses text content, evaluates its sentiment, and ensures positive and engaging communication. This tool could be used in various fields, such as customer service, marketing, or even personal communication, to gauge the emotional tone of the text and ensure it aligns with the desired communication style.
The agent could be deployed in customer service, social media monitoring, or brand management to help companies understand how their customers feel about them in real time. For example, a company can use Sentiment AI to analyse incoming customer support requests and route negative sentiment cases for immediate resolution. This helps companies maintain positive customer relationships and address pain points quickly.
Here’s the link: CrewAI
from crewai import Agent, Task, Crew
from dotenv import load_dotenv
load_dotenv()
import os
# from utils import get_openai_api_key, pretty_print_result
# from utils import get_serper_api_key
# openai_api_key = get_openai_api_key()
os.environ["OPENAI_MODEL_NAME"] = 'gpt-4o'
# os.environ["SERPER_API_KEY"] = get_serper_api_key()
Here are the agents:
sales_rep_agent = Agent(
role="Sales Representative",
goal="Identify high-value leads that match "
"our ideal customer profile",
backstory=(
"As a part of the dynamic sales team at CrewAI, "
"your mission is to scour "
"the digital landscape for potential leads. "
"Armed with cutting-edge tools "
"and a strategic mindset, you analyze data, "
"trends, and interactions to "
"unearth opportunities that others might overlook. "
"Your work is crucial in paving the way "
"for meaningful engagements and driving the company's growth."
),
allow_delegation=False,
verbose=True
)
lead_sales_rep_agent = Agent(
role="Lead Sales Representative",
goal="Nurture leads with personalized, compelling communications",
backstory=(
"Within the vibrant ecosystem of CrewAI's sales department, "
"you stand out as the bridge between potential clients "
"and the solutions they need."
"By creating engaging, personalized messages, "
"you not only inform leads about our offerings "
"but also make them feel seen and heard."
"Your role is pivotal in converting interest "
"into action, guiding leads through the journey "
"from curiosity to commitment."
),
allow_delegation=False,
verbose=True
)
from crewai_tools import DirectoryReadTool, \
FileReadTool, \
SerperDevTool
directory_read_tool = DirectoryReadTool(directory='/home/badrinarayan/Downloads/instructions')
file_read_tool = FileReadTool()
search_tool = SerperDevTool()
from crewai_tools import BaseTool
from textblob import TextBlob
class SentimentAnalysisTool(BaseTool):
name: str ="Sentiment Analysis Tool"
description: str = ("Analyzes the sentiment of text "
"to ensure positive and engaging communication.")
def _run(self, text: str) -> str:
# Perform sentiment analysis using TextBlob
analysis = TextBlob(text)
polarity = analysis.sentiment.polarity
# Determine sentiment based on polarity
if polarity > 0:
return "positive"
elif polarity < 0:
return "negative"
else:
return "neutral"
sentiment_analysis_tool = SentimentAnalysisTool()
This code demonstrates a tool use pattern within an agentic AI framework, where a specific tool—Sentiment Analysis Tool—is implemented for sentiment analysis using the TextBlob library. Let’s break down the components and understand the flow:
Imports
The class SentimentAnalysisTool inherits from BaseTool, meaning it will have the behaviours and properties of BaseTool, and it’s customised for sentiment analysis. Let’s break down each section:
Attributes
Method: _run()
The _run() method is the core logic of the tool. In agentic AI frameworks, methods like _run() are used to define what a tool will do when it’s executed.
At the end of the code, sentiment_analysis_tool = SentimentAnalysisTool() creates an instance of the SentimentAnalysisTool. This instance can now be used to run sentiment analysis on any input text by calling its _run() method.
For the full code implementation, refer to this link: Google Colab.
Output
Here, the agent retrieves information from the internet based on the “search_query”: “Analytics Vidhya company profile.”
Here’s the output with sentiment analysis:
Here, we are displaying the final result as Markdown:
You can find the full code and output here: Colab Link
HackerBot is an AI agent designed to fetch and present the latest top stories from hacker_news_stories, a popular news platform focused on technology, startups, and software development. By leveraging the Hacker News API, HackerBot can quickly retrieve and deliver information about the top trending stories, including their titles, URLs, scores, and more. It serves as a useful tool for developers, tech enthusiasts, and anyone interested in staying updated on the latest tech news.
The agent can interact with users based on their requests and fetch news in real time. With the ability to integrate more tools, HackerBot can be extended to provide other tech-related functionalities, such as summarizing articles, providing developer resources, or answering questions related to the latest tech trends.
Note: For tool use from scratch, we are referring to the research and implementation of the Agentic AI design pattern by Michaelis Trofficus.
Here is the code:
import json
import requests
from agentic_patterns.tool_pattern.tool import tool
from agentic_patterns.tool_pattern.tool_agent import ToolAgent
For the class implementation of “from agentic_patterns.tool_pattern.tool import tool” and “from agentic_patterns.tool_pattern.tool_agent import ToolAgent” you can refer to this repo.
def fetch_top_hacker_news_stories(top_n: int):
"""
Fetch the top stories from Hacker News.
This function retrieves the top `top_n` stories from Hacker News using the
Hacker News API.
Each story contains the title, URL, score, author, and time of submission. The
data is fetched
from the official Firebase Hacker News API, which returns story details in JSON
format.
Args:
top_n (int): The number of top stories to retrieve.
"""
top_stories_url = 'https://hacker-news.firebaseio.com/v0/topstories.json'
try:
response = requests.get(top_stories_url)
response.raise_for_status() # Check for HTTP errors
# Get the top story IDs
top_story_ids = response.json()[:top_n]
top_stories = []
# For each story ID, fetch the story details
for story_id in top_story_ids:
story_url = f'https://hacker-news.firebaseio.com/v0/item/{story_id}.json'
story_response = requests.get(story_url)
story_response.raise_for_status() # Check for HTTP errors
story_data = story_response.json()
# Append the story title and URL (or other relevant info) to the list
top_stories.append({
'title': story_data.get('title', 'No title'),
'url': story_data.get('url', 'No URL available'),
})
return json.dumps(top_stories)
except requests.exceptions.RequestException as e:
print(f"An error occurred: {e}")
return []
json.loads(fetch_top_hacker_news_stories(top_n=5))
hn_tool = tool(fetch_top_hacker_news_stories)
hn_tool.name
json.loads(hn_tool.fn_signature)
tool_agent = ToolAgent(tools=[hn_tool])
output = tool_agent.run(user_msg="Tell me your name")
print(output)
I don't have a personal name. I am an AI assistant designed to provide information and assist with tasks.
output = tool_agent.run(user_msg="Tell me the top 5 Hacker News stories right now")
Output
The way an agentic AI uses tools reveals key aspects of its autonomy and problem-solving capabilities. Here’s how they connect:
Agentic AI systems often rely on tool selection patterns to make decisions. For instance, based on the problem it faces, the AI needs to recognise which tools are most appropriate. This requires pattern recognition, decision-making, and a level of understanding of both the tools and the task at hand. Tool use patterns indicate how well the AI can analyze and break down a task.
Example: A natural language AI assistant might decide to use a translation tool if it detects a language mismatch or a calendar tool when a scheduling task is required.
One hallmark of agentic AI is its ability to autonomously execute actions after selecting the right tools. It doesn’t need to wait for human input to choose the correct tool. The pattern in which these tools are used demonstrates how autonomous the AI is in completing tasks.
For instance, if a weather forecasting AI autonomously selects a data scraping tool to gather up-to-date weather reports and then uses an internal modeling tool to generate predictions, it’s demonstrating a high degree of autonomy through its tool use pattern.
Agentic AI systems often employ reinforcement learning or similar techniques to refine their tool use patterns over time. By tracking which tools successfully achieved goals or solve problems, the AI can adapt and optimise its future behaviour. This self-improvement cycle is essential for increasingly agentic systems.
For instance, an AI system might learn that a certain computation tool is more effective for solving specific types of optimisation problems and will adapt its future behaviour accordingly.
An advanced agentic AI doesn’t just use a single tool at a time but may coordinate the use of multiple tools to achieve more complex objectives. This pattern of multi-tool use reflects a deeper understanding of task complexity and how to manage resources effectively.
Example: An AI performing medical diagnosis might pull data from a patient’s health records (using a database tool), run symptoms through a diagnostic AI model, and then use a communication tool to report findings to a healthcare provider.
The more diverse an AI’s tool use patterns, the more flexible and generalisable it tends to be. Systems that can effectively apply various tools across domains are more agentic because they aren’t limited to a predefined task. These patterns also suggest the AI’s capability to abstract and generalise knowledge from its tool-based interactions, which is key to achieving true agency.
As AI systems evolve, their ability to dynamically acquire and integrate new tools will further enhance their agentic qualities. Current AI systems usually have a pre-defined set of tools. Future agentic AI might autonomously find, adapt, or even create new tools as needed, further deepening the connection between tool use and agency.
If you are looking for an AI Agent course online, then explore: the Agentic AI Pioneer Program.
The Tool Use Pattern in Agentic AI allows large language models (LLMs) to transcend their inherent limitations by interacting with external tools, enabling them to perform tasks beyond simple text generation based on pre-trained knowledge. This pattern shifts AI from relying solely on static data to dynamically accessing real-time information and performing specialised actions, such as running simulations, retrieving live data, or executing code.
The core idea is to modularise tasks by assigning them to specialised tools (e.g., fact-checking, solving equations, or language translation), which results in greater efficiency, flexibility, and scalability. Instead of a monolithic AI handling all tasks, Agentic AI leverages multiple tools, each designed for specific functionalities, leading to faster processing and more effective multitasking.
The Tool Use Pattern highlights key features of Agentic AI, such as decision-making, autonomous action, learning from tool usage, and multi-tool coordination. These capabilities enhance the AI’s autonomy and problem-solving potential, allowing it to handle increasingly complex tasks independently. The system can even adapt its behaviour over time by learning from successful tool usage and optimizing its performance. As AI continues to evolve, its ability to integrate and create new tools will further deepen its autonomy and agentic qualities.
I hope you find this article informative, in the next article of the series ” Agentic AI Design Pattern” we will talk about: Planning Pattern
If you’re interested in learning more about Tool Use, I recommend:
Ans. The Agentic AI Tool Use Pattern refers to the way AI tools are designed to operate autonomously, taking initiative to complete tasks without requiring constant human intervention. It involves AI systems acting as “agents” that can independently decide the best actions to achieve specified goals.
Ans. Unlike traditional AI that follows pre-programmed instructions, agentic AI can make decisions, adapt to new information, and execute tasks based on goals rather than fixed scripts. This autonomy allows it to handle more complex and dynamic tasks.
Ans. A common example is using a web search tool to answer real-time queries. For instance, if asked about the latest stock market trends, an LLM can use a tool to perform a live web search, retrieve current data, and provide accurate, timely information, instead of relying solely on static, pre-trained data.
Ans. Modularization allows tasks to be broken down and assigned to specialized tools, making the system more efficient and scalable. Each tool handles a specific function, like fact-checking or mathematical computations, ensuring tasks are processed faster and more accurately than a single, monolithic model.
Ans. The pattern enhances efficiency, scalability, and flexibility by enabling AI to dynamically select and use different tools. It also allows for real-time adaptation, ensuring responses are up-to-date and accurate. Furthermore, it promotes learning from tool usage, leading to continuous improvement and better problem-solving abilities.