The ReAct pattern: Reason plus Act
Giving an AI access to tools so it can make API calls and execute its own code is a nightmare scenario for anyone who values constrained environments. The ReAct pattern, short for Reason+Act, addresses this by implementing additional actions an LLM can take — searching Wikipedia, running calculations, and more — then teaching it how to request those actions and feeding their results back into the model. The pattern is described in a paper that formalizes the loop of thought, action, observation, and answer.
ChatGPT's price drop and AI loops
Matt Webb highlighted the practical value of this approach in a piece about AI in a loop. He noted that ChatGPT's price being one-tenth that of GPT-3 makes it an ideal candidate for cheaply implementing ReAct. Matt used LangChain for his implementation, while Simon Willison decided to build the pattern from scratch on top of his tiny Python wrapper for the ChatGPT API. The resulting code — roughly three dozen lines — demonstrates how little code is needed to open up tool use for an LLM.
Wikipedia search action in practice
The implementation gives the LLM access to three concrete actions. The first is wikipedia: followed by a search term, which queries the Wikipedia API and returns the snippet from the first result. In the example, querying "What does England share borders with?" produces the Thought "I should list down the neighboring countries of England," then the Action "wikipedia: England," and finally an Observation summarizing that England shares land borders with Wales to its west and Scotland to its north.
Simon's blog search action
The second action, simon_blog_search:, searches Willison's personal blog and returns the truncated text of the first matching result. When asked "Has Simon been to Madagascar?", the LLM triggers "simon_blog_search: Madagascar" and the Observation confirms he visited the Pirates Museum in Antananarivo, a niche museum listed on the site. The search queries a Datasette instance running Willison's blog data, using full-text search against blog entries.
The calculate action and eval risk
The third action, calculate:, evaluates a mathematical expression using Python's eval() function. One example sends "calculate: 15 * 25" as the Action, the Observation returns 375, and the final Answer states "Fifteen times twenty five equals 375." The implementation explicitly flags this as dangerous, noting that eval should absolutely use a WebAssembly sandbox instead of direct Python evaluation. The risk comes from arbitrary code execution if an LLM is tricked into evaluating malicious input.
Building the Python loop from scratch
The core of the implementation is a query() function that drives a loop of Thought, Action, PAUSE, and Observation. A ChatBot class handles sending messages to the OpenAI API and receiving replies. A regular expression action_re parses lines starting with "Action:" to extract the action name and input. The loop continues until no action line is found, at which point the assistant outputs an Answer.
The available actions live in a known_actions dictionary mapping strings to handler functions. The wikipedia() function constructs an httpx request to the Wikipedia API with search parameters and extracts the first result's snippet. The simon_blog_search() function queries Willison's Datasette instance, running a full-text search against blog entries and returning the first 1,000 characters of the matched body text. The calculate() function simply calls Python's eval() on the provided expression string.
action_re = re.compile('^Action: (\w+): (.*)$')
def query(question, max_turns=5):
i = 0
bot = ChatBot(prompt)
next_prompt = question
while i < max_turns:
i += 1
result = bot(next_prompt)
print(result)
actions = [action_re.match(a) for a in result.split('\n') if action_re.match(a)]
if actions:
action, action_input = actions[0].groups()
if action not in known_actions:
raise Exception("Unknown action: {}: {}".format(action, action_input))
print(" -- running {} {}".format(action, action_input))
observation = known_actions[action](action_input)
print("Observation:", observation)
next_prompt = "Observation: {}".format(observation)
else:
return
The prompt handed to the model defines the ReAct loop format, listing the available actions with brief descriptions and providing an example session showing how the model should structure its responses. The prompt instructs the model to use Thought to describe its reasoning, Action to invoke one of the available actions and return PAUSE, and then output an Answer at the end of the loop.
known_actions = {
"wikipedia": wikipedia,
"calculate": calculate,
"simon_blog_search": simon_blog_search
}
What developers take away
The ReAct pattern shows that giving an LLM tool use capability does not require complex frameworks. Willison's implementation proves the pattern can be assembled in a few dozen lines of Python on top of a minimal API wrapper. For developers, the key considerations are defining clear, safe actions, parsing the model's output reliably, and handling the observation loop carefully. The calculate action's use of eval highlights why sandboxing matters — any tool exposed to an LLM must be constrained to prevent unintended code execution. The pattern's value lies in its simplicity: an LLM can be guided to reason about when a tool is needed, request it, and incorporate the result into its final answer, all within a structured loop that is straightforward to implement and adapt.