Do Agents Think of Electric Sheep? The ReAct Framework

Introduction to AI Agents

Adel Nehme

VP of AI Curriculum, DataCamp

The Different Type of Model Thoughts

1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Planning "To help them move apartments, I'll need to find moving companies, compare prices, check availability for their date"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Planning "To help them move apartments, I'll need to find moving companies, compare prices, check availability for their date"
Analysis "Looking at their spending patterns, they're overpaying for subscriptions they rarely use"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Planning "To help them move apartments, I'll need to find moving companies, compare prices, check availability for their date"
Analysis "Looking at their spending patterns, they're overpaying for subscriptions they rarely use"
Decision-making "Since they need it by tomorrow, I should suggest express shipping despite the extra cost"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Planning "To help them move apartments, I'll need to find moving companies, compare prices, check availability for their date"
Analysis "Looking at their spending patterns, they're overpaying for subscriptions they rarely use"
Decision-making "Since they need it by tomorrow, I should suggest express shipping despite the extra cost"
Problem-solving "To optimize this code, I should first profile it to identify bottlenecks"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Memory integration "They mentioned being lactose intolerant last week, so I'll exclude dairy from these recipe suggestions"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Memory integration "They mentioned being lactose intolerant last week, so I'll exclude dairy from these recipe suggestions"
Self-reflection "I was too technical in my explanation - let me simplify this using everyday analogies"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Memory integration "They mentioned being lactose intolerant last week, so I'll exclude dairy from these recipe suggestions"
Self-reflection "I was too technical in my explanation - let me simplify this using everyday analogies"
Goal-setting "Before planning their workout routine, I need to understand their fitness level and available time"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The Different Type of Model Thoughts

Thought Example
Memory integration "They mentioned being lactose intolerant last week, so I'll exclude dairy from these recipe suggestions"
Self-reflection "I was too technical in my explanation - let me simplify this using everyday analogies"
Goal-setting "Before planning their workout routine, I need to understand their fitness level and available time"
Prioritization "They should book the flights before the hotel, since flight prices increase faster"
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
Introduction to AI Agents

The ReAct Framework: Where Thoughts Come From

react

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
Introduction to AI Agents

The ReAct Framework: Reasoning and Acting

Reasoning

Acting

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
Introduction to AI Agents

The ReAct Framework: Reasoning and Acting

Reasoning

Chain of thought prompting

[Prompt...]. Think step by step.

Acting

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
Introduction to AI Agents

The ReAct Framework: Reasoning and Acting

Reasoning

Chain of thought prompting

[Prompt...]. Think step by step.

Acting

Thought > Action > Observation examples

[Prompt...]. Follow this format:

  • Thought: [Think about what to calculate first]
  • Action: [Perform calculation]
  • Observation: [Result of calculation]
  • ...repeat as needed...
  • Final Answer: [Complete solution]
1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
Introduction to AI Agents

ReAct in Action: Simple Arithmetic

gpt-4o

Introduction to AI Agents

ReAct in Action: Simple Arithmetic

$$

The question

Calculate the total cost if I buy 3 laptops at $899 each with a 15% discount and 8% sales tax

$$

The correct answer

$2475.85

Introduction to AI Agents

ReAct in Action: Simple Arithmetic

incorrect

Prompt used

Calculate the total cost if I buy 3 laptops at $899 each with a 15% discount and 8% sales tax. Only return the answer.

Introduction to AI Agents

ReAct in Action: Simple Arithmetic

correct

Prompt used

Calculate the total cost if I buy 3 laptops at $899 
each with a 15% discount and 8% sales tax. Think step by step.

Follow this format:

Thought: [Think about what to calculate first]
Action: [Perform calculation]
Observation: [Result of calculation]
...repeat as needed...
Final Answer: [Complete solution]

Example:

Thought: Calculate base cost first
Action: 2 × $50 = $100
Thought: Apply 10% discount
Action: $100 - ($100 × 0.10) = $90
Final Answer: Total is $90
Introduction to AI Agents

Without ReAct Prompting

incorrect_answer

The correct answer $2475.85

ChatGPT answer: $2,776.63

Introduction to AI Agents

With ReAct Prompting

correct_answer

The correct answer $2475.85

ChatGPT answer: $2475.85

Introduction to AI Agents

ReAct as a Way of Thinking

ReAct is part of the model's system prompt

System prompt are hidden instructions that tell the model how to behave throughout all conversations.

1 Image generated with GPT-4o
Introduction to AI Agents

Reasoning Models and ReAct

$$

  • ReAct is especially useful on "traditional" language models like the GPT-series of models
  • Newer generation reasoning models are explicitly trained to think step by step, and don't need ReAct prompting

$$ Example of reasoning models

  • OpenAI o-series of models
  • DeepSeek R-series of models
  • Gemini thinking models
Introduction to AI Agents

How to Spot a Reasoning Model

thinking_models

Introduction to AI Agents

Let's Practice!

Introduction to AI Agents

Preparing Video For Download...