AIMY: You've been using AI to respond. In this module, you're going to understand how AI reasons — or rather, how it simulates reasoning using a loop. The ReAct pattern (Reason + Act) is the architecture behind every AI agent you've ever used. ChatGPT's code interpreter, Claude's tool use, every autonomous AI workflow: they're all ReAct loops. Once you understand the pattern, you can design it yourself.
ANALYZE
Understanding the ReAct Loop
1
What ReAct is
Reason: the model thinks through what it needs to do. Act: the model takes an action (calls a tool, searches something, writes code). Observe: the model reads the result. Repeat: until the goal is reached or the loop ends.
2
Why it works
A single prompt gives AI one shot to get the answer. A ReAct loop gives it multiple shots. Each iteration improves on the last. This is how AI goes from "generate text" to "complete a multi-step task."
3
Tools are actions
In a ReAct agent, "tools" are functions the AI can call: search the web, read a file, run code, call an API. The model decides which tool to use based on the current state of the task.
4
Where it breaks
ReAct loops can go infinite if the goal isn't clearly defined. They can produce wrong results if the Observe step doesn't verify the output. Building good agents means designing good stopping conditions.
INTEGRATE
Trace and Implement a ReAct Loop
- Create:
touch react_trace.py - First, trace the loop manually:
Task: Find word count of test_doc.txt and report if above 50 words Reason: I need to read the file and count words Act: open and count Observe: Word count is X Reason: Is X > 50? Act: compare Observe: result Conclude: print final answer
- Implement as Python:
def react_loop(task, filename): with open(filename) as f: content = f.read() words = content.split() count = len(words) observation = f"Word count: {count}" if count > 50: conclusion = "Above threshold" else: conclusion = "Below threshold" return f"Task: {task}\nObservation: {observation}\nConclusion: {conclusion}" result = react_loop("Word count check", "test_doc.txt") print(result)
- Run and read the output — trace each step of the loop.
PTR (PROOF THAT IT RUNS)
- You implemented a minimal ReAct loop.
- You can label each step (Reason, Act, Observe) in your code.
- You understand why a stopping condition is required.
Common Mistakes
Infinite loops
Always define what "done" looks like before starting. Add a
max_iterations counter.Skipping the Observe step
Every action must be followed by reading the result. Don't chain actions without checking outputs.
CHECKPOINT
- You traced all four ReAct steps in your code.
- You understand why agents use loops instead of single prompts.
- You can name what "tools" are in agent architecture.
- You're ready to deploy your first AI tool.
AIM COMMITMENT
Analyze: You understood the ReAct pattern as the foundation of AI agent behavior.
Integrate: You implemented a minimal ReAct loop in Python with explicit Reason → Act → Observe steps.
Manage: You understand how to design stopping conditions and why they matter for reliable agents.