ReAct (Reason and Act, Yao et al. 2023) is the way an LLM chooses its next step in an agent: it alternates between thinking and doing.
Comparison
| Think only | Act only | ReAct |
|---|---|---|
| Reasons from memory | Calls tools with no plan | Think, act, look at the result, repeat |
| Invents prices and stock levels | Searches again and again | The results correct the thinking |
Today modern LLMs call tools natively. The action is a structured tool call and the thinking happens inside the model, so most agent frameworks run this same loop by default.
Remembering tip
Cooking from a recipe while tasting as you go: decide the next step, do it, taste, adjust. A single LLM call is cooking without tasting.
One turn of the loop
- the LLM reads the goal and the history, and decides the next action
- the code runs the tool
- the shop sends back a result
- the result goes back to the LLM as part of the history
Worked example
Goal: buy a 65 W USB-C charger under 50 Singapore dollars, delivered by Friday. The agent has 4 tools: search_products, check_compat, add_to_cart and checkout.
| Step | The LLM thinks | Action | Observation |
|---|---|---|---|
| 1 | Find chargers | search_products | 3 listings |
| 2 | One arrives too late, does VoltMate fit | check_compat | Yes, it fits |
| 3 | Add it and see the full total | add_to_cart | Total 47.85 |
| 4 | Under 50, arrives Thursday | checkout | Order placed after you approve |
| 5 | Done | finish | None |
This is 4 tool calls and a finish. Any step could go wrong, the history grows at each step, and step 1 already showed the LLM a listing with hidden text, see Shopping Agent.
Code
The bare loop: the LLM proposes a tool call, the code runs it, and the result is added to the history. The LLM is any function that maps the goal and the history to the next tool call.
def run_naive(goal, llm, shop, max_steps=100):
"""The bare loop: the LLM proposes, code runs it. No checks of any kind."""
tools, history = make_tools(shop), []
for step in range(max_steps):
call = llm(goal, history) # the LLM proposes the next action
if call["name"] == "finish":
history.append({"call": call, "obs": call["arguments"]["answer"]})
break
obs = tools[call["name"]](**call["arguments"]) # our code executes it
history.append({"call": call, "obs": obs}) # the result goes back to the LLM
return historyNotes:
callhas the form{"name": "search_products", "arguments": {"query": "65W USB-C charger"}}make_tools(shop)returns a dict that maps each tool name to a method of the shop- the loop still stops after
max_steps, so it can never hang - a mock LLM that reads the goal and the history and returns the next tool call lets everyone get the same trace, and a real LLM can replace it with native function calling
Before the loop is used, the LLM must learn to write correct tool calls, see Supervised Fine-Tuning (SFT).