Tree of thoughts

Tree of Thoughts (ToT) extends Chain of Thought by exploring multiple reasoning paths simultaneously. Instead of a single chain, the LLM generates several thought branches, evaluates each, prunes weak ones, and explores the most promising paths, much like a chess engine evaluating moves.

Meta-controller architecture

A meta-controller that orchestrates multiple specialist agents for complex tasks

Tree of thoughts exploration

Yes, ToT is token-intensive because it generates and scores multiple branches. That is why pruning is essential: you cut low-scoring branches early so you only expand the most promising paths. Reserve ToT for problems where finding the best solution justifies the extra cost.

patterns/38_tree_of_thoughts.py
python
class ThoughtNode:
    def __init__(self, content, parent=None):
        self.content = content
        self.parent = parent
        self.children = []
        self.evaluation_score = 0.0

class TreeOfThoughts:
    def build_tree(self, problem, max_depth=3):
        self.root = ThoughtNode(problem)
        queue = [self.root]
        for level in range(max_depth):
            next_queue = []
            for node in queue:
                thoughts = self.generate_thoughts(problem, node.get_path())
                for thought in thoughts:
                    child = ThoughtNode(thought, parent=node)
                    score = self.evaluate_thought(problem, child.get_path())
                    child.evaluation_score = score
                    node.children.append(child)
                    if score >= 6.0:  # Prune low-scoring branches
                        next_queue.append(child)
            queue = next_queue
        return self.find_best_solution()

    def find_best_solution(self):
        """Find the highest-scoring leaf node."""
        best = max(self._get_leaves(), key=lambda n: n.evaluation_score)
        return best.get_path()

BFS-based tree construction with scoring and pruning.

It depends on the branching factor. If you explore 3 possibilities at each of 3 steps, that is 9 calls just for generation, plus evaluation calls. For complex problems the accuracy gain is worth it, but for simple tasks it is overkill. Always consider the cost-quality tradeoff.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Matching exercise: Match tree of thoughts concepts

Loading practice…

Flashcards: Flashcards

Loading practice…

Tree of Thoughts turns reasoning into a search problem by exploring multiple paths and pruning dead ends. It is one of the most powerful techniques for complex problem-solving. Next, we will look at Mental Loop and Dry Run, patterns that let agents simulate actions before committing to them.