AI Agent (A Brain With A Body)

Table of contents

“AI might know how to make chai, but an AI agent can make chai.”
Confused by the above statement and want to know more about AI agents? In this article, we will go through the concepts of AI agents with examples and code implementation.
What is an AI agent?
AI agents are software systems that use artificial intelligence to perform tasks and achieve goals on behalf of users or other systems. In simple words, an AI agent is connected with an AI model (pretrained model) that gives the information, and it is connected to the system to execute the task. For example, a model may not have a real-time weather report of a city. If the AI model gets a response from a weather API call, it will show the city’s current weather.
Examples
In the chat interface of ChatGPT, we get a feature “Search the web.” In this, we get a response from the web search, but ChatGPT uses a pretrained model. This is possible due to AI agents, which provide information from the web, and ChatGPT shows the result.
Another example can be vibe coding, where programmers use AI-powered tools to generate software based on natural language prompts, rather than writing code manually. This is possible because the AI agent performs tasks from the knowledge of a pretrained model and accesses the operating system to make changes.
Code Implementation
Suppose I want to make an AI agent that can tell the current weather of a city. So I need to connect current weather information with my AI model.
def get_weather(city: str):
print("Tool Called for", city)
url = f"https://wttr.in/{city}?format=%C+%t"
response = requests.get(url)
if response.status_code == 200:
return f"The weather in {city} is {response.text}"available_tools = {
"get_weather": {
"fn": get_weather,
"description": "Takes a city name as input and returns the current weather for the city"
}
}
In the above code, a get_weather function which takes a city name as a parameter and returns the current weather status.
available_tools = {
"get_weather": {
"fn": get_weather,
"description": "Takes a city name as input and returns the current weather for the city"
}
}
Here is a dictionary for the available tools, for all tools, the function reference and its description are mapped as values to their tool names.
systemPrompt = """
You are a helpful AI assistant who is specialize in resolving user query.
You work on start, plan, action, observe mode.
For the given query and avaialable tool, plan a step by step execution on the planning,
Select relevant tool from the available tool and based on the tool selection you perform an action to call the tool
Wait for the observation and based on the observation from the tool call resolve the user query.
Rules:
- Follow the Output JSON Format.
- Always perform one step at a time and wait for next input
- Carefully analyse the user query
Output JSON Format:
{{
"step": "string",
"content": "string",
"function": "The name of the function if the step is action",
"input": "The input parameter for the function",
}}
Available Tools:
- get_weather: Takes a city name as an input and returns the current weather for the city.
Example:
User Query: What is the weather of new york?
Output: {{"step": "plan", "content": "The user is interested in weather data of new york" }}
Output: {{"step": "plan", "content": "From the available tools I should call get_weather" }}
Output: {{"step": "action", "function": "get_weather", "input": "new york"}}
Output: {{"step": "observe", "output": "12 Degree Cel"}}
Output: {{"step": "output", "content": "The weather for new yorks seems to be 12 degrees." }}
"""
A system prompt that clearly defines the rules, JSON output format, available tools, and an example is provided.
query = input("> ")
messages = [{"role": "system", "content": systemPrompt}]
messages.append({"role": "user", "content": query})
messages is a list initially containing the system prompt. The query is taken from the user and appended to the messages.
while True:
response = client.chat.completions.create(
model = 'gpt-4.1-nano',
response_format={"type": "json_object"},
messages = messages
)
content = response.choices[0].message.content
parsed_content = json.loads(content)
messages.append({"role": "assistant", "content": content})
print(response.choices[0].message.content)
if parsed_content.get("step") == "action":
required_tool = parsed_content.get("function")
required_input = parsed_content.get("input")
if required_tool in available_tools:
output = available_tools[required_tool].get("fn")(required_input)
messages.append({"role": "assistant", "content": json.dumps({"step": "observe", "output": output})})
if parsed_content.get("step") == "output":
break
In a while loop, the stepwise output is generated using gpt-4.1-nano mode. When the step ‘action’ comes from the available tools, the appropriate tool is used to provide the result from the tool to the model.
Output
The results are significantly different when tools are not provided to the model. Without any tool, the model can suggest how to check the weather, but with the tool, it gives the current weather condition of Sohra.
Without tool (Only brain)
> How is the weather in Sohra?
I don’t have real-time weather data, but Sohra (also known as Cherrapunji) is famous for its heavy rainfall, particularly during the monsoon season from June to September. The weather is generally cool and pleasant for most of the year, with temperatures ranging from 10°C to 25°C (50°F to 77°F). To get the current weather conditions in Sohra, I recommend checking a reliable weather website or app for the latest updates.
With tool (Brain with body)
> How is the weather in Sohra?
{"step": "plan", "content": "The user wants to know the weather in Sohra."}
{"step": "plan", "content": "The user wants to know the weather in Sohra."}
{"step": "plan", "content": "From the available tools I should call get_weather"}
{"step": "action", "function": "get_weather", "input": "Sohra"}
Tool Called for Sohra
{"step": "output", "content": "The weather in Sohra is Patchy rain nearby with a temperature of 21°C."}
Conclusion
Now you must have got the statement the “AI might know how to make chai, but an AI agent can make a chai”. In code implementation, we integrated the current weather information with the model. Similarly, a database can be connected to the model to resolve queries, the model can be connected with OS, and endless possibilities exist. And finally, we can make our chai.
Subscribe to my newsletter
Read articles from Nitesh Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
