# Tree of Thoughts: breadth-first search with a value function and a beam on Game of 24

Tree of Thoughts turns one chain of thought into a search. Twenty hard Game of 24 puzzles show how proposing several equations per step, rating them and keeping the best five lifts success from 5.7% for one chain to 74.1%, and what the 242 model calls per puzzle buy.

<div data-sim="specx" data-spec="{&#34;head&#34;:&#34;Tree of Thoughts: breadth-first search over thoughts with a value function and a beam&#34;,&#34;sub&#34;:&#34;Twenty Game of 24 puzzles from the hard end of 4nums.com. At each of three steps the model proposes candidate equations, a value prompt rates them, and the search keeps the best b. Change the beam, the proposals and the value function&#39;s reliability.&#34;,&#34;params&#34;:[{&#34;id&#34;:&#34;b&#34;,&#34;label&#34;:&#34;Beam width (b)&#34;,&#34;min&#34;:1,&#34;max&#34;:10,&#34;step&#34;:1,&#34;value&#34;:5,&#34;note&#34;:&#34;States kept after each step; the paper used b = 5&#34;},{&#34;id&#34;:&#34;c&#34;,&#34;label&#34;:&#34;Thoughts proposed per state&#34;,&#34;min&#34;:1,&#34;max&#34;:15,&#34;step&#34;:1,&#34;value&#34;:7,&#34;note&#34;:&#34;Candidate next equations one propose prompt lists&#34;},{&#34;id&#34;:&#34;a&#34;,&#34;label&#34;:&#34;Value function hit rate&#34;,&#34;min&#34;:0.3,&#34;max&#34;:1,&#34;step&#34;:0.01,&#34;value&#34;:0.85,&#34;note&#34;:&#34;Chance one kept slot holds a state that can still reach 24, when one exists&#34;},{&#34;id&#34;:&#34;depth&#34;,&#34;label&#34;:&#34;Thought steps (depth)&#34;,&#34;min&#34;:1,&#34;max&#34;:6,&#34;step&#34;:1,&#34;value&#34;:3,&#34;note&#34;:&#34;Game of 24 needs 3 equations&#34;},{&#34;id&#34;:&#34;nv&#34;,&#34;label&#34;:&#34;Value samples per thought&#34;,&#34;min&#34;:1,&#34;max&#34;:5,&#34;step&#34;:1,&#34;value&#34;:3,&#34;note&#34;:&#34;The paper sampled the value prompt 3 times&#34;}],&#34;data&#34;:{&#34;columns&#34;:[&#34;idx&#34;,&#34;s&#34;],&#34;rows&#34;:[[901,0.62],[906,0.55],[911,0.5],[916,0.47],[921,0.44],[926,0.42],[931,0.4],[936,0.38],[941,0.36],[946,0.34],[951,0.32],[956,0.3],[961,0.28],[966,0.26],[971,0.24],[976,0.22],[981,0.2],[986,0.17],[991,0.14],[996,0.1]],&#34;note&#34;:&#34;Illustrative: 20 games indexed 901 to 996 on 4nums.com (the paper tested 901 to 1000). s = chance one proposed equation keeps a solvable state, set so a single chain solves 5.7%, near the paper&#39;s 4.0% CoT and 7.3% IO with GPT-4.&#34;},&#34;pre&#34;:[{&#34;id&#34;:&#34;keep&#34;,&#34;label&#34;:&#34;Chance the beam keeps a good state&#34;,&#34;expr&#34;:&#34;1 - (1 - a)^b&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;K_b = 1-(1-a)^b&#34;},{&#34;id&#34;:&#34;keep1&#34;,&#34;label&#34;:&#34;Same with b = 1&#34;,&#34;expr&#34;:&#34;a&#34;,&#34;hide&#34;:true}],&#34;row&#34;:[{&#34;id&#34;:&#34;e&#34;,&#34;label&#34;:&#34;a good thought is proposed&#34;,&#34;expr&#34;:&#34;1 - (1 - s)^c&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;e_i = 1-(1-s_i)^c&#34;},{&#34;id&#34;:&#34;surv&#34;,&#34;label&#34;:&#34;good state survives one step&#34;,&#34;expr&#34;:&#34;e*keep&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;g_i = e_i\\,K_b&#34;},{&#34;id&#34;:&#34;tot&#34;,&#34;label&#34;:&#34;solved by ToT&#34;,&#34;expr&#34;:&#34;surv^depth&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;P^{ToT}_i = g_i^{\\,D}&#34;},{&#34;id&#34;:&#34;tot1&#34;,&#34;label&#34;:&#34;solved by ToT with b = 1&#34;,&#34;expr&#34;:&#34;(e*keep1)^depth&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;P^{b=1}_i = (e_i\\,a)^{D}&#34;},{&#34;id&#34;:&#34;cot&#34;,&#34;label&#34;:&#34;solved by one chain&#34;,&#34;expr&#34;:&#34;s^depth&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;P^{CoT}_i = s_i^{\\,D}&#34;},{&#34;id&#34;:&#34;alive&#34;,&#34;label&#34;:&#34;good state alive after d steps&#34;,&#34;expr&#34;:&#34;surv^min(d, depth)&#34;,&#34;hide&#34;:true}],&#34;derived&#34;:[{&#34;id&#34;:&#34;acc_cot&#34;,&#34;label&#34;:&#34;Success, one chain of thought&#34;,&#34;expr&#34;:&#34;mean(cot)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;acc_b1&#34;,&#34;label&#34;:&#34;Success, ToT with b = 1&#34;,&#34;expr&#34;:&#34;mean(tot1)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;acc&#34;,&#34;label&#34;:&#34;Success, ToT with beam b&#34;,&#34;expr&#34;:&#34;mean(tot)&#34;,&#34;fmt&#34;:&#34;p&#34;,&#34;tex&#34;:&#34;S = \\tfrac{1}{n}\\sum_i g_i^{\\,D}&#34;},{&#34;id&#34;:&#34;step_loss&#34;,&#34;label&#34;:&#34;Mean chance of losing the good path per step&#34;,&#34;expr&#34;:&#34;1 - mean(surv)&#34;,&#34;fmt&#34;:&#34;p&#34;},{&#34;id&#34;:&#34;calls&#34;,&#34;label&#34;:&#34;LM calls per puzzle&#34;,&#34;expr&#34;:&#34;(1 + c*nv) + (depth - 1)*(b + b*c*nv)&#34;,&#34;fmt&#34;:&#34;i&#34;,&#34;tex&#34;:&#34;L = (1 + c\\,v) + (D-1)\\,(b + b\\,c\\,v)&#34;},{&#34;id&#34;:&#34;calls_solved&#34;,&#34;label&#34;:&#34;LM calls per solved puzzle&#34;,&#34;expr&#34;:&#34;calls/acc&#34;,&#34;fmt&#34;:&#34;i&#34;},{&#34;id&#34;:&#34;alive_d&#34;,&#34;label&#34;:&#34;Puzzles with a good state in the beam after d steps&#34;,&#34;expr&#34;:&#34;mean(alive)&#34;,&#34;fmt&#34;:&#34;p&#34;}],&#34;iterate&#34;:{&#34;state&#34;:[{&#34;id&#34;:&#34;d&#34;,&#34;label&#34;:&#34;Search steps done&#34;,&#34;init&#34;:&#34;0&#34;}],&#34;update&#34;:[{&#34;id&#34;:&#34;d&#34;,&#34;expr&#34;:&#34;d + 1&#34;}],&#34;max&#34;:6,&#34;per&#34;:1,&#34;track&#34;:&#34;alive_d&#34;},&#34;chart&#34;:{&#34;title&#34;:&#34;Success against how often a proposed thought is good&#34;,&#34;x&#34;:{&#34;var&#34;:&#34;t&#34;,&#34;label&#34;:&#34;Chance one proposed thought keeps the puzzle solvable (s)&#34;,&#34;from&#34;:0,&#34;to&#34;:0.7},&#34;y&#34;:{&#34;from&#34;:0,&#34;to&#34;:1},&#34;series&#34;:[{&#34;label&#34;:&#34;ToT, beam b&#34;,&#34;expr&#34;:&#34;((1 - (1 - t)^c)*keep)^depth&#34;},{&#34;label&#34;:&#34;ToT, b = 1&#34;,&#34;expr&#34;:&#34;((1 - (1 - t)^c)*a)^depth&#34;,&#34;dash&#34;:true},{&#34;label&#34;:&#34;One chain of thought&#34;,&#34;expr&#34;:&#34;t^depth&#34;,&#34;dash&#34;:true}],&#34;fit&#34;:{&#34;x&#34;:&#34;s&#34;,&#34;y&#34;:&#34;tot&#34;,&#34;label&#34;:&#34;Puzzles: chance ToT solves them&#34;}},&#34;level&#34;:&#34;advanced&#34;,&#34;bars&#34;:{&#34;title&#34;:&#34;Chance ToT solves each puzzle (4nums.com index)&#34;,&#34;y&#34;:&#34;tot&#34;,&#34;label&#34;:&#34;idx&#34;},&#34;steps&#34;:[{&#34;title&#34;:&#34;Decompose into thoughts&#34;,&#34;text&#34;:&#34;A Game of 24 solution is {depth} intermediate equations, each one thought. A single chain of thought must get every step right and solves {acc_cot:p} of these puzzles.&#34;,&#34;show&#34;:[&#34;acc_cot&#34;]},{&#34;title&#34;:&#34;Propose&#34;,&#34;text&#34;:&#34;From every state in the beam the model lists {c} candidate next equations. The chance that at least one of them keeps the puzzle solvable averages over the rows to the column e.&#34;},{&#34;title&#34;:&#34;Evaluate&#34;,&#34;text&#34;:&#34;A value prompt rates each candidate sure, maybe or impossible, sampled {nv} times. Each kept slot holds a good state with probability {a}, so a beam of {b} keeps one with probability {keep:p}.&#34;,&#34;show&#34;:[&#34;keep&#34;]},{&#34;title&#34;:&#34;Keep the best b&#34;,&#34;text&#34;:&#34;Breadth-first search keeps the {b} highest-valued states and expands them at the next step. On average the good path is lost with probability {step_loss:p} per step.&#34;,&#34;show&#34;:[&#34;step_loss&#34;]},{&#34;title&#34;:&#34;Reach 24&#34;,&#34;text&#34;:&#34;Surviving all {depth} steps solves {acc:p} of the puzzles, against {acc_b1:p} with a beam of one.&#34;,&#34;show&#34;:[&#34;acc&#34;,&#34;acc_b1&#34;]},{&#34;title&#34;:&#34;Pay for the search&#34;,&#34;text&#34;:&#34;The search makes {calls} LM calls per puzzle, about {calls_solved} per solved puzzle.&#34;,&#34;show&#34;:[&#34;calls&#34;,&#34;calls_solved&#34;]}]}"><p>Interactive simulation. It needs JavaScript; the text below explains what it shows.</p></div>

