Tree of Thoughts

Branch the reasoning. Keep the best leaf.

The agent explores multiple reasoning paths simultaneously like a tree diagram and selects promising paths to pursue while discarding dead ends.

Level
01 · Single Agent
Also known as
ToT, Branching Reasoning, Search over Thoughts
Trade-off
Enables a much broader and deeper exploration of the solution space, but leads to exponentially increasing computational and token overhead.
Native in
LangGraph, AutoGen / AG2, Google ADK
The mental model

A shape you can draw on a napkin.

Each thought spawns a few children; promising branches expand, dead ends stop.

prunedprunedexpandexpandprunedprunedprunedprunedbestbestPromptBranch ABranch BBranch CBranch B1Branch B2
Walk it through

A real run, step by step.

TaskUse the numbers 4, 9, 10 and 13 once each, with +, −, × and ÷, to reach 24. The model proposes four first steps, rates each one, and continues only with the two best:
First level of the search. The two kept thoughts are expanded in the next round.
ThoughtNumbers leftRatingDecision
10 − 4 = 66, 9 and 13Promising: 13 − 9 = 4, and 6 × 4 = 24Keep
13 − 9 = 44, 4 and 10Promising: 10 − 4 = 6, and 6 × 4 = 24Keep
4 + 9 = 1310, 13 and 13No way to 24 from hereDrop
10 × 13 = 1304, 9 and 130Far too largeDrop
What this shows: a single chain of reasoning that happens to start with 4 + 9 cannot recover. The tree drops that branch after one rating and spends its calls on the two that can still succeed. The price is visible too: four proposals and four ratings for one step.
In code

The pattern in code.

Plain Pythonpython
def tree_of_thoughts(problem, breadth=2, depth=3):
    frontier = [problem]  # the partial solutions kept so far
    for _ in range(depth):
        candidates = [state + "\n" + thought
                      for state in frontier
                      # model call: possible next steps
                      for thought in propose(state, n=4)]
        # model call: how promising is each?
        ranked = sorted(candidates, key=rate, reverse=True)
        frontier = ranked[:breadth]  # everything else is dropped
    return frontier[0]
Fit

Use it, or not.

When to reach for it

  • The problem has multiple plausible solution paths.
  • Early decisions have high cascading effects.
  • The search space can be meaningfully evaluated by the LLM.

When it backfires

  • The task is linear or directly solvable.
  • Costs and latency must remain low.
Pitfalls

Two ways this pattern will hurt you.

Branching factor blows up the budget

Naively expanding every node multiplies LLM calls exponentially without obvious payoff.

Countermeasure

Cap branching factor and depth; prune by score after each layer.

Pruning heuristic is also an LLM

The pruner is itself non-deterministic; on tight budgets it can discard the best branch.

Countermeasure

Anchor pruning to a verifiable signal (executable check, oracle, regex) when possible.

Framework support

Where Tree of Thoughts is native.

Sources

Where this pattern comes from.

  1. Tree of Thoughts: Deliberate Problem Solving with LLMsIn the source list

Search

Search patterns, frameworks, and pages.