Agentic AI
In this chapter, we will look at what Agentic AI means and how you can build your own Agent using an LLM. We will go over what Agentic AI actually is, how it is different from normal AI, and the steps you need to follow to build your own Agent using a Large Language Model (LLM).
What is AI Agent?
Section titled “What is AI Agent?”An LLM (Large Language Model) is like a very smart chatbot. You give it a question or a message, and it gives you back a text answer. It can explain things, write code, summarize articles, and answer questions, but on its own it can only read text and write text. It cannot actually do anything in the real world.
An AI Agent is an LLM that has been given tools and permission to take action. Instead of only replying with text, it can decide what needs to be done, use tools, access databases, call APIs, search the web, send emails, or do tasks on your behalf. A simple way to picture this is: an LLM is the brain, while an AI Agent is the brain plus hands and legs. The brain can think, but the agent can think and act.
Example:
- LLM: “Query: Find the current weather in New York City. Response: I don’t have access to real-time data, but you can check the weather on a website or app.”
- AI Agent: “Query: Find the current weather in New York City. Response: I will use the OpenWeatherMap API to get the current weather for you. [Agent calls API and retrieves data] The current weather in New York City is 75°F with clear skies.” This means we gave the LLM a tool or method that lets it fetch real-time weather data and reply using that data.
So, in simple words, an LLM only talks, while an AI Agent talks and actually gets things done.
How to Build an AI Agent?
Section titled “How to Build an AI Agent?”As we said, an LLM works like the brain of an AI system. It can think, understand instructions, and make decisions, but it cannot directly touch or interact with the outside world by itself. To actually do things, it needs hands and a body. These are called tools.
When you build an AI Agent, you connect the LLM to a set of tools, such as APIs, databases, web browsers, or other software. The LLM (the brain) decides what needs to be done, and the tools (the hands and body) carry out those actions. This lets the AI Agent not just think and answer questions, but also gather information, interact with other systems, and finish real tasks in the real world.
# example of tool functionimport requests
def get_weather(city: str): try: url = f"https://wttr.in/{city.lower()}?format=%C+%t" response = requests.get(url, timeout=10)
if response.status_code == 200: return f"The weather in {city} is {response.text}"
return "Something went wrong, Try again or respond accordingly if tried Multiple times"
except Exception: return "Something went wrong, Try again or respond accordingly if tried Multiple times"
# mapping of tool name to functionget_tool = { "get_weather": { "function": get_weather, "parameters": { "city": "string" } }}In this example, we have a simple tool function called get_weather that takes a city name as input and gives back the current weather for that city by calling the wttr.in API. We then build a mapping that connects tool names to their functions and parameters. This lets the LLM know how to use this tool whenever it needs weather information.
Weather Agent Example
Section titled “Weather Agent Example”Let’s say we want to build a simple Weather Agent that can give the current weather for any city. We will use the get_weather tool we made earlier and connect it to an LLM.
from dotenv import load_dotenvimport jsonfrom openai import OpenAIimport osfrom pydantic import BaseModel, Fieldfrom typing import Optional, Literalimport requests
load_dotenv()
# Tool function to get weather information for a citydef get_weather(city: str): try: url = f"https://wttr.in/{city.lower()}?format=%C+%t" response = requests.get(url, timeout=10)
if response.status_code == 200: return f"The weather in {city} is {response.text}"
return "Something went wrong, Try again or respond accordingly if tried Multiple times"
except Exception: return "Something went wrong, Try again or respond accordingly if tried Multiple times"
# mapping of tool name to functionget_tool = {"get_weather": {"function": get_weather, "parameters": {"city": "string"}}}
class ResponseFormat(BaseModel): action: Literal["START", "THINK", "TOOL", "END"] = Field( ..., description="Action to be taken by the agent" ) content: Optional[str] = Field( None, description="Thought content or answer content based on the action" ) tool: Optional[str] = Field(None, description="Name of tool that agent want to use") tool_input: Optional[str] = Field(None, description="Input for the tool")
SYSTEM_PROMPT = """ You're an expert AI Assistant in resolving user queries using chain of thought. You work on START, THINK, TOOL, and END steps. Only output one action at a time.
Output JSON Format: {{ "action": "START" | "THINK" | "TOOL" | "END", "content": "Optional[string]", "tool": "Optional[string]", "tool_input": "Optional[string]" }}
Available Tools: - get_weather(city: str): Takes city name as an input string and returns the weather info about the city.
Rules: - Strictly Follow the given JSON output format - Only run one step at a time. - Total number of THINK steps should not be more than 10 and not less than 3. - If you want to use a tool, you must output TOOL action with tool name and tool input. Then wait for the developer response which is the output from the tool and then continue with your thought process. - Once you have enough information to answer the user's question, you can give the final answer with END action. - Always use THINK action to think step by step before giving the final answer. - Always use TOOL action when you want to use a tool and wait for the developer response before giving the next thought then giving the final answer. - If error occurs while read error message and think about it and if needed try again with the same or different tool or give the final answer based on the information you have but not directly say this tool is not working. Use some jargon.
Example 1: User: Hey, Can you tell me the weather in New York? Assistant: {{ "action": "START", "content": "Hey, Can you tell me the weather in New York?" }} Assistant: {{ "action": "THINK", "content": "User is asking about the weather in New York. I have to check what is the weather in New York." }} Assistant: {{ "action": "THINK", "content": "I need to use the get_weather tool to get the weather information for New York." }} Assistant: {{ "action": "TOOL", "tool": "get_weather", "tool_input": "New York" }} developer: {{ "action": "TOOL", "tool": "get_weather", "tool_input": "New York", "content": "The weather in New York is Cloudy with 25 C" }} Assistant: {{ "action": "THINK", "content": "I got the weather information for New York. Now I can give the answer to the user." }} Assistant: {{ "action": "END", "content": "The current weather in New York is Cloudy with 25 C." }}
Example 2: User: What is 2 + 3 * 5? Assistant: {{ "action": "START", "content": "What is 2 + 3 * 5?" }} Assistant: {{ "action": "THINK", "content": "User is asking about a math problem. I need to solve this problem step by step." }} Assistant: {{ "action": "THINK", "content": "According to BODMAS rule, I need to solve the multiplication first." }} Assistant: {{ "action": "THINK", "content": "3 * 5 is 15. Now the new equation is 2 + 15." }} Assistant: {{ "action": "THINK", "content": "Now I need to solve the addition. 2 + 15 is 17." }} Assistant: {{ "action": "END", "content": "The answer to the problem is 17." }}"""
clint = OpenAI( api_key=os.getenv("API_KEY"), base_url=os.getenv("API_BASE"),)
def main(): question = input("Ask a question: ") messages = [ {"role": "system", "content": SYSTEM_PROMPT}, {"role": "user", "content": question}, ]
while True: response = clint.chat.completions.parse( model=os.getenv("AI_MODEL"), messages=messages, response_format=ResponseFormat, )
messages.append( {"role": "assistant", "content": response.choices[0].message.content} )
parsed_result = response.choices[0].message.parsed
if parsed_result.action == "START": print(f"🚀: {parsed_result.content}") continue elif parsed_result.action == "THINK": print(f"🤔: {parsed_result.content}") continue elif parsed_result.action == "END": print(f"✅: {parsed_result.content}") break elif parsed_result.action == "TOOL": tool_name = parsed_result.tool tool_input = parsed_result.tool_input
print(f"🛠️: {tool_name} ({tool_input})") tool_response = get_tool[tool_name]["function"](tool_input) print(f"🛠️: {tool_name} ({tool_input}) = {tool_response}")
messages.append( { "role": "developer", "content": json.dumps( { "action": "TOOL", "tool": tool_name, "tool_input": tool_input, "content": tool_response, } ), } )
if __name__ == "__main__": main()In this example, we built a simple Weather Agent that can give the current weather for any city. The agent uses the get_weather tool to fetch weather information, and it follows a clear process of thinking and acting until it reaches a final answer for the user. The agent starts by understanding the question, thinks through how to approach it, decides to use a tool, and then gives the final answer based on the information it gathered.
Conclusion
Section titled “Conclusion”Agentic AI is a big step forward in what artificial intelligence can do. By connecting Large Language Models (LLMs) to tools and letting them take action, we can build AI Agents that don’t just understand and write text, but also interact with the world to get things done. Building your own AI Agent means deciding what tools it can use, setting up its thought process, and letting it act based on what the user asks. This opens the door to many possibilities for building smart systems that can help with all kinds of tasks in real time.