6.2.2AI Agents & Tool Use

ReAct (reasoning + acting) framework

2,594 words12 min readdifficulty · medium1 backlinks

What Is ReAct?

Why This Architecture?

Pure chain-of-thought (CoT) can reason deeply but cannot interact with external knowledge or tools—it's trapped in its training data. Pure action agents can use tools but often hallucinate or make britle decisions without explicit reasoning. ReAct gets both:

  1. Grounding: Observations from real tools prevent hallucination
  2. Transparency: Reasoning traces make decisions interpretable
  3. Error recovery: The agent can recognize mistakes in observations and change course
  4. Dynamic planning: Each thought can revise the plan based on what was just learned

The ReAct Loop: From First Principles

Let's derive how ReAct operates step-by-step.

Starting point: You have a question QQ and a set of available tools T={t1,t2,,tn}\mathcal{T} = \{t_1, t_2, \ldots, t_n\} (e.g., search, calculator, database).

Goal: Generate answer AA by alternating reasoning and acting.

Step 1: Initial Reasoning

The LM receives the question and generates a thought Thought1\text{Thought}_1:

Thought1simPLM(thoughtQ,examples)\text{Thought}_1 sim P_{\text{LM}}(\text{thought} \mid Q, \text{examples})

This thought analyzes what information is needed. Example: "To find the population of Paris in 2023, I need to search for current demographic data."

Why this step? Without reasoning first, the agent might search for the wrong thing or not know why it's searching.

Step 2: Action Selection

Based on Thought1\text{Thought}_1, the LM choses an action a1a_1 from the tool set:

a1=argmaxaTPLLM(aQ,Thought1)a_1 = \arg\max_{a \in \mathcal{T}} P_{\text{LLM}}(a \mid Q, \text{Thought}_1)

Example: Action: Search["Paris population 2023"]

Why this step? Reasoning without action is useless for questions requiring external knowledge. The action must match the thought's intent.

Step 3: Environment Observation

The environment (tool executor) runs a1a_1 and returns observation o1o_1:

o1=Execute(a1)o_1 = \text{Execute}(a_1)

Example: Observation: Paris had a population of approximately 2.1 million in 2023 within city limits.

Why this step? The observation provides grounding—real data that the LLM's parametric knowledge might lack or get wrong.

Step 4: Reasoning with New Information

The LLM now generates Thought2\text{Thought}_2 conditioned on everything so far:

Thought2PLLM(thoughtQ,Thought1,a1,o1)\text{Thought}_2 \sim P_{\text{LLM}}(\text{thought} \mid Q, \text{Thought}_1, a_1, o_1)

Example: "The city limits population is 2.1M, but the question might be asking about the metropolitan area. I should search for that."

Why this step? The agent uses the observation to refine its understanding. This is where error recovery happens—if o1o_1 was unexpected, Thought2\text{Thought}_2 can pivot.

Step 5: Iteration Until Done

Repeat steps 2-4, building a trajectory:

τ=[(Thought1,a1,o1),(Thought2,a2,o2),,(ThoughtT,aT,oT)]\tau = [(\text{Thought}_1, a_1, o_1), (\text{Thought}_2, a_2, o_2), \ldots, (\text{Thought}_T, a_T, o_T)]

The loop terminates when the LLM generates:

aT=Finish[answer]a_T = \text{Finish}[\text{answer}]

Example: Action: Finish[The population of Paris is 2.1 million in the city and 12.4 million in the metro area as of 2023]

Mathematical Formulation

The probability of the entire trajectory given the question is:

P(τQ)=t=1TP(ThoughttQ,τ<t)P(atQ,τ<t,Thoughtt)P(otat)P(\tau \mid Q) = \prod_{t=1}^{T} P(\text{Thought}_t \mid Q, \tau_{<t}) \cdot P(a_t \mid Q, \tau_{<t}, \text{Thought}_t) \cdot P(o_t \mid a_t)

