My First AI Agent
A hands-on guide to implementing autonomous AI Agent with function tools and reasoning loops.
Introduction
This is the first article in the Intro to AI Agents series..
This blog post explores the implementation of simple AI Agents using the ReAct (Reasoning and Acting) pattern. We focus on their role in automating complex workflows and decision-making processes.
Intro to AI Agents:
👉 (This) Article 1: Simple ReAct Agent from Scratch
👉 Article 2: ReAct Agent in LangGraph
👉 Article 3: Persistence (memory) and streaming in Langgraph
👉 Article 4: Human in the loop
[Github] The Jupiter Notebook of this implementation
[Inspired by] Deeplearning.ai course
The Case for Agents
When writing a long summary without agents, you have two options:
Option 1: Give context to an LLM and ask it to write in one shot.
Option 2: Prompt the LLM to generate an outline, then another prompt to draft, then others to revise, and so on.
On the other hand, Agentic AI improves this process by planning tasks, using tools when needed, analyzing outputs, and refining results through repeated feedback loops until it reaches a strong final outcome. This allows the system to adapt dynamically and handle complex tasks with minimal human guidance.
Tools of Agentic AI
Functions: Functions that LLMs can decide to use if needed.
Reflection: Iteratively improving results with multiple LLMs through suggestions and editing cycles.
Multi-agent communication and
Memory
This article focuses on implementing a simple ReAct Agent with function tools.
Methodology of the ReAct Pattern
The ReAct pattern forms a core methodology involving:
Plan: Decide on actions based on observations.
Act and Observe: Execute actions and update with observations in a loop until completion.
Whenever the LLM decides to take an action, it returns the exact action to take with input parameters and pauses itself. We then execute it in Python and provide it back as an observation.
Here’s an example of a simple ReAct prompt:
You run in a loop of Thought, Action, PAUSE, Observation.
At the end of the loop you output an Answer
Use Thought to describe your thoughts about the question you have been asked.
Use Action to run one of the actions available to you - then return PAUSE.
Observation will be the result of running those actions.
Example of Tools
# Tool definitions
def calculate(what):
return eval(what)
def average_dog_weight(name):
if name in "Scottish Terrier":
return("Scottish Terriers average 20 lbs")
elif name in "Border Collie":
return("a Border Collies average weight is 37 lbs")
elif name in "Toy Poodle":
return("a toy poodles average weight is 7 lbs")
else:
return("An average dog weights 50 lbs")
known_actions = {
"calculate": calculate,
"average_dog_weight": average_dog_weight
}
The ReAct Loop
Implement a loop for action and observation as the agent processes:
# a regular expression to selection action assuming LLM will return
# Action: average_dog_weight: Scottish Terrier
action_re = re.compile('^Action: (\w+): (.*)$')
actions = [
action_re.match(a)
for a in result.split('\n')
if action_re.match(a)
]
if actions:
# There is an action to run
action, action_input = actions[0].groups()
if action not in known_actions:
# This should never happen
raise Exception("Unknown action: {}: {}".format(action, action_input))
print(" -- running {} {}".format(action, action_input))
observation = known_actions[action](action_input)
print("Observation:", observation)
next_prompt = "Observation: {}".format(observation)
else:
# This should happen only for the Answer
return
This is the sample execution output from the ReAct Agentic AI.
References
Intro to AI Agents:
👉 (This) Article 1: Simple ReAct Agent from Scratch
👉 Article 2: ReAct Agent in LangGraph
👉 Article 3: Persistence (memory) and streaming in Langgraph
👉 Article 4: Human in the loop[Github] The Jupiter Notebook of this implementation
The implementation of this article is inspired by this blog post and this course