ReAct pattern fundamentals

LLMs can think, but they cannot act. They cannot search the web, run code, or query a database. The ReAct pattern (Reason + Act) fixes this by giving the model a loop: Think about what to do, choose a tool, observe the result, then think again. This is the foundation of every AI agent.

Tools are functions the model can call by outputting structured text. Think of them as giving the AI hands to interact with external systems: a search tool queries the web, a calculate tool evaluates math, and a respond tool sends the final answer to the user.

Exactly, the model outputs structured text like "Action: search(weather in Paris)" and your code parses that text to execute the real function. The model never calls tools directly. Your application reads the model output, extracts the tool name and arguments, runs the function, and feeds the result back as a new message.

The ReAct loop has 4 steps: Thought: "I need to find the current weather in Paris." Action: search("weather in Paris") Observation: "Paris: 18C, partly cloudy" Thought: "I now have the weather. The user asked for a packing suggestion." Action: respond("Pack a light jacket and sunglasses.") The model reasons about what tool to use, executes it, observes the result, and repeats until the task is complete.

12_react_prompting.py
python
react_system = """You have access to these tools:
- search(query): Search the web
- calculate(expr): Evaluate a math expression
- respond(text): Send final answer to the user

For each step, output:
Thought: your reasoning
Action: tool_name(arguments)

Wait for the Observation, then continue.
When you have the final answer, use respond()."""

The system prompt defines available tools and the Thought/Action/Observation format. The model must follow this structure so we can parse and execute its tool calls.

The loop works like a conversation: the model outputs a Thought and Action, we execute the tool and send the Observation back as a user message, and the model continues reasoning. The loop ends when the model calls respond() with the final answer.

12_react_prompting.py
python
def react_loop(question):
    messages = [
        {"role": "system", "content": react_system},
        {"role": "user", "content": question},
    ]
    for step in range(5):  # max 5 steps
        response = get_completion_messages(messages)
        action = parse_action(response)
        if action.tool == "respond":
            return action.args
        observation = execute_tool(action)
        messages.append({"role": "assistant", "content": response})
        messages.append({"role": "user",
                         "content": f"Observation: {observation}"})

Each iteration: get model response, parse the action, execute the tool, and append the observation. The max 5 steps prevents infinite loops if the model gets stuck.

Quiz: Quiz

Loading practice…