Chapter 4. The Agent Loop and ReAct
Repeat the tool round trip · Write a reasoning step before each action
In Chapter 3, the program ran one tool and returned its result to the model. Some questions need several tool calls. The model can select the next call only after it sees a result. This chapter shows two methods:
- The agent loop repeats the tool round trip until the model gives an answer.
- ReAct adds a written reasoning step before each action.
Part 1. The agent loop
1.1 Why a loop
One round trip gives one result. But one result can give only part of the answer. For example: “Where was the author of the novel born?” The first search gives the name of the author. The second search gives the birthplace. The model can write the second request only after it sees the first result. For this, the program must repeat the round trip.
1.2 How the loop works
- Call the model. The program sends the conversation: the instructions, the question, and all earlier requests and results.
- Read the response. The response is a tool request or a final answer.
- Run the tool. If the response is a request, the program runs the tool.
- Save both. The program adds the request and the result to the conversation. Then it goes back to step 1.
The saved observation connects one round to the next. Without it, the next model call does not know what the tool returned.
The model selects each action, but it does not run the tools. The program runs the tools and keeps the conversation.
1.3 Stop conditions
An agent loop has two stop conditions:
- The model gives a final answer. In the lab, the model selects the operation
finishwith its answer. - The number of model calls gets to a limit. Then the program reports that there is no answer.
Check 1 · Save the observation
The program runs the tool but saves only the request of the model. What does the next model call not receive?
Read the answer
The observation: the result of the tool. Without it, the model cannot use that result to select the next action or to answer.
Part 2. ReAct
2.1 Why write a reasoning step
In the basic loop, the model writes only the next action. The response does not show why the model selects that action. A written assessment helps the model keep track of what it knows and what it does not know yet. ReAct adds this assessment before each action.
2.2 Thought, Action, Observation
Each round of ReAct has three parts:
| Part | Meaning | Who writes it |
|---|---|---|
| Thought | An assessment: what the evidence shows, what is not known yet, and what to do next | The model |
| Action | The tool request | The model |
| Observation | The result of the action | The program, which runs the tool |
The Thought and the Action are in one model response. The Observation comes after the program runs the tool. The program saves all three parts. The next Thought can then use the new observation.
2.3 What ReAct changes
ReAct does not change the loop. The program still runs the tools, saves the results, and stops at the same conditions. Only the model response changes: it contains a Thought and an Action.
Check 2 · Basic and ReAct
What does a ReAct response contain that a basic response does not contain?
Read the answer
A Thought: a written assessment of the evidence so far and of the next action.
Summary
- The agent loop repeats the round trip: call the model, run the requested tool, and save the request and the result.
- An observation is the result of a tool that the program saves. The next model call reads it.
- A stop condition ends the loop: a final answer or a call limit.
- ReAct adds a Thought before each Action.
- ReAct does not change the loop. It changes only the content of the model response.
Lab preparation: from concept to code
The lab uses the OpenAI SDK and a shop database. The model has one tool, act. Its field action names an operation: run_sql, calculate, or finish. Its field action_input holds the input. For ReAct, the lab adds one paragraph to the instructions and a thought field to the tool request.
| Concept | Lab code (short form) |
|---|---|
| Call the model | response = client.chat.completions.create(model=MODEL, messages=messages, tools=tools, tool_choice="required") |
| Read the request | request = json.loads(message.tool_calls[0].function.arguments) |
| Run the tool | result = operations[request["action"]](request["action_input"]) |
| Save the request | messages.append(message.model_dump(exclude_none=True)) |
| Save the observation | messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)}) |
| Stop at an answer | if request["action"] == "finish" and "answer" in result: return ... |
| Stop at a limit | for turn in range(max_calls): |
| ReAct | run_loop(question, instructions=BASE + REACT, tools=action_tools(react=True)) |
Lab
- Do one exchange by hand: request, run, and save.
- Run the same steps as a loop with
run_loop. Find where the loop stops. - Add ReAct to the same question. Read how each Thought uses the last observation.
- Change the question to the average value of a completed order.
- Compare sales in February and March. Use the first result to select the next question.
Lab notebook in Colab · Download the notebook · Local Jupyter bundle
Homework: Follow the Evidence in a Sales Investigation uses the same loop on sales in January and February. See the Week 4 homework and submission instructions.
Materials and sources
- Yao et al., ReAct: Synergizing Reasoning and Acting in Language Models (2022) · Author project: reasoning steps and actions in one loop.
- OpenAI — Function calling guide: the message pattern for tool requests and tool results.
- Lab notebook · Lab bundle.