## What you are looking at

Each row is one Game of 24 puzzle: four numbers that must be combined with plus, minus, times and divide into 24. The rows sit at indices 901 to 996 of the 4nums.com list, which is sorted by how long people take to solve each game, so these are hard ones; the Tree of Thoughts paper tested on games 901 to 1000. The column s is the chance that one equation the model proposes still leaves a set of numbers that can reach 24. It falls from 0.62 for the easiest row to 0.10 for the hardest.

A solution has three thoughts, one equation per step. A single chain of thought must pick a good equation three times in a row, so it solves s³ of the time: 5.7% on average here. The paper measured 4.0% for chain-of-thought prompting and 7.3% for plain input-output prompting with GPT-4. Tree of Thoughts with the default settings solves 74.1%, and with a beam of one 45.5%; the paper reports 74% and 45%.

The chart shows the three methods as curves against s, with the twenty puzzles as dots on the Tree of Thoughts curve. The bars show each puzzle's chance of being solved. The iteration plays the search one step at a time and tracks the share of puzzles that still have a good state in the beam: 100% before the search, 88.9% after step one, 80.5% after step two and 74.1% at the end.

## The algorithm

Tree of Thoughts (Yao et al., 2023) treats reasoning as search over a tree whose nodes are partial solutions. It needs four choices: how to split the problem into thoughts, how to generate candidate thoughts, how to evaluate states, and which search algorithm to run. For Game of 24 the paper uses:

