Back to blog
4 min read

Building ReAct: A Simple Python Implementation

Link:

A simple Python implementation of the ReAct pattern for LLMs

Synopsis:

Shows how to:

  • Implement ReAct pattern in Python
  • Connect LLMs to external tools
  • Build thought-action-observation loops
  • Create practical tool integrations

Context

The ReAct pattern enables LLMs to interact with external tools through a structured thought-action-observation loop.

This implementation is notable because it demonstrates how to build this capability with minimal code (~100 lines of Python), making it accessible for developers who want to extend their LLMs with external tool access.

The timing is particularly relevant as organizations look to integrate LLMs with existing tools and services.

Key Implementation Patterns

The implementation demonstrates three key patterns:

  1. Core ReAct Components
  • Thought-Action-Observation loop (e.g., “I should look this up” → Wikipedia search → process result)
  • Tool integration via simple functions
  • Natural language interface to tools
  • Structured response format
  1. Tool Integration
  • Wikipedia search (for general knowledge)
  • Blog content search (for specific domain knowledge)
  • Calculator function (for mathematical operations)
  • Extensible action framework (for adding new tools)
  1. Implementation Approach
  • Minimal Python code (~100 lines)
  • ChatGPT API integration
  • Simple regex pattern matching
  • Exception handling for unknown actions

These patterns reveal important strategic considerations for teams implementing ReAct.

Strategic Implications

For technical leaders, this implementation suggests several key implications:

  1. Architecture Design
  • Simple yet powerful pattern (~100 lines enables complex tool interactions)
  • Easy tool integration (new tools require just a function definition)
  • Natural language interfaces (no complex API design needed)
  • Flexible action framework (supports diverse tool types)
  1. Development Approach
  • Start with basic tools
  • Add complexity gradually
  • Focus on error handling
  • Build for extensibility
  1. System Capabilities
  • External tool access
  • Reasoning about actions
  • Multi-step problem solving
  • Self-directed task completion

To translate these implications into practice, teams need a straightforward implementation approach.

Implementation Framework

For teams building ReAct systems, the framework involves:

  1. Foundation Setup
  • Implement basic ChatBot class
  • Define tool integration interface
  • Create an action parser
  • Build response handler
  1. Tool Integration
  • Define tool functions
  • Add error handling
  • Implement action registry
  • Handle response formatting
  1. System Management
  • Monitor tool usage
  • Track API costs
  • Handle errors gracefully
  • Manage conversation flow

This implementation framework leads to clear development strategies.

Development Strategy

Key development considerations include:

  1. Keep implementation simple
  • Focus on tool integration
  • Use structured prompts
  • Handle errors gracefully
  1. Tool Design
  • Simple function interfaces
  • Clear input/output formats
  • Flexible action framework
  • Easy extensibility
  1. System Considerations
  • API cost management
  • Response validation
  • Safe tool execution
  • Conversation management

While these technical patterns are valuable, their significance becomes more apparent when considering practical applications.

Personal Notes

The simplicity of this implementation is inspiring because it shows how just 100 lines of Python code enables an LLM to reason about and use multiple external tools.

The ReAct pattern demonstrates how to combine LLM reasoning with traditional tools effectively, suggesting a future where AI systems routinely orchestrate existing software tools.

Looking Forward

ReAct implementations will likely evolve to include the following:

  • More sophisticated tools (database access, API integrations)
  • Better error handling (retry logic, fallback options)
  • Enhanced safety measures (sandboxing, access controls)
  • Standardized patterns for tool integration

The future of AI engineering may involve more such patterns that blend LLM capabilities with traditional software tools, creating robust hybrid systems that combine AI reasoning with practical utilities.