AI Agents

Ganesh GhadageGanesh Ghadage
7 min read

Now days you must have heard everyone talking about AI Agents or Agentic AI workflows. If you want to know what are the AI agents and how to build them,this is guide for you. In this blog we will talk about AI Agents and We will aslo build a small real-time weather AI Agent.

AI Agents

AI agents are not a secret agents like James Bond. They are just a piece of code, which will fo some task for us.

All the pre trained LLM models are very good in understating user query, logical reasoning and thinking, but you ask them to perform any task they can not perform it, itโ€™s like brain without hands and legs which can think but can not act. Agentic AI is like giving hands and legs to brain so that it can perform any action or task for us.

In Agentic AI workflow, we build some tools which acts as hands and legs of model and can perform any actions. We gave itโ€™s access to LLM model, LLM model does the NLP (Natural Language Processing) of the user query, and based on the query it uses appropriate tool to perform the action asked by the user.

Building An Weather AI Agent

To build AI Agents we have to build an tool which will perform the action that we need.

Here we need an tool which make an API call to weather API and get a real-time current weather information for us.

def get_weather(city: str):
  print("๐Ÿ”จ: get_weather tool called, input : ", city)

  url = f"https://wttr.in/{city}?format=%C+%t"
  response = requests.get(url)

  if response.status_code == 200:
    return response.text

  return "Something went wrong"

Now we have tell model that we have a tool which makes an and gets the real-time weather information. we provide this information in SYSTEM_PROMPT

avaliable_tools = {
  "get_weather": {
    "fn": get_weather,
    "description": "This function takes a city as a input and retuns the weather infomation for it."
  }
}

tool_descriptions = "\n".join(
  f"- {tool_name}: {avaliable_tools[tool_name]['description']}" for tool_name in avaliable_tools
)

system_prompt = f"""
  You are an helpfull weather AI assistant, you help in user to finding the current weather of their location.
  If user asks anything besids weather info, kindly reject the question.

  once user asks his query, you break down the query, analyze it, you plan how to answer it,
  use the approprate tool from the list to get required data, wait for it, and once you get data from tool
  use it to answer user query. include some funy message in output

  Follow the steps in sequence:
    analyze > plan > action > observe > output

  Rules:
    - follow strict JSON output format.
    - perform one step at a time and wait for next step
    - carefully analyze the user query

  Output JSON Format:
    {{
      "step": "string",
      "content": "string",
      "function": "string", # Name of the function in action step
      "input": "string", # Input paramenter for the function
      "result": string" # Output of the function call
    }}

  Avaliable tools:
    {tool_descriptions}

  Example:
    Input: What is weather in Pune?
    Output: {{"step": "analyze", "content": "User is instreated in knowing the weather information of pune"}}
    Output: {{"step": "plan", "content": "In order to get weather information I need to call get_weather tool with pune as parameter"}}
    Output: {{"step": "action", "function": "get_weather", "input": "pune"}}
    Output: {{"step": "observe", "result": "raniy, 20 degree Celcuis"}}
    Output: {{"step": "output", "content": "Curent weather of pune is raniy with 20 degree Celcuis, don't forget to take your umbrella!!"}}
"""

Notice in the above system_prompt we have asked model to follow the strict JSON output and also asked him to follow the certain steps, here in this case analyze > plan > action > observe > output. Also provided model an available tools list and example demonstrating how he should approach for the answer.

When we ask AI a query which is related to weather, it will give us a tool name to call with itโ€™s input parameter during an action step, By checking which step currently we are in, we only have call the function, decided by model. Note: model only provide us the tool name and we only have to invoke is, model can not invoke the tool.

while True:
    response = client.chat.completions.create(
      model="gemini-2.5-flash",
      response_format={"type": "json_object"},
      messages=messages
    )

    parsed_response = json.loads(response.choices[0].message.content)
    messages.append({"role": "assistant", "content": json.dumps(parsed_response)})

    if parsed_response.get("step") == "output":
      print(f"๐Ÿค–: {parsed_response.get("content")}")
      break

    if parsed_response.get("step") == "action":
      function_name = parsed_response.get("function")
      function_input = parsed_response.get("input")

      if function_name in avaliable_tools:
        result = avaliable_tools[function_name]["fn"](function_input)
        messages.append({"role": "assistant", "content": json.dumps({"step": "observe", "result": result})})
        continue
      else:
        print(f"โŒ Invalid function name")

