0PricingLogin
AI Agents · Lesson

Implementing ReAct from Scratch

Write a 100-line Python implementation without LangChain: parse Action lines, run tools, loop until Final Answer.

Build the Loop Yourself

You can write ReAct in ~100 lines of Python — no framework needed. Doing so once builds intuition for what frameworks abstract away.

Step 1: Define Tools

import json, math

def calculator(expression: str) -> str:
    try:
        return str(eval(expression, {'__builtins__': {}}, vars(math)))
    except Exception as e:
        return f'Error: {e}'

def search_wikipedia(query: str) -> str:
    # placeholder — call a real search API
    return f'Wikipedia summary for: {query}'

TOOLS = {
    'calculator': calculator,
    'search_wikipedia': search_wikipedia,
}

All lessons in this course

  1. ReAct: Reason + Act Pattern
  2. Implementing ReAct from Scratch
  3. Common Tool Sets (Web, Calculator, RAG)
  4. Detecting and Recovering from Tool Errors
← Back to AI Agents