Advanced Prompting Frameworks (ReAct, ToT)
# CHAPTER 19
Advanced Prompting Frameworks (ReAct, ToT)
1. Introduction
Chain-of-Thought (CoT) prompting taught the AI to think step-by-step. But what if the problem requires the AI to search the internet, test a hypothesis, realize it made a mistake, and try a different path? To achieve autonomous, high-level reasoning, researchers have developed complex Prompting Frameworks. In this chapter, we will explore the bleeding edge of Prompt Engineering: ReAct and Tree of Thoughts (ToT).2. Learning Objectives
By the end of this chapter, you will be able to:- Understand the limitation of linear reasoning in LLMs.
- Implement the ReAct (Reason + Act) framework for Agentic AI.
- Explain the Tree of Thoughts (ToT) methodology.
- Identify when to use advanced frameworks vs. standard prompts.
3. Beginner-Friendly Explanation
Imagine you lose your keys. Standard AI: Guesses immediately. "They are in the car." (Usually wrong). Chain-of-Thought AI: Thinks linearly. "I had them this morning. I walked to the kitchen. Therefore, they are in the kitchen." (Better, but still guessing). Tree of Thoughts AI: Brainstorms multiple paths. "Path 1: Kitchen. Path 2: Bedroom. Let me evaluate Path 1. The kitchen makes sense. Let me evaluate Path 2. I didn't go in the bedroom. I will abandon Path 2 and follow Path 1." ReAct AI: Thinks AND acts. "Thought: I need to find the keys. Action: Walk to kitchen. Observation: Keys are not here. Thought: I was wrong. Action: Walk to car. Observation: Keys found." Advanced frameworks turn the AI from a static text-generator into a dynamic, experimenting scientist.4. ReAct (Reasoning and Acting)
ReAct is the framework used to build AI Agents (like AutoGPT). It forces the AI into a strict, repetitive loop: Thought, Action, Observation.The prompt provides the AI with "Tools" (like Google Search or a Calculator).
The ReAct Prompt Loop:
*Why it works:* The AI is fact-checking itself in real-time by interacting with the outside world, completely eliminating factual hallucinations.
5. Tree of Thoughts (ToT)
When a problem requires deep creativity or logic (like writing a complex software algorithm or solving a puzzle), linear thinking fails. Tree of Thoughts forces the AI to brainstorm multiple different solutions, score them, and explore the best ones.The ToT Prompt Structure:
*Why it works:* By forcing the AI to critique its own ideas *before* committing to an answer, you simulate high-level human problem-solving.
6. Meta-Prompting (Self-Correction)
A simpler advanced technique is Meta-Prompting, where the AI critiques its own output in a loop.Never accept the AI's first draft of code. Always pass it through a Meta-Prompting review.
7. Python Example: The ReAct Loop
Developers use loops to execute ReAct. (Note: Frameworks like LangChain automate this entirely).8. Mini Project
The Critic Prompt: You want the AI to write a marketing slogan for a new energy drink. Instead of just asking for a slogan, write a Tree of Thoughts style prompt that asks the AI to generate 3 slogans, critique them based on target audience appeal, and then pick the winner. *(Answer Example: "Task: Write a slogan for an extreme energy drink. Step 1: Generate 3 entirely different slogans. Step 2: Critique each slogan based on its appeal to extreme sports athletes. Identify the weaknesses in each. Step 3: Based on your critique, select the best slogan and refine it into the Final Answer.")*9. Best Practices
- Token Awareness: Advanced frameworks (especially ToT) burn through massive amounts of tokens because the AI generates paragraphs of brainstorming and critique before giving the final answer. Use these frameworks *only* for highly complex tasks, not simple requests.
10. Common Mistakes
- Infinite Loops in ReAct: If an AI Agent is poorly prompted, it might get stuck in an infinite loop (e.g., searching the same Google phrase over and over and getting confused). Developers must implement "Max Iteration" limits (e.g., stop the loop after 5 actions) to prevent the AI from running forever and racking up API bills.
11. Exercises
- 1. Explain the difference between linear "Chain-of-Thought" prompting and the branching logic of "Tree of Thoughts."
12. MCQs with Answers
What does the "ReAct" prompting framework force the AI to do?
Why is the "Tree of Thoughts" (ToT) framework effective for highly complex logical problems?
13. Interview Questions
- Q: Describe the ReAct framework. How does integrating real-time "Observations" from external tools solve the LLM hallucination problem?
- Q: In an enterprise environment, when would you justify the high token cost of using a Tree of Thoughts (ToT) prompt architecture over a simple Chain-of-Thought (CoT) prompt?