1. **Thoughts**: three steps, each an intermediate equation that replaces two numbers with their result.
2. **Generate**: one propose prompt lists possible next equations from the current numbers.
3. **Evaluate**: a value prompt rates each new state as sure, maybe or impossible to reach 24, sampled 3 times per state.
4. **Search**: breadth-first search that keeps the best b = 5 states at each step.

The simulation models one good lineage through the tree. From a good state the model proposes c thoughts, each good with probability s, so at least one good thought appears with probability

$$e_i = 1-(1-s_i)^c$$

The value function then has to put a good state into the beam. If each of the b kept slots holds a good state with probability a, the beam keeps one with probability K = 1 − (1 − a)^b, and the good lineage survives one step with probability g = e × K. Surviving all D steps gives

$$P_i = (e_i\,K_b)^{D}$$

The cost is the number of model calls: one propose call for the root and c × v value calls for its children, then b propose calls and b × c × v value calls at each later step, L = (1 + cv) + (D − 1)(b + bcv).

Worked example, puzzle 946 with s = 0.34 and the defaults c = 7, a = 0.85, b = 5, D = 3. A good thought is proposed with probability 1 − 0.66⁷ = 0.945. The beam keeps a good state with probability 1 − 0.15⁵ = 0.9999, so one step is survived with probability 0.945 and all three with 0.945³ = 84.5%. With b = 1 the beam is only as good as the value function's top pick: (0.945 × 0.85)³ = 51.9%. One chain of thought solves it 0.34³ = 3.9% of the time. The search costs 1 + 21 = 22 calls at the first step and 5 + 105 = 110 at each of the next two, 242 in all.