Notice if step is action we are extracting function_name and function_input from the models response and invoking the function from avaliable_tools list with function_input and appending the functionโ€™s output in the messages list to give the context to model.

Complete Code:

import os
import json
import requests

from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()

client = OpenAI()

def get_weather(city: str):
  print("๐Ÿ”จ: get_weather tool called, input : ", city)

  url = f"https://wttr.in/{city}?format=%C+%t"
  response = requests.get(url)

  if response.status_code == 200:
    return response.text

  return "Something went wrong"

avaliable_tools = {
  "get_weather": {
    "fn": get_weather,
    "description": "This function takes a city as a input and retuns the weather infomation for it."
  }
}

tool_descriptions = "\n".join(
  f"- {tool_name}: {avaliable_tools[tool_name]['description']}" for tool_name in avaliable_tools
)

system_prompt = f"""
  You are an helpfull weather AI assistant, you help in user to finding the current weather of their location.
  If user asks anything besids weather info, kindly reject the question.

  once user asks his query, you break down the query, analyze it, you plan how to answer it,
  use the approprate tool from the list to get required data, wait for it, and once you get data from tool
  use it to answer user query. include some funy message in output

  Rules:
    - follow strict JSON output format.
    - perform one step at a time and wait for next step
    - carefully analyze the user query

  Output JSON Format:
    {{
      "step": "string",
      "content": "string",
      "function": "string", # Name of the function in action step
      "input": "string", # Input paramenter for the function
      "result": string" # Output of the function call
    }}

  Avaliable tools:
    {tool_descriptions}

  Example:
    Input: What is weather in Pune?
    Output: {{"step": "analyze", "content": "User is instreated in knowing the weather information of pune"}}
    Output: {{"step": "plan", "content": "In order to get weather information I need to call get_weather tool with pune as parameter"}}
    Output: {{"step": "action", "function": "get_weather", "input": "pune"}}
    Output: {{"step": "observe", "result": "raniy, 20 degree Celcuis"}}
    Output: {{"step": "output", "content": "Curent weather of pune is raniy with 20 degree Celcuis, don't forget to take your umbrella!!"}}
"""

messages = []
messages.append({"role": "system", "content": system_prompt})

print("Welcome ๐Ÿ™‹๐Ÿผโ€โ™‚๏ธ, Know the weather of your location!โ›…")
while True:
  query = input("> ")
  messages.append({"role": "user", "content": query})

  if query.lower() in ['exit', 'quit'] :
    print("Good Bye!! ๐Ÿ‘‹๐Ÿผ")
    break

  while True:
    response = client.chat.completions.create(
      model="gemini-2.5-flash",
      response_format={"type": "json_object"},
      messages=messages
    )

    parsed_response = json.loads(response.choices[0].message.content)
    messages.append({"role": "assistant", "content": json.dumps(parsed_response)})

    if parsed_response.get("step") == "output":
      print(f"๐Ÿค–: {parsed_response.get("content")}")
      break

    if parsed_response.get("step") == "action":
      function_name = parsed_response.get("function")
      function_input = parsed_response.get("input")

      if function_name in avaliable_tools:
        result = avaliable_tools[function_name]["fn"](function_input)
        messages.append({"role": "assistant", "content": json.dumps({"step": "observe", "result": result})})
        continue
      else:
        print(f"โŒ Invalid function name")

    print(f"๐Ÿง : {parsed_response.get("content")}")

Above is the complete for a simple real-time weather AI Agent code. You can directly copy and run it. Make sure you provide the OpenAI API Key.

Now, once you understand the Agentic AI concept, we just have to build various tools and give their access to model. Use case this can be anything, we can build function which can read/write in database, a function which can even write a code for us, any function which fulfills your requirement.

You are just one function away to build your own personalized AI Agent.

Summary

The article introduces AI Agents and Agentic AI workflows, explaining their function as code pieces that execute tasks, akin to giving hands and legs to a brain (LLM models). It outlines steps to build an AI agent with a real-time weather example, showcasing how to create a tool for weather data retrieval via API calls. The guide emphasizes a structured workflow: analyze, plan, action, observe, and output. The article also highlights the capability of creating personalized AI agents by developing various functions that can interact with databases, generate code, or meet specific needs, contingent on integrating them with AI models like OpenAI's.

0
Subscribe to my newsletter

Read articles from Ganesh Ghadage directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Ganesh Ghadage
Ganesh Ghadage