Introduction
Welcome back. In our last lesson, we explored the theoretical underpinnings of the Tree-of-Thoughts (ToT) framework. You learned how it transforms problem-solving from a single, linear path (like Chain-of-Thought) into a deliberate tree search, complete with thought generation, state evaluation, and backtracking.
Today, we transition from theory to practice. This lesson is designed to help you implement a basic ToT pattern by prompting a model to generate and evaluate multiple reasoning paths. We won't be building a complex, fully-automated library from scratch. Instead, we'll focus on the core logic and prompt engineering required to execute the ToT pattern, first manually and then by examining the building blocks for automation. This hands-on perspective is a crucial step toward building the robust, sophisticated AI systems you aim to master.
1. A Manual ToT Workflow: Human-in-the-Loop Search
At its heart, ToT is an algorithm. But before we automate it, it's incredibly instructive to walk through the steps manually. This approach, where you act as the "search algorithm," guiding the LLM through the generation and evaluation phases, reveals the core mechanics of the framework.
A great way to see this in action is with open-ended, qualitative problems where the "thoughts" are ideas or strategies rather than mathematical steps.
ChatGPT-4 Prompt Engineering: The Tree of Thoughts Method - WOW!
The video 'ChatGPT-4 Prompt Engineering: The Tree of Thoughts Method' by the 'All About AI' channel provides an excellent, step-by-step demonstration of a manual ToT process for a practical problem: planning how to ask for a raise.
Please watch this video from the beginning to the end (around 10 minutes). As you watch, map the four phases presented in the video (Brainstorm, Evaluation, Expansion, Decision) to the four core ToT components we discussed in the last lesson: Thought Decomposition: How is the problem broken down? Thought Generation: Which prompt serves this role? State Evaluation: How are the generated thoughts assessed? Search Algorithm: Who or what is guiding the search from one step to the next?
As the video demonstrates, you can orchestrate a ToT process using a sequence of distinct prompts. Let's formalize this pattern:
The Four-Phase Prompting Pattern
-
Brainstorming (Thought Generation):
- Goal: Generate a diverse set of initial candidate solutions or paths.
- Prompt Structure: You ask the model to generate multiple distinct solutions, often encouraging variety by specifying different factors to consider.
- Example from video: "Could you brainstorm three distinct solutions? Please consider a variety of factors such as my background, timing, preparation..."
-
Evaluation (State Evaluation):
- Goal: Assess the quality and viability of each generated thought.
- Prompt Structure: You instruct the model to act as a critic, analyzing the pros, cons, potential challenges, and probability of success for each of the brainstormed solutions.
- Example from video: "For each of the three proposed solutions, evaluate their potential. Consider the pros and cons... Assign a probability of success..."
-
Expansion (Lookahead/Refinement):
- Goal: Deepen the analysis of each promising thought by exploring implementation details and potential obstacles.
- Prompt Structure: For each solution, you ask the model to generate potential scenarios, strategies, and ways to handle unexpected outcomes.
- Example from video: "For each solution, deep in the thought process, generate potential scenarios, strategies for implementation..."
-
Decision (Selection/Pruning):
- Goal: Synthesize all the analysis to rank the solutions and make a final, justified recommendation.
- Prompt Structure: You ask the model to rank the solutions based on the previous evaluation and expansion, providing a final justification.
- Example from video: "Based on the evaluations and scenarios, rank the solutions in order of promise. Provide a justification..."
This manual workflow is a powerful pattern for tackling complex, ill-defined problems. You, the user, act as the search algorithm, deciding which thoughts to expand and when to move to the final decision.
Test your understanding!
Imagine you're an AI engineer tasked with designing a new feature for a social media app to combat misinformation. How would you structure the Brainstorming and Evaluation prompts for a manual ToT session to explore this problem?
Show answer
Here's one possible approach:
-
Brainstorming Prompt:
"I need to design a feature to reduce the spread of misinformation on our platform. Please brainstorm four distinct approaches. Consider a variety of factors such as user experience, technical feasibility, potential for censorship bias, and scalability. The solutions should be conceptually different from each other (e.g., don't just suggest four variants of content moderation)." -
Evaluation Prompt:
"For each of the four proposed solutions, please provide a critical evaluation. For each, analyze the following:- Pros and Cons: What are the main benefits and drawbacks?
- Implementation Difficulty: Rate the engineering effort required (Low, Medium, High).
- Potential Challenges: What are the primary risks or failure modes (e.g., user backlash, adversarial attacks, performance issues)?
- Confidence Score (1-10): Give a score representing your confidence in the approach's overall effectiveness at reducing misinformation without harming user trust."
2. Towards Automation: Specialized Prompts for Structured Problems
The manual workflow is excellent for exploration, but for more constrained problems like the Game of 24, we can design highly specialized prompts to automate the Generate-Evaluate-Select loop. Your computer science background will be particularly useful in seeing how these prompts are essentially function calls in a larger search algorithm.
Let's examine the prompt engineering used to automate ToT for the Game of 24.
Understanding and Implementing the Tree of Thoughts ...
The Hugging Face blog post 'Understanding and Implementing the Tree of Thoughts ...' provides an excellent, code-oriented breakdown of the specific prompts used for the Game of 24. We will focus on the prompts themselves, as they are the core of the implementation.
Please read the section 'Game of 24'. You don't need to digest every line of code, but focus intensely on the prompts being constructed. Specifically, identify: The 'propose' prompt for generating intermediate thoughts. The 'value' prompt for evaluating intermediate thoughts (i.e., assessing if the remaining numbers can reach 24). The 'value' prompt for evaluating the final answer. The logic of the heuristic_calculator function, which translates the LLM's text output ('sure'/'likely'/'impossible') into a numerical score.
Let's break down the key implementation patterns from this resource.
Step 2a: The "Propose" Prompt (Constrained Thought Generation)
Instead of a broad "brainstorm," this approach uses a one-shot prompt to generate a list of concrete, valid next steps.
Example Propose Prompt:
Input: 2 8 8 14
Possible next steps:
2 + 8 = 10 (left: 8 10 14)
8 / 2 = 4 (left: 4 8 14)
...
Input: 1 1 1 8
Possible next steps:
When given to an LLM, this prompt elicits a list of potential operations, such as 1 + 1 = 2 (left: 1 2 8), which become the child nodes in our search tree. This is a far more structured form of thought generation than the open-ended brainstorming we saw earlier.
Step 2b: The "Value" Prompt (Heuristic State Evaluation)
This is the most critical part of automating ToT. We use a separate LLM call with a specifically designed few-shot prompt to act as a heuristic function, evaluating the "promise" of each generated thought.
Example Value Prompt (for intermediate thoughts):
Evaluate if given numbers can reach 24 (sure/likely/impossible)
10 14 -> 10 + 14 = 24 -> sure
11 12 -> ... -> impossible
...
3 8 ->
The LLM's response (sure, likely, or impossible) is then programmatically mapped to a numerical value (e.g., sure=20, likely=1, impossible=0.001). This score allows the search algorithm to quantitatively compare different branches of the tree and prune the unpromising ones.
Step 2c: The Search Logic
With these generation and evaluation prompts, you can write a simple program to execute the search. The logic for one step of a Breadth-First Search (BFS) would look like this in pseudocode:
# Assume 'queue' holds the most promising nodes from the previous level
new_queue = []
for node in queue:
# 1. GENERATE: Use the "propose" prompt with the numbers in 'node'
candidate_thoughts = generate_thoughts(node.state)
# 2. EVALUATE: For each candidate thought...
for thought in candidate_thoughts:
# Use the "value" prompt to get a score
score = evaluate_thought(thought)
# Create a new node with the thought and its score
new_node = TreeNode(state=thought, value=score)
new_queue.append(new_node)
# 3. SELECT/PRUNE: Sort 'new_queue' by score and keep the top N (the breadth_limit)
queue = sorted(new_queue, key=lambda x: x.value, reverse=True)[:breadth_limit]
This loop—Generate, Evaluate, Select—is the fundamental pattern of an automated ToT implementation. It uses targeted LLM calls to explore a problem space much more effectively than a single, monolithic prompt.
This console output from a real ToT implementation shows the iterative nature of the search, exploring and refining paths until a solution is found.

Conclusion
Today, you've bridged the gap between the theory of ToT and its practical implementation. You've seen that at its core, ToT is a pattern of generating multiple potential next steps and then evaluating them to intelligently guide a search toward a solution.
Key Takeaways:
- ToT can be implemented manually: A sequence of prompts for brainstorming, evaluating, and deciding allows a human to guide the search process, which is effective for complex, open-ended problems.
- Automation relies on specialized prompts: For structured problems, you can automate ToT using a "propose" prompt for generation and a "value" prompt for evaluation.
- The LLM acts as a heuristic function: The "value" prompt is a powerful technique that turns the LLM into a programmable heuristic for your search algorithm.
- The core loop is Generate-Evaluate-Select: Whether manual or automated, the fundamental pattern involves creating candidate thoughts, scoring them, and selecting the most promising ones to expand upon.
What's Next?
In this lesson, you learned how ToT uses a deliberate search to find the best reasoning path. In our next lesson, "Design self-consistency prompting strategies," we will explore a different, complementary technique for improving reliability. While ToT is a deep, evaluative search, self-consistency is a broader, simpler approach: it generates multiple reasoning paths independently and takes a "majority vote" on the final answer. Understanding both will give you a powerful and flexible toolkit for enhancing LLM performance.