Lesson 12 of 13
Module 10 Practice
Practice designing and simulating an AI agent workflow.
Mini Project: Simulate an AI Agent
Mini Project: Simulate an AI Agent
# Mini Project: Simulate a simple AI research agent
# This illustration shows the ReAct (Reason, Act, Observe) loop
import os
class SimpleResearchAgent:
"""
A simple AI research agent simulation.
In practice, this would use the OpenAI API with tool calling.
"""
def __init__(self, goal):
self.goal = goal
self.memory = []
self.step = 0
self.completed = False
print(f"Agent initialized with goal: '{goal}'")
print()
def reason(self, context):
"""The agent reasons about what to do next."""
# In a real agent, this would call the LLM with the goal + context
# Here we simulate the reasoning
reasoning_map = {
0: ("web_search", "Python data visualization libraries comparison"),
1: ("extract_data", "library names and GitHub stars from search results"),
2: ("format_output", "comparison table with library name and star count"),
3: ("complete", "task complete"),
}
return reasoning_map.get(self.step, ("complete", "done"))
def act(self, action, action_input):
"""The agent takes an action."""
print(f"Step {self.step + 1} - Action: {action}('{action_input[:50]}')")
# Simulate tool outputs
simulated_results = {
"web_search": "Found: Matplotlib (18k stars), Plotly (14k stars), Seaborn (12k stars), Altair (8k stars), Bokeh (7k stars)",
"extract_data": "Extracted: [Matplotlib: 18000, Plotly: 14000, Seaborn: 12000, Altair: 8000, Bokeh: 7000]",
"format_output": "Created comparison table:\n| Library | GitHub Stars |\n|---------|-------------|\n| Matplotlib | 18,000 |\n| Plotly | 14,000 |\n| Seaborn | 12,000 |",
"complete": "Task completed successfully.",
}
return simulated_results.get(action, "Action executed.")
def observe(self, result):
"""The agent observes the result and updates memory."""
self.memory.append({"step": self.step + 1, "result": result})
print(f" Observation: {result[:80]}...")
print()
def run(self, max_steps=5):
"""Run the agent loop until the goal is complete or max steps reached."""
print("=== Agent Running ===")
print()
while not self.completed and self.step < max_steps:
# Reason about what to do
action, action_input = self.reason(self.memory)
if action == "complete":
print("Goal achieved!")
self.completed = True
break
# Act
result = self.act(action, action_input)
# Observe
self.observe(result)
self.step += 1
print(f"Agent completed in {self.step} steps.")
print()
print("Final memory:")
for mem in self.memory:
print(f" Step {mem['step']}: {mem['result'][:60]}...")
# Run the simulation
agent = SimpleResearchAgent(
"Find the top 5 Python data visualization libraries and compare their GitHub stars"
)
agent.run()Diagram
Loading diagram…
Practice Diagram
Key Takeaways from Module 10
- An AI agent receives a goal, plans steps, uses tools, takes actions, and repeats until the goal is complete
- AI agents differ from AI assistants: agents work autonomously across multiple steps; assistants respond to one prompt at a time
- Tools give agents their power: web search, code execution, file operations, API calls, and more
- The ReAct loop (Reason, Act, Observe, Repeat) is the core mechanism of most agents
- Memory allows agents to store and recall information during a task
- AI agents have limitations: error propagation, hallucinated actions, cost, and security risks
- Always implement safeguards before giving agents access to real systems with write permissions
Key Takeaways
- Practice designing and simulating an AI agent workflow.
- An AI agent receives a goal, plans steps, uses tools, takes actions, and repeats until the goal is complete
- AI agents differ from AI assistants: agents work autonomously across multiple steps; assistants respond to one prompt at a time
- Tools give agents their power: web search, code execution, file operations, API calls, and more
Finished this lesson?
0 of 13 in Module 10