Where:

  • P(Thoughtt)P(\text{Thought}_t \mid \cdot) is the LM's reasoning distribution
  • P(at)P(a_t \mid \cdot) is the action selection distribution
  • P(otat)P(o_t \mid a_t) is deterministic (the environment's response)

Why this matters: This factorization shows that ReAct is a sequential decision process where each step depends on all prior steps. The LLM is doing multi-step inference, not just one-shot generation.

Concrete Example: Multi-Hop Question

Question: "What is the capital of the country where the Eiffel Tower is located?"

Trajectory:

Thought 1: I need to find which country the Eiffel Tower is in. Action 1: Search["Eiffel Tower location"] Observation 1: "The Eiffel Tower is in Paris, France."

Why this step? The agent identifies it lacks country information and uses search to ground its knowledge.

Thought 2: Now I know it's in France. I need to find France's capital. Action 2: Search["capital of France"] Observation 2: "Paris is the capital of France."

Why this step? The observation from Action 1 enables a dependent second query. This is multi-hop reasoning—each step builds on the last.

Thought 3: I have all information needed. Paris is both the city with the Eiffel Tower and France's capital. Action 3: Finish["Paris"]

Why this step? The agent recognizes the task is complete and synthesizes the answer from accumulated observations.

Contrast with Pure CoT

A pure CoT approach would try to answer from memory:

Let me think step by step:
1. The Eiffel Tower is in Paris
2. Paris is in France (I think?)
3. The capital of France is Paris
Answer: Paris

Risk: If the LLM halucinates step 2 (e.g., says "Paris is in Belgium"), there's no correction mechanism. ReAct's observations enforce correctness.

Common Mistakes and How to Fix Them

When ReAct Fails

ReAct is not magic. It struggles with:

  1. Poor tool design: If tools return noisy or irrelevant observations, the agent can't recover. Garbage in, garbage out.
  2. Ambiguous questions: If the question is underspecified, the agent might not know what to search for. Reasoning traces expose this, but don't solve it.
  3. Long trajectories: Each thought-action-observation cycle uses tokens. Very long tasks (20+ steps) can exceed context limits or become expensive.
  4. Irreducible hallucination: If the LM misinterprets a correct observation (e.g., reads "2.1 million" as "21 million"), ReAct can't fix that—it's a model capability issue.

Connections to Other Concepts

  • Chain-of-Thought (CoT): ReAct extends CoT by adding actions. CoT is "think then answer"; ReAct is "think, act, observe, think, act, ..."
  • Tool Use in LLMs: ReAct is a structured framework for tool use, ensuring tools are called for good reasons (the thoughts).
  • Agent Architectures: ReAct is a specific agent loop. Compare with Reflexion (self-reflection) and MRKL (modular reasoning).
  • Self-Consistency: ReAct can be combined with self-consistency by sampling multiple trajectories and picking the most common answer.
  • Retrieval-Augmented Generation (RAG): ReAct often uses search as a tool, making it a form of interactive RAG where the agent decides when to retrieve.
Recall Explain to a 12-Year-Old

Imagine you're solving a mystery. You don't just sit and think about the answer—you think "I need to check the library," then you go to the library, find a book, read it, and then think "Okay, now I know the villain was in Paris, so I need to search Paris records." You keep going back and forth between thinking and doing.

That's ReAct! A computer (AI) thinks about what it needs to know, does something to find out (like searching Google or doing math), looks at what it found, then thinks again. If it finds something surprising, it can change its plan. It's like having a smart assistant who explains their thinking and actually looks things up instead of guessing.


#flashcards/ai-ml

What are the three components of a ReAct loop? :: Thought (reasoning trace), Action (tool call), and Observation (environment feedback).

Why does ReAct interleave reasoning and acting instead of doing all reasoning first?
Observations from actions provide grounding and new information that refines reasoning. Reasoning on incomplete information (before acting) leads to hallucination or wrong plans.
What is the key advantage of ReAct over pure Chain-of-Thought?
ReAct can interact with external tools and use real observations to ground its reasoning, preventing hallucination and enabling access to knowledge beyond training data.
What is the key advantage of ReAct over pure action-only agents?
Explicit reasoning traces make decisions interpretable, enable error recovery (the agent can recognize bad observations and change course), and guide action selection based on logical analysis.
In the ReAct formula P(τQ)=t=1TP(Thoughtt)P(at)P(ot)P(\tau \mid Q) = \prod_{t=1}^{T} P(\text{Thought}_t) \cdot P(a_t) \cdot P(o_t), what does P(otat)P(o_t \mid a_t) represent?
The deterministic environment response—given action ata_t, the environment returns observation oto_t. This is not probabilistic for the agent; it's the real world's answer.
What is a multi-hop question in ReAct context?
A question requiring multiple dependent reasoning-action steps, where each action's observation informs the next action. E.g., "Capital of the country where X is located" needs: find country of X, then find capital of that country.
How does ReAct handle errors or unexpected observations?
The next Thought step can acknowledge the unexpected observation and revise the plan. E.g., if a search returns "No results," the agent can think "I need to rephrase my query" and try a different action.
When does a ReAct trajectory terminate?
When the agent generates Finish[answer], indicating it has sufficient information to answer the question.
What is the "hallucination-then-act" problem that ReAct solves?
Action-only agents might hallucinate that a tool returned certain information and act on that false belief. ReAct's explicit observations force the agent to work with actual tool outputs, not imagined ones.
Why must each ReAct action be preceded by a thought?
The thought justifies why the action is needed, making decisions transparent and enabling the agent to choose the right tool with the right input. Without it, the agent is acting blindly.

Concept Map

cannot use tools

no reasoning

combines

combines

interleaves

guides

executes tool

returns

refines

prevents

makes decisions

detect mistakes

revise plan

Chain-of-Thought only

Trapped in training data

Action-only agents

Brittle decisions

ReAct framework

Thought

Action

Environment

Observation

Grounding no hallucination

Transparency

Error recovery

Hinglish (regional understanding)

Intuition Hinglish mein samjho

ReAct framework ek bahut powerful idea hai AI agents ke liye. Socho agar tumhe koi sawaal solve karna hai jiska answer tumhe pata nahi, toh tum kya karoge? Pehle sochoge "mujhe kya information chahiye," phir actually jake Google pe search karoge ya calculator use karoge, jo milega usse dekhoge, phir wapas sochoge "acha ab next kya karna hai." Yahi ReAct ka concept hai—AI model bhi exactly yehi karta hai.

Traditional LMs sirf apne dimag (training data) pe depend karte the. Agar unhe 2023 ki Paris ki populationucho aur training 2021 mein ruki thi, toh wo guess karenge ya purana data denge. Lekin ReAct framework mein, AI sochta hai "mujhe latest data chahiye," phir actual search tool call karta hai, real result pata hai (observation), phir us result ko dekhke next decision leta hai.Isse hallucination (galat guess) bahut kam hoti hai kyunki har step pe real-world se feedback mil raha hai.

Ye approach multi-hop questions ke liye bhi perfect hai jahan ek answer dosre question ko unlock karta hai. For example, "Eiffel Tower jis country mein hai uski capital kya hai?"—pehle find karo country (France), phir uski capital (Paris). ReAct mein AI transparent tareke se har step explain karta hai: "Thought: mujhe country find karni hai. Action: Search karo. Observation: France mila. Thought: Ab France ki capital find karo."Isse debugging aur trust dono easy ho jate hain.

Iska real-world impact bada hai—customer support bots, research assistants, coding agents sab isse better ban sakte hain kyunki wo na sirf blindly act karte hain, na sirf hallucinate karte hain, balki think-act-learnrepeat ka cycle follow karte hain jaise ek intelligent human karta hai.

Go deeper — visual, from zero

Test yourself — AI Agents & Tool Use

Connections