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 onlyAct onlyReAct
Reasons from memoryCalls tools with no planThink, act, look at the result, repeat
Invents prices and stock levelsSearches again and againThe 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.

StepThe LLM thinksActionObservation
1Find chargerssearch_products3 listings
2One arrives too late, does VoltMate fitcheck_compatYes, it fits
3Add it and see the full totaladd_to_cartTotal 47.85
4Under 50, arrives ThursdaycheckoutOrder placed after you approve
5DonefinishNone

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 history

Notes:

  • call has 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).