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
- ReAct: Reason + Act Pattern
- Implementing ReAct from Scratch
- Common Tool Sets (Web, Calculator, RAG)
- Detecting and Recovering from Tool Errors