## Things to try

- Drop the beam to 1. Success falls to 45.5% because a single wrong pick by the value function ends the search. With b = 2 it is already back to 69.2%, and b = 3 gives 73.4%; beyond that the beam is no longer the bottleneck.
- Raise the beam to 10. Success barely moves, to 74.1%, but calls nearly double from 242 to 462.
- Change the proposals. With 3 thoughts per state success is 36.5%; with 10 it is 85.5% at 341 calls. When the generator rarely proposes a good equation, no amount of evaluation can save the search.
- Lower the value function's hit rate to 0.6. With b = 5 success only drops to 71.8%, but with b = 1 it collapses to 16.0%. A wider beam is insurance against a noisy evaluator.
- Look at the bars: the hardest puzzles, with s of 0.14 and 0.10, are solved 27.7% and 14.2% of the time even with search.

## How it works in real systems

The same propose, evaluate and keep pattern sits under many agent planners: generate several candidate plans or tool calls, score them with a model or a heuristic, and expand only the best. The paper also ran depth-first search with pruning for mini crosswords. The reference code at princeton-nlp/tree-of-thought-llm implements both, with the propose and value prompts as plain text templates.

Two things decide whether search pays off. The generator has to produce good candidates often enough, and the evaluator has to rank them well; the beam only covers for evaluator noise. Cost grows with b × c × v per step, so production systems cache value calls for repeated states, use a cheaper model as the evaluator, or stop when a state is rated sure. When actions touch an environment and their outcomes give feedback, Monte Carlo tree search as in [LATS](/articles/lats-language-agent-tree-search-mcts) replaces fixed breadth-first expansion. Compare token-level [beam search](/ai-simulations/beam-search), which applies the same keep-the-best-b rule to tokens instead of thoughts, and the single-path [ReAct loop](/ai-simulations/react-tool-calling). The [Tree of Thoughts article](/articles/tree-of-thoughts-search-explained-code) walks through the code.

## Sources

- [Yao et al., Tree of Thoughts: Deliberate Problem Solving with Large Language Models (arXiv 2305.10601)](https://arxiv.org/abs/2305.10601)
- [Tree of Thoughts paper, full text with Game of 24 setup, results and costs](https://arxiv.org/pdf/2305.10601)
- [Zhou et al., Language Agent Tree Search (arXiv 2310.04406)](https://arxiv.org/pdf/2310.04406)
- [Wang et al., Self-Consistency Improves Chain of Thought Reasoning (arXiv 2203.11171)](https://arxiv.org/pdf/2203.11171)
