01. ReAct Pattern from Scratch
Overview
In this session, we implement the ReAct (Reasoning + Acting) pattern from scratch using pure Python—no frameworks, no magic. This helps you understand exactly how AI agents work under the hood.
What is ReAct?
ReAct is a pattern where an LLM alternates between:
- Thought - Reasoning about what to do next
- Action - Calling a tool to get information
- Observation - Receiving and processing results
Key Concepts
The Agent Loop
# Pseudocode for ReAct loop
while not done:
response = llm.generate(messages)
if "Final Answer" in response:
return parse_final_answer(response)
action = parse_action(response)
observation = execute_tool(action)
messages.append(observation)System Prompt Design
The system prompt is crucial—it tells the LLM exactly how to format its responses:
You are an AI agent that can use tools.
Available tools:
1. calculate[expression] - Performs math calculations
2. search[query] - Searches for information
Format your response as:
Thought: [your reasoning]
Action: [tool_name][argument]Regex Parsing
We use regular expressions to extract structured information from the LLM's free-form text output:
import re
def parse_action(text):
pattern = r"Action:\s*(\w+)\[(.+?)\]"
match = re.search(pattern, text)
if match:
return match.group(1), match.group(2)
return NoneHands-on Practice
In the notebook, you will:
- Define Tools - Create
calculateandsearchfunctions - Design System Prompt - Craft instructions for the LLM
- Implement Parser - Extract actions from LLM output
- Build Agent Class - Put it all together in
ReActAgent - Test & Debug - Run various queries and analyze behavior
Limitations of This Approach
⚠️
This raw implementation has limitations that we'll address in the next session.
| Issue | Problem |
|---|---|
| Fragile Parsing | Regex breaks if LLM output varies |
| No Type Safety | Arguments aren't validated |
| Error Handling | Tool failures aren't graceful |
Next Steps
In Session 2: Tool Calling, we'll solve these issues using:
- Pydantic for type-safe schemas
- OpenAI Function Calling for structured output
- Proper error handling