DEV Community

Cover image for Beginner's Guide To Tree Of Thoughts Prompting (With Examples)
Aldo Pagani Jr. for Zero To Mastery

Posted on

Beginner's Guide To Tree Of Thoughts Prompting (With Examples)

Have you ever felt like AI models rush to give answers without reasoning things through? Tree of Thoughts (ToT) prompting is designed to solve that. Unlike simple prompting techniques, ToT helps AI slow down, think step-by-step, explore multiple solutions, and choose the best one, much like how humans approach complex problems.

πŸ‘‰ This post is a portion of Scott's full blog post on Zero To Mastery. To read the full article, click here.


🌳 What is Tree of Thoughts Prompting?

Tree of Thoughts (ToT) prompting is a framework that replicates human problem-solving by having AI generate, evaluate, and refine multiple lines of reasoning. Instead of following a single path (like Chain of Thought), ToT explores various ideas at each stage, building a tree-like structure of possibilities. This method came from research by Princeton, Google DeepMind, and Long in 2023.


🌱 How Does Tree of Thoughts Work?

Here’s how ToT operates:

  • Generate Thoughts: The AI creates several possible solutions at each stage.
  • Evaluate Thoughts: It assesses which ideas to keep exploring and which to discard.
  • Expand Promising Thoughts: The best ideas branch into further possibilities.
  • Search for the Best Solution: Algorithms like Breadth-First Search (BFS) or Depth-First Search (DFS) guide exploration.

πŸ‘‰ Example: Imagine planning a vacation. You start with general ideas (beach, city, skiing). Based on your preferences (you hate cold), you eliminate skiing. Then, you narrow choices to cities and finally pick Paris. This mirrors how ToT prunes ideas to find the best one.


πŸ’‘ Why Use Tree of Thoughts?

ToT shines because it:

  • Encourages iterative exploration, increasing the chance of finding the right solution.
  • Helps discard weak ideas early, saving time and resources.
  • Adapts across domains β€” from math problems to creative writing.

Game of 24 Example:

In this problem-solving game, you must combine 4 numbers using arithmetic to reach 24.

πŸ‘‰ Example solution:

Numbers: 8, 3, 3, 2

Solution: (8 Γ· 2) Γ— (3 + 3) = 24

When tested on this:

  • Input-Output prompting: 7.3% success
  • Chain of Thought: 4% success
  • Self-consistency CoT: 9% success
  • Tree of Thoughts (B=1): 45% success
  • Tree of Thoughts (B=5): 74% success

The more paths ToT explores (higher B), the better it performs.


πŸš€ How To Implement Tree of Thoughts

Option 1: Via Code

Ideal if you want full control. You can:

  • Generate multiple paths programmatically
  • Score and filter ideas automatically
  • Apply BFS/DFS algorithms for exploration

Option 2: Prompt Chaining

Guide the AI with a sequence of prompts:

  • Generate multiple ideas
  • Evaluate them through follow-up prompts
  • Expand on the most promising

πŸ‘‰ Example:

"List 3 strategies for warehouse logistics" β†’ "Evaluate these strategies" β†’ "Expand on the best one"

Option 3: Zero Shot ToT

Use a single structured prompt:

"Imagine 3 experts answering this question. Each writes one step, shares it, and leaves if they realize they're wrong. Repeat until a solution is reached."

This simulates multiple reasoning paths in one go β€” perfect for quick experimentation.


Final Thoughts

Tree of Thoughts helps AI reason smarter by exploring and pruning multiple paths. It’s a flexible method you can try with or without code to tackle complex challenges.

Ready to level up your prompting skills? Check out the full ZTM Prompt Engineering course for a deeper dive!


Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.