English
Week 1: Fundamentals
01. ReAct Pattern

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:

  1. Thought - Reasoning about what to do next
  2. Action - Calling a tool to get information
  3. 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 None

Hands-on Practice

In the notebook, you will:

  1. Define Tools - Create calculate and search functions
  2. Design System Prompt - Craft instructions for the LLM
  3. Implement Parser - Extract actions from LLM output
  4. Build Agent Class - Put it all together in ReActAgent
  5. 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.

IssueProblem
Fragile ParsingRegex breaks if LLM output varies
No Type SafetyArguments aren't validated
Error HandlingTool 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