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.
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.
Each thought spawns a few children; promising branches expand, dead ends stop.
| Thought | Numbers left | Rating | Decision |
|---|---|---|---|
| 10 − 4 = 6 | 6, 9 and 13 | Promising: 13 − 9 = 4, and 6 × 4 = 24 | Keep |
| 13 − 9 = 4 | 4, 4 and 10 | Promising: 10 − 4 = 6, and 6 × 4 = 24 | Keep |
| 4 + 9 = 13 | 10, 13 and 13 | No way to 24 from here | Drop |
| 10 × 13 = 130 | 4, 9 and 130 | Far too large | Drop |
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]When to reach for it
When it backfires
Naively expanding every node multiplies LLM calls exponentially without obvious payoff.
Countermeasure
Cap branching factor and depth; prune by score after each layer.
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.
Search patterns, frameworks, and pages.