Agentic AI · L-ToT
Classical search — BFS, DFS, best-first — over a tree whose nodes are LLM-generated thoughts and whose heuristic is an LLM judge.
Tree-of-Thoughts is classical search over a tree whose nodes are thoughts — intermediate reasoning steps written by a language model — and whose heuristic is an evaluation by the same model. Part I states why a single chain of thoughts is insufficient for a combinatorial problem, Part II which prompting strategies precede the tree, Part III which four design decisions define the framework, Part IV which search algorithms operate on the tree, Part V what the search gains and costs, Part VI how a graph generalises the tree, and Part VII where the search is executed:
A left-to-right decoder commits to one continuation and can never revisit it. The argument for the alternative is made before any machinery: problem solving is classically search through a combinatorial space, and the two things such a search needs — branches at a decision point and a heuristic that judges them — are exactly the two an autoregressive chain lacks. Length and breadth are different resources, and only breadth requires a evaluator to compare the alternative paths.
"It is perhaps surprising that underlying all this progress is still the original autoregressive mechanism for generating text, which makes token-level decisions one by one and in a left-to-right fashion. Is such a simple mechanism sufficient for a LM to be built toward a general problem solver? If not, what problems would challenge the current paradigm, and what should be alternative mechanisms?" — Yao et al. (2023), §1
System 1 and System 2. Yao et al. answer their own question from dual-process theory (Kahneman, 2011): people engage a decision in a fast, automatic, unconscious mode — System 1 — or a slow, deliberate, conscious one — System 2. Token-level autoregressive decoding is the System-1 analogue: associative, left to right, one pass, no alternatives held open. The tree is the System-2 augmentation — maintain several candidates, evaluate them, and only then commit. Hence the paper's subtitle: Deliberate Problem Solving.
"A genuine problem-solving process involves the repeated use of available information to initiate exploration, which discloses, in turn, more information until a way to attain the solution is finally discovered." — Newell et al. (1959), quoted in Yao et al. (2023), §3
Research on human problem solving describes it as search through a combinatorial problem space (Newell & Simon, 1972): the nodes are partial solutions, the branches are operators that extend them, and which branch to take is decided by a heuristic that guides the solver towards a solution.
Read against that picture, a left-to-right decoder is missing both halves (Yao et al., 2023, §3):
| The shortcoming | What is absent | |
|---|---|---|
| Locally | it does not explore different continuations within one thought process | the branches of the tree |
| Globally | it has no planning, lookahead or backtracking with which to weigh those options | the heuristic-guided search |
The whole week is the repair of those two — branches, and a heuristic to judge them. The second is the hard half.
A ReAct agent chooses its next action just in time: it looks at the current observation, picks one action, and commits — the choice is made at the moment it is needed, with no alternatives held open. Plan-and-solve moves the choosing forward in time, into an explicit plan. ToT moves it sideways: several candidate next steps exist simultaneously, are scored against one another, and only then is one committed to — and the commitment can be undone.
| when the next step is chosen | alternatives kept | can un-choose | |
|---|---|---|---|
| Just-in-time (ReAct) | at the moment it is needed | none | no — the trace only grows |
| Plan-and-solve | up front, for all steps | none (one plan) | only by replanning |
| Deliberate search (ToT) | at the moment it is needed, over a scored frontier | several — $b$, the breadth limit | yes — backtracking |
Read that way, "just-in-time" and "deliberate" are not opposites: ToT is also just-in-time — it decides step $i$ only when it gets there. What it adds is breadth at the decision point and the ability to return to it.
Search and decomposition sit at different levels, and compose. ToT is a modification of CoT: the same object — one reasoning trace towards one answer — with several continuations sampled and searched instead of a single one taken. Decomposition (→ L-Decomp) works one level up: the task is split into sub-tasks, and each sub-task is handed to an executor that itself reasons — with CoT for straight sequential logic, with ReAct where it must call tools, or with ToT where that one sub-step needs genuine exploration. So the question is never "plan or search?": planning decides what the steps are, search decides how one hard step is solved.
Three prompting strategies precede the tree, and each leaves a defect the next repairs. They are stated in the notation of Yao et al. (2023), because the same symbols carry the framework that follows: one model $p_\theta$ under several prompts, a thought $z_i$, and a state $s = [x, z_{1\cdots i}]$ ($x$ is the full initial prompt context, including system instructions and loaded files). The ladder ends at two limits a chain cannot repair — a decomposition into steps that is left ambiguous, and a majority vote that needs a small output space.
This week is written in the notation of Yao et al. (2023), §2. Four conventions, then the bridge to ours.
Every symbol this week uses, introduced here and used from The Tree-of-Thoughts Framework on:
| Symbol | Name | Meaning |
|---|---|---|
| $x$, $y$ | input, output | the problem statement, and the final answer |
| $z_i$ | thought | one intermediate step — a coherent language sequence |
| $s = [x, z_{1\cdots i}]$ | state | the input plus the thoughts so far — one node of the tree, and the context $c_k$ the model is conditioned on there |
| $S$ | frontier | a set of states, e.g. all candidates at one level |
| $G(p_\theta, s, k)$ | generator | ask $p_\theta$ for $k$ candidate next thoughts from state $s$ |
| $V(p_\theta, S)$ | evaluator | build a scoring function for the states in $S$ |
| $k$ | size limit | number of candidate thoughts generated per state |
| $b$ | breadth limit | number of states kept per level (ToT-BFS) |
| $T$ | step limit | depth of the tree — how many thoughts make a solution |
| $v_{th}$ | threshold | prune a state scoring at or below it (ToT-DFS) |
The bridge to our notation. The course writes a token sequence as random variables $W_{1:T}$ with values $w_{1:T}$; the paper's $x$, $y$, $z$ are exactly such values, and $p_\theta(x)$ above is the same chain rule as $P(W_{1:T}) = \prod_t P(W_t \mid W_{1:t-1})$. A thought $z_i$ is a contiguous sub-sequence of that token stream — which is why the two indices differ. And a state is a context. $s=[x,z_{1\cdots i}]$ is precisely the context $c_k$ of the agent loop — everything the model conditions on at that node — grown by the thought update $c_{k+1}=(c_k,\hat a_k)$, with no observation because a thought returns none (the context $c_k$). ToT is therefore a search over contexts: an edge appends one thought, and what the tree adds to a single chain is keeping several contexts alive at once.
⚠️ Four collisions, kept on purpose. The paper's letters clash with symbols the course already uses. They never occur in the same expression, but read them deliberately:
| Symbol | Here | Elsewhere in the course | |---|---|---| | $V$ | the state evaluator $V(p_\theta, S)$ | the vocabulary $V$, of size $\lvert V \rvert$ | | $z$ | a thought $z_i$, a language sequence | logits $\mathbf{z} \in \mathbb{R}^{\lvert V\rvert}$ (bold) | | $s$ | a ToT state | a parser/FSM state $s_t \in Q$ (constrained decoding) | | $T$ | the step limit | sequence length $W_{1:T}$; sampling temperature |
And one deliberate break with the house rule: $S$ here is a set, not a random variable. Capital letters mean random variables everywhere else in this course; in this week's source they mean collection of sequences. We keep the paper's letters so that the lecture and the paper can be read side by side.
IO prompting is the bare case: wrap the input in instructions, read off the answer.
$$y \sim p_\theta^{\mathrm{IO}}(y \mid x)$$
Chain-of-Thought (Wei et al., 2022) addresses the case where the map from $x$ to $y$ is non-trivial — $x$ a maths question, $y$ a single number. It introduces a chain of thoughts $z_1, \dots, z_n$ to bridge $x$ and $y$, each $z_i$ "a coherent language sequence that serves as a meaningful intermediate step toward problem solving" (Yao et al., 2023, §2) — for maths QA, one intermediate equation. The thoughts are sampled in sequence, then the answer:
$$z_i \sim p_\theta^{\mathrm{CoT}}\big(z_i \mid x, z_{1 \cdots i-1}\big), \qquad y \sim p_\theta^{\mathrm{CoT}}\big(y \mid x, z_{1\cdots n}\big)$$
What CoT leaves open — and it is the opening ToT walks through. In practice nobody samples the $z_i$ one at a time: the whole thing is drawn as one continuous sequence, $[z_{1\cdots n}, y] \sim p_\theta^{\mathrm{CoT}}(z_{1\cdots n}, y \mid x)$, and "the decomposition of thoughts (e.g. is each $z_i$ a phrase, a sentence, or a paragraph) is left ambiguous" (Yao et al., 2023, §2). CoT therefore has the $z_i$ in its formalism but no step boundary in its mechanism — nothing to branch at, nothing to score, nothing to return to.
What the CoT primary actually established (Wei et al., 2022), because the formalism above hides it. The method is a change to the exemplars, not to the instruction: each becomes a triple $\langle x, z, y\rangle$ instead of a pair $\langle x, y\rangle$ — eight hand-written chains, greedy decoding (temperature $T=0$), no fine-tuning. PaLM 540B on GSM8K — grade-school mathematics word problems — goes 17.9 → 56.9. Two findings matter for this week. First, a four-way ablation rules out the cheap explanations: prompting for the equation only ≈ the baseline, putting the chain after the answer ≈ the baseline, and — the interesting one — variable compute only, emitting a run of dots as long as the equation needed, also ≈ the baseline. Extra token positions are not the mechanism; what the intermediate steps carry is. Second, CoT is an emergent ability of scale — no benefit below ~100 B parameters and an active loss below ~10 B, where models produce "fluent but illogical chains of thought." And their own stated limitation is this week's premise: "there is no guarantee of correct reasoning paths."
⚠ Few-shot CoT is this paper; the zero-shot trigger "Let's think step by step" is Kojima et al. (2022) — a different result, and not what is formalised above.
CoT-SC (Wang et al., 2023) is an ensemble over chains: sample $k$ of them i.i.d., return the most frequent answer.
$$\big[z^{(i)}_{1\cdots n},\, y^{(i)}\big] \sim p_\theta^{\mathrm{CoT}}\big(z_{1\cdots n}, y \mid x\big), \quad i = 1 \cdots k \qquad\Longrightarrow\qquad \hat{y} \;=\; \arg\max_{y} \; \#\{\, i \mid y^{(i)} = y \,\}$$
The notation $\#\{\dots\}$ counts the set of indices where the condition holds, and $\arg\max_y$ acts on this count function to select the candidate answer with the highest tally — the voted answer $\hat{y}$.
It improves on CoT because there are generally different thought processes for the same problem — different ways to prove the same theorem — so a decision taken across many of them rests on a richer set of thoughts than any single chain can offer.
To find the true marginal probability of an answer $y$, one would have to sum $p_\theta^{\mathrm{CoT}}(z_{1\cdots n}, y \mid x)$ across all infinitely many possible thought sequences $z_{1\cdots n} \in \mathcal{Z}$:
$$p_\theta^{\mathrm{CoT}}(y \mid x) = \sum_{z_{1\cdots n} \in \mathcal{Z}} p_\theta^{\mathrm{CoT}}(z_{1\cdots n}, y \mid x)$$
Because summing over all $z_{1\cdots n} \in \mathcal{Z}$ is intractable, Monte Carlo integration is used instead. By drawing $k$ finite i.i.d. samples $\big[z^{(i)}_{1\cdots n},\, y^{(i)}\big] \sim p_\theta^{\mathrm{CoT}}\big(z_{1\cdots n}, y \mid x\big)$, the marginal distribution is approximated empirically:
$$p_\theta^{\mathrm{CoT}}(y \mid x) \;\approx\; \frac{1}{k} \#\Big\{\, i \;\mid\; y^{(i)} = y \,\Big\}$$
Selecting the answer $\hat{y}$ that maximizes this empirical probability directly yields the majority vote:
$$\hat{y} \;=\; \arg\max_{y} \; \#\Big\{\, i \;\mid\; y^{(i)} = y \,\Big\}$$
Two limits, and each one names a piece of what comes next (Yao et al., 2023, §2):
| The limit | The consequence |
|---|---|
| No local exploration. Within a chain the individual thought steps are never varied — the breadth sits at the root and nowhere else | branch at every step ⇒ a tree |
| "Most frequent" needs a small output space. The vote is an equality test between answers, so it works for a letter, a label or a number, and not at all for a paragraph | score a partial state instead of counting final answers ⇒ an evaluator |
A tree and an evaluator: that is Tree-of-Thoughts, and the rest of the lecture is what each one costs.
Why the plain vote, and not the model's own probability. One could weight each sampled pair by its likelihood. Measured on PaLM-540B / GSM8K, the unweighted majority scores 74.4, a length-normalised weighted sum 74.1, an unnormalised weighted sum 59.9, and a weighted average 22.1. The reason Wang et al. (2023) give for the tie at the top is the teaching point: the sampled paths' normalised probabilities are nearly equal, because the model "is not well calibrated and thus cannot distinguish well between correct solutions and wrong solutions" (their fn. 2) — which is precisely why Cobbe et al. (2021) had to train a verifier. The count carries information the likelihood does not. That is this week's "the evaluator is the bottleneck" thesis arriving from the other side: here the model's own score is the thing you must not trust, and counting independent draws is what replaces it.
Majority voting guarantees higher accuracy only under two strict conditions:
To see why, we can derive it directly:
Write $y^*$ for the correct answer, as distinct from the voted answer $\hat{y}$ the procedure returns; the vote succeeds exactly when $\hat{y} = y^*$. Let $N$ chains be sampled, each reaching $y^*$ independently with the same probability $p$, and let the random variable $K$ denote how many of them do.
The probability that exactly $k$ chains are correct is given by the Binominal Distribution:
$$P(K = k) = \binom{N}{k} p^k (1-p)^{N-k}$$
A majority vote yields the correct answer if and only if strictly more than half of the $N$ chains are correct ($K > N/2$).
Since $K$ must be an integer, the smallest possible integer greater than $N/2$ is the ceiling $\lceil N/2 \rceil$ (or $\frac{N}{2} + 1$ if $N$ is even, or $\frac{N+1}{2}$ if $N$ is odd).
Because these outcomes ($K = \lceil N/2 \rceil, K = \lceil N/2 \rceil + 1, \dots, K = N$) are mutually exclusive, we add their individual probabilities
$$P(\text{majority correct}) = P(K > N/2) = \sum_{k=\lceil N/2 \rceil}^{N} \binom{N}{k} p^k (1-p)^{N-k}$$
To evaluate the limit as $N \to \infty$, we express $K$ as the sum of $N$ independent indicator variables:$$K = X_1 + X_2 + \dots + X_N$$where each $X_i = 1$ if chain $i$ is correct, and $X_i = 0$ otherwise.
By the Weak Law of Large Numbers, as $N \to \infty$, the sample average $\overline{X}_N$ converges in probability to the true mean $p$:
$$\lim_{N \to \infty} P\left(\left\vert{} \frac{K}{N} - p \right\vert{} < \epsilon\right) = 1 \quad \text{for any } \epsilon > 0$$
Now, rewrite the majority condition $\frac{K}{N} > \frac{1}{2}$ relative to $p$:
$$\frac{K}{N} > \frac{1}{2} \iff \frac{K}{N} - p > \frac{1}{2} - p$$
The majority is correct exactly for case 1, i.e. if $p > 1/2$:
$$\lim_{N \to \infty} P(\text{majority correct}) = \begin{cases} 1 & \text{if } p > \frac{1}{2} \\ 0 & \text{if } p < \frac{1}{2} \end{cases}$$
This is Condorcet's jury theorem (1785), and it earns its place three times:
This creates a direct contrast with the $p^n$ decay seen in multi-step reasoning. In a sequential chain of $n$ steps, overall success requires every step to succeed, causing reliability to collapse exponentially as $p^n$.Parallel voting works in the opposite direction: by aggregating $N$ independent samples using the Binomial distribution, reliability concentrates toward 1 (provided $p > 0.5$). Chain sequentially and errors compound; vote in parallel and agreement concentrates.
Cost. $k\times$ the inference of a single chain, but accuracy saturates — the authors' own advice is 5–10 paths for most of the gain. Compare the bill for full tree search later in this lecture: 5–100× CoT's generated tokens.
Where it fails. Nothing in the method contradicts the majority, so a model that is consistently wrong is reinforced rather than detected — high agreement is compatible with a shared bias (Turpin et al., 2023). And the vote is over answers only: Wang et al.'s own example on StrategyQA — yes/no questions whose reasoning steps are left implicit — reaches the correct conclusion through incorrect population figures. Agreement is evidence about the answer, never about the narration.
Sampling $k$ chains from one model under one prompt can only average out sampling noise: the probability of drawing the correct answer, $p_\theta^{\mathrm{CoT}}(y^* \mid x)$, is fixed by the parameters $\theta$ and the prompt $x$ together. With $\theta$ fixed, the remaining variable is $x$. Sampling under a family of prompts $\{x_1, \dots, x_k\}$ — paraphrases, a different persona, a reordered few-shot set — replaces one conditional distribution by several, $p_\theta^{\mathrm{CoT}}(y \mid x_i)$, so a bias that lives in the phrasing rather than in the parameters no longer affects every draw. An instance on which the original wording gives $p_\theta^{\mathrm{CoT}}(y^* \mid x) < 1/2$ may sit above the threshold under a different wording.
Unequal voters change the statement of the theorem. One prompt per vote leaves the $X_i$ independent but no longer identically distributed, and the limit is then governed by the mean $\bar p = \frac{1}{k} \sum_i p_\theta^{\mathrm{CoT}}(y^* \mid x_i)$: the majority is correct in the limit while $\bar p$ stays above $1/2$. A variant earns its place only if it raises $\bar p$, or fails on different instances than the others do.
That is the caveat on varying the reasoning structure. Demanding a bare answer, an explicit chain, and a program to execute decorrelates far more than paraphrase does, since the three read the problem through different mechanisms — but they are not equally accurate. Prompting for the answer alone lowers $p$ on precisely the multi-step tasks self-consistency is used for (Wei et al., 2022), so mixing it into the pool pulls $\bar p$ down and the vote can end up below the best single variant. Diversity is free only among variants of comparable strength, which is why the structural variant worth having is the one that is also grounded: a program whose result enters the vote is scored by execution rather than by the same parameters. Grounding a model's judgement in an execution result is measured to be worth more than a larger model in the neighbouring task of scoring candidates (Chen et al., 2024).
The boundary is unchanged. Every variant queries the same $\theta$. Where the model lacks the knowledge or the capability outright, $p_\theta^{\mathrm{CoT}}(y^* \mid x_i) < 1/2$ for all $i$, $\bar p$ stays below the threshold, and no rephrasing rescues the vote. A prompt-bound error is within reach of prompt variation; a parameter-bound one requires different parameters.
Varying $x$ leaves the parameters in place, so it cannot repair an error that lives in them. The remaining variable is $\theta$ itself: draw the votes from different models — different checkpoints, different families, different training data — so that the pool is $p_{\theta_1}, \dots, p_{\theta_m}$ rather than one distribution in $m$ costumes. This is the classical ensemble, and it is the only one of the three moves that changes the parameters an error lives in rather than the phrasing around them.
$$y^{(i,j)} \sim p_{\theta_j}^{\mathrm{CoT}}\big(y \mid x_i\big), \qquad \hat{y} \;=\; \arg\max_{y} \; \#\big\{\, (i,j) \;\mid\; y^{(i,j)} = y \,\big\}$$
Three things to weigh before paying for it:
The ordering that follows is the practical one. Resample to remove sampling noise; rephrase to remove a phrasing-bound error; re-model to remove a parameter-bound one — rising in cost, and falling in how often the cheaper move would already have sufficed.
Self-consistency spends its budget in parallel — $k$ independent chains, one vote, no chain ever reading another. The alternative spends it sequentially: produce one answer, criticise it, and produce the next answer conditioned on that criticism. It is the last thing a single chain can do before branching, and it introduces the component the rest of the lecture turns on — a critic whose judgement steers generation. The two cells below state the canonical forms in the notation of this lecture. Both return in Refinement as a Third Thought Generator, where the same loop is read as a way of producing the next node of a tree.
Reflexion (Shinn et al., 2023) for self-awareness across trails splits the loop into three roles and lets them be three different things.
Fig. 2 of (Shinn et al., 2023)
Written with this lecture's prompt superscripts — one model $p_\theta$ under several prompts:
| Role | The call | What it produces |
|---|---|---|
| actor | $p_\theta^{\mathrm{act}}(\,\cdot \mid x, \mathrm{mem})$ | a trajectory — the thoughts, actions and observations of one attempt; a CoT chain or a ReAct run |
| evaluator | $p_\theta^{\mathrm{eval}}(\,\cdot \mid \text{trajectory})$ | a scalar reward $r$ for the finished attempt. It need not be a model: exact-match grading, a hand-written check, or self-generated unit tests |
| self-reflection | $p_\theta^{\mathrm{reflect}}(\,\cdot \mid \text{trajectory}, r)$ | a sentence saying why the attempt failed and what to do differently |
That sentence is appended to a memory $\mathrm{mem}$ holding one to three of them — one to three because the context window bounds it — and the next attempt is conditioned on it. The loop repeats until the evaluator passes or a trial budget is exhausted. The update is text, not a gradient: nothing about $\theta$ changes, and what improves between attempts is the context the actor is given. Shinn et al. write this by folding the memory into the policy's parameters, $\pi_\theta(a \mid s)$ with $\theta = \{M_a, \mathrm{mem}\}$ — their $\theta$, which is not the model weights this lecture writes $\theta$ for.
Reflection is suitable for tasks where the cost per attempt is low and therefore multiple attempts are acceptable, such as code generation against a test suite (clear evaluation signal and stopping signal).
Self-Refine (Madaan et al., 2023) is the same loop with the three roles collapsed onto one model under three prompts, which the lecture's superscript convention writes directly (with $\mathrm{fb}$=feedback):
$$y_0 \sim p_\theta^{\mathrm{gen}}(y \mid x), \qquad \mathrm{fb}_t \sim p_\theta^{\mathrm{fb}}(\mathrm{fb} \mid x, y_t), \qquad y_{t+1} \sim p_\theta^{\mathrm{refine}}\big(y \mid x,\, y_0, \mathrm{fb}_0, \dots, y_t, \mathrm{fb}_t\big)$$
Three things follow from the third expression. The refine step is conditioned on the whole history of drafts and critiques rather than on the last pair alone, so a correction already made is not made twice — and the context grows with every iteration, which is what makes the loop expensive. All three prompts are few-shot: the behaviour is demonstrated by examples, not described by instructions. And the loop stops either at a fixed cap on $t$ or on a stop indicator the feedback itself emits, so the critic also decides when it is finished. Across seven tasks this is worth roughly 20 points over a single generation by the same model, with no training and no external signal.
Its two negative results are the ones this lecture needs.
Read as a thought generator, refinement therefore inherits the problem of the whole lecture: it is a proposal mechanism whose direction is set by a critique, and a critique of a reasoning chain is the case in which a model judge is measurably at its worst.
Tree-of-Thoughts is a framework, not one algorithm. This part first states the classical search problem it instantiates, then takes its four questions in turn — what one thought is, how candidates are generated, how a partial state is scored, and which search algorithm walks the tree — and closes by reading the earlier prompting strategies back as degenerate trees, collapsed in depth or in breadth.
Classical search does not require the graph to exist in memory. A search problem is given by five parts (Russell & Norvig, 2021, §3.1):
| Part | Meaning |
|---|---|
| $s_0$ | the initial state |
| $\text{Actions}(s)$ | what may be done in $s$ |
| $\text{Result}(s,a)$ | the transition model — the state reached by doing $a$ in $s$ |
| $\text{IsGoal}(s)$ | the goal test, a predicate on a state |
| $\text{Cost}(s,a,s')$ | the price of one edge |
Together they define a state space: a directed graph whose vertices are states and whose edges are actions. It is generated on demand — $\text{Actions}$ and $\text{Result}$ are the adjacency list, computed when a node is expanded — so the graph may be far larger than any machine could store, and the search tree built over it (whose nodes are paths) may be infinite where the state space is finite.
The five parts above say what the moves are; they say nothing about which move is worth trying. A heuristic $h(s)$ supplies that: a cheap, fallible estimate of the cost of getting from $s$ to a goal — cheap because it is consulted at every expansion, fallible because computing the true remaining cost $h^*(s)$ would mean solving the problem. It orders the frontier and decides nothing irrevocably.
Together with $g$ it splits one path at the node under consideration:
| $g(s)$ | $h(s)$ | |
|---|---|---|
| looks | backwards, at the path already taken | forwards, at the path still missing |
| is | computed — the sum of the edge costs from $s_0$ to $s$ | estimated — a guess about a part of the graph not yet generated |
| is exactly right | always | only if $h = h^*$, which nobody has |
| comes from | the problem's own $\text{Cost}$ | outside the problem definition, from knowledge about the domain |
Straight-line distance is the standard example: on a road map $g$ is the kilometres already driven and $h$ the distance as the crow flies, which no route can beat and most routes exceed. Over thoughts neither quantity is given — $g$ has to be invented and $h$ is asked of a language model — and what that costs is the subject of Search Algorithms over Thoughts.
While the underlying state space is a graph (where multiple paths can lead to the same state), classical search algorithms navigate this space by building an explicit search tree rooted at the initial state $s_0$. In this tree, every node represents a unique path through the state space graph rather than just a state itself.
The pseudocode below demonstrates this translation in practice: it dynamically expands the implicit graph into a search tree on the fly, generating path nodes on demand without ever needing to construct or store the full graph in memory.
$$ \begin{array}{l} \textbf{Tree-Search}(s_0, \text{Actions}, \text{Result}, \text{StepCost}, \text{IsGoal}, h) \\ \hline \textbf{Input: } \text{initial state } s_0 \text{; the problem functions; a heuristic } h \\ \textbf{Output: } \text{a node holding a goal state, or } \textit{failure} \\ \hline 1: \quad n_0 \gets \text{Node}(\text{State} = s_0,\; \text{Parent} = \text{null},\; \text{Action} = \text{null},\; \text{PathCost} = 0) \\ 2: \quad \textit{frontier} \gets \{\, n_0 \,\} \\ 3: \quad \textbf{while } \textit{frontier} \neq \emptyset \textbf{ do} \\ 4: \quad \qquad n \gets \text{Remove}(\textit{frontier}) \qquad \text{// the strategy — the only line that differs} \\ 5: \quad \qquad \textbf{if } \text{IsGoal}(n.\text{State}) \textbf{ then return } n \\ 6: \quad \qquad \textbf{for each } a \in \text{Actions}(n.\text{State}) \textbf{ do} \\ 7: \quad \qquad \qquad s' \gets \text{Result}(n.\text{State}, a) \\ 8: \quad \qquad \qquad g' \gets n.\text{PathCost} + \text{StepCost}(n.\text{State}, a, s') \\ 9: \quad \qquad \qquad n' \gets \text{Node}(\text{State} = s',\; \text{Parent} = n,\; \text{Action} = a,\; \text{PathCost} = g') \\ 10: \quad \qquad \qquad \text{Insert}(n',\, \textit{frontier}) \\ 11: \quad \textbf{return } \textit{failure} \\ \hline \end{array} $$
Three names in the box are used for the rest of the week. The frontier is the set of nodes that have been generated but not yet expanded — the search's own to-do list, and the object written $S$ once the states are thoughts. $\text{PathCost}$ carries $g(n)$, the cost accumulated from the root, and $h$ estimates the cost still to come. Breadth-first and depth-first search use neither: they read only the shape of the tree, so for them $\text{StepCost}$ and $h$ drop out of the signature. One box therefore covers the whole family, informed algorithms included — what distinguishes them is whether line 4 consults $g$ and $h$.
Line 4 alone decides the algorithm (Russell & Norvig, 2021, §§3.3–3.5):
| $\text{Remove}$ returns | The algorithm |
|---|---|
| the oldest state — a FIFO queue | breadth-first search |
| the newest state — a LIFO stack | depth-first search, and with it backtracking |
| the state minimising $f = g + h$ — a min-heap | best-first search: $h \equiv 0$ is uniform-cost search (Dijkstra), an admissible $h$ is $A^*$ |
Path Reconstruction
The parent reference (or backpointer) links each child node directly to the node that generated it. Because the search algorithm explores forward from the initial state to the goal, storing parent pointers creates a linked chain pointing backward toward the root.
A Markov decision process (MDP) is a search problem in which an action has an uncertain outcome and the task is stated as a reward. Russell & Norvig (2021, §16.1) call it "a stochastic generalization of the search problems of Chapter 3". The MDP keeps the states, $s_0$ and $\text{Actions}(s)$ of the search problem and replaces two of its parts:
| Search problem | MDP |
|---|---|
| $\text{Result}(s,a)$ — the one state reached by $a$ | the transition model $P(s'\mid s,a)$ — the probability of reaching $s'$ by doing $a$ in $s$ |
| $\text{IsGoal}(s)$ and $\text{Cost}(s,a,s')$ | the reward $R(s,a,s')$ — a number received on every transition; a goal becomes a terminal state with a high reward |
Four quantities describe how an agent behaves in an MDP (Sutton & Barto, 2018, §§3.1–3.5):
A partially observable MDP (POMDP) adds a sensor model. Where the agent does not see the state, it receives an observation $o$ with probability $O(o\mid s)$, the sensor model (also called observation or emission model) (Russell & Norvig, 2021, §16.4). An LLM agent is in this case: its context holds the observations, not the state of the environment.
Reward misspecification. The reward is the specification of the task: an agent raises the reward that was written down, not the goal that was meant by it. Agreement between the objective given to a machine and the true preferences of its user is the value alignment problem (Russell & Norvig, 2021, §1.1.5); a reward that can be raised without reaching the goal is misspecified, and a model-based scorer that is optimised against (see Evaluator Quality as the Limiting Factor) is an instance. The MDP formalism itself — the dynamics, discounting and the POMDP that agent benchmarks specify — is the subject of the extended course (→ L-MDP).
ToT instantiates exactly that search problem, over a tree in which each node is a state
$$s \;=\; [\,x,\; z_{1\cdots i}\,]$$
— "a partial solution with the input and the sequence of thoughts so far" (Yao et al., 2023, §3). The root is $s_0 = [x]$, an edge appends one thought, and a leaf is a candidate answer. Two parts of the classical problem are replaced, and both are LLM calls:
| Classical search | Search over thoughts |
|---|---|
| the successor function $\text{Actions} + \text{Result}$ — children read off an adjacency list, $O(1)$ each | the generator $G(p_\theta, s, k)$ — one LLM call, sampled, non-deterministic |
| the heuristic $h$ — a cheap, fallible estimate of how promising a node is, used to order or prune rather than to decide | the evaluator $V(p_\theta, S)$ — the model's own judgement of a half-finished solution |
That is the whole idea; everything difficult about this week lives in the second row.
A specific instantiation of ToT answers exactly four questions (Yao et al., 2023, §3):
| # | Question | The design choice |
|---|---|---|
| 1 | Thought decomposition — what is one step? | a word, an equation, a paragraph-length plan |
| 2 | Thought generator $G(p_\theta, s, k)$ — the successor function | how to propose $k$ children of $s$ |
| 3 | State evaluator $V(p_\theta, S)$ — the heuristic | how to score a partial solution |
| 4 | Search algorithm | BFS, DFS, best-first, … |
CoT samples thoughts coherently with no explicit decomposition; ToT uses the problem's own structure to fix what one thought is — a couple of words (crosswords), one line of equation (Game of 24), a whole paragraph of writing plan (creative writing).
Sizing a thought is a two-sided constraint (Yao et al., 2023, §3). A thought must be small enough that the model can sample promising and diverse candidates — "generating a whole book is usually too 'big' to be coherent" — yet big enough that the model can evaluate its prospects — "generating one token is usually too 'small' to evaluate". Making the decomposition explicit is what turns a chain into a searchable tree.
Thought generator $G(p_\theta, s, k)$. Given a state $s = [x, z_{1\cdots i}]$, produce $k$ candidates for the next thought $z_{i+1}$. Two strategies, chosen by how rich the thought space is:
| Strategy | Sampling | Use when |
|---|---|---|
| i.i.d. samples | $z^{(j)} \sim p_\theta^{\mathrm{CoT}}(z_{i+1}\mid s)$ — $k$ independent calls | the space is rich (a paragraph) — independence is the diversity |
| propose prompt | $[z^{(1)},\dots,z^{(k)}] \sim p_\theta^{\mathrm{propose}}\big(z^{(1\cdots k)}_{i+1}\mid s\big)$ — one call | the space is constrained (a word, an equation) — seeing all candidates in one context avoids duplicates |
The trade is diversity against duplication. Independent draws from a narrow space keep returning the same handful of thoughts — $k$ calls paid for one distinct candidate — while a single propose prompt sees what it has already suggested and is therefore forced to vary. Both are the same model; only the prompt and the number of calls differ. And the candidates come out of the decoder: a model pinned to greedy decoding offers a search nothing to explore, so temperature and top-$p$ are a precondition, not an unrelated setting.
ToT's creative-writing task supplies a third strategy. An iterative-refine pass on top of the ToT output raises the coherency score from 7.56 to 7.91, which the authors read structurally:
"We believe it could be thought of as a third approach to thought generation in the ToT framework, where new thoughts can arise from refining old thoughts instead of i.i.d. or sequentially generated." — Yao et al. (2023), §4.2
So a critique-and-revise loop — Self-Refine (Madaan et al., 2023) is the canonical one — is not a rival to search: it is another way of producing the next node, and it inherits the dependency every node-producing step has — it is only as good as the critique driving it. (The loop itself — execute, critique, refine, iterate — and the producer–critic split are the Reflection pattern.)
State evaluator $V(p_\theta, S)$ — the heuristic, computed by the model itself. It is curried: $V(p_\theta, S)$ takes the model and a frontier and returns a scoring function, which applied to one state yields that state's score.
What is scored is the state's prospect, not its prose. $V$ is asked how likely a solution is to
be reachable from $s$ — the question a classical $h$ asks, put as a success likelihood instead of
a remaining cost (Sharon, 2026). The paper's own mechanism says as much: the value prompt performs
"a few lookahead simulations" (5, 5, 14 reaches 24 via 5+5+14) and commonsense elimination
(1, 2, 3 are too small to reach 24), and both are claims about the goal rather than about the
elegance of the step just taken (Yao et al., 2023, §3). A locally impeccable thought that leaves an
unreachable state must score low. What the model actually returns is the plausibility of the
text in front of it, and the distance between the two is where this week's failures live.
| Strategy | The returned function | Does it use the frontier $S$? |
|---|---|---|
| value | $V(p_\theta,S)(s) \sim p_\theta^{\mathrm{value}}(v\mid s)$ — a scalar $1$–$10$, or a label sure/likely/impossible mapped to a number |
no — each state is judged on its own; $S$ only says which states to score |
| vote | $V(p_\theta,S)(s) = \mathbb{1}[s = s^*]$ where $s^* \sim p_\theta^{\mathrm{vote}}(s^*\mid S)$ | yes — one prompt sees the whole frontier and the states are compared, the only option where success cannot be valued directly (is this passage more coherent?) |
Either strategy can be sampled several times and aggregated, trading tokens for a steadier heuristic.
ToT is not a competitor to the earlier prompting patterns — they are special cases of it, with the tree collapsed in one direction or the other (Yao et al., 2023, §3):
| Method | Depth | Breadth | Where the verifier sits |
|---|---|---|---|
| IO prompting | 1 | 1 | nowhere |
| CoT | $n$ | 1 | nowhere |
| CoT self-consistency | $n$ | $k$ — at the root only, $k$ independent chains | nowhere — a majority vote on the final answer, not a score |
| Self-refine / reflection (Madaan et al., 2023) | $n$ | 1, revisited | the model's own critique, after the fact |
| ToT | $T$ | $b$ — at every level | at every node |
The axis that matters is not "which prompting trick", but where the verifier sits. Self-consistency needs none at all — the vote marginalises the reasoning path away (Wang et al., 2023), which is legal only where the answer set is small and an equality test exists. Best-of-$N$ needs one at the end, where a complete answer is much easier to score than a half-finished thought. Tree search needs one at every node. Cost rises in that order, and so does exposure to a bad evaluator.
The fourth design question, worked out. With $G$ as the successor function and $V$ as the heuristic, the algorithms are the classical ones — the tree is new, the search is not:
Those two are what Yao et al. (2023) implement and measure; best-first / $A^*$ and MCTS they name and explicitly leave to future work (§3.4). Both are constructed here anyway, because what breaks in them is the instructive part: which classical guarantee survives when the heuristic is an LLM judgement.
Beam search is breadth-first search whose frontier is limited to the $b$ best nodes: it keeps "only the k nodes with the best f-scores, discarding any other expanded nodes", which makes it "incomplete and suboptimal" but bounded in memory (Russell & Norvig, 2021, §3.5.5). Applied to a language model, a node is a partial output $y = (w_1, \dots, w_t)$ and its score is the log-probability of the whole sequence,
$$ s(y) = \sum_{i=1}^{t} \log p_\theta(w_i \mid x, w_{<i}), $$
where $\mathcal{V}$ denotes the vocabulary (written $V$ elsewhere in the course; in this lecture $V$ is the state evaluator) and $\langle\text{eos}\rangle$ the end-of-sequence token. A sequence that does not yet end in $\langle\text{eos}\rangle$ is called open.
$$ \begin{array}{l} \textbf{BeamSearch}(x, p_\theta, b, T_{\max}) \\ \hline \textbf{Input: } \text{prompt } x \text{, LM } p_\theta \text{, beam width } b \text{, length limit } T_{\max} \\ \textbf{Output: } \text{the highest-scoring sequence found} \\ \hline 1: \quad B_0 \gets \{\, () \,\} \\ 2: \quad \textbf{for } t = 1, \dots, T_{\max} \textbf{ do} \\ 3: \quad \qquad C_t \gets \{\, y \in B_{t-1} \mid y \text{ ends in } \langle\text{eos}\rangle \,\} \cup \{\, [y, w] \mid y \in B_{t-1} \text{ open},\; w \in \mathcal{V} \,\} \\ 4: \quad \qquad B_t \gets \text{the } b \text{ elements of } C_t \text{ with the largest } s(y) \\ 5: \quad \qquad \textbf{if } \text{no } y \in B_t \text{ is open} \textbf{ then break} \\ 6: \quad \textbf{return } \arg\max_{y \in B_t} s(y) \\ \hline \end{array} $$
Greedy search is the case $b = 1$. Line 4 is where the two differ: greedy search commits to the most probable next token and cannot revise the choice, whereas the beam retains $b - 1$ prefixes whose last token was less probable but whose continuation may be more probable. In Russell & Norvig's example, translating The front door is red into Spanish, greedy search emits La entrada, because entrada is the most probable second word; every continuation of it is improbable, and with $b = 2$ the prefix La puerta is still in the beam when La entrada is discarded (Russell & Norvig, 2021, §25.3.2). The price is $b$ forward passes per step instead of one, and the result is the most probable sequence within the beam, not the most probable sequence overall.
Code generation. A program has a single correctness criterion and can fail on one wrong token, so the objective is the most probable whole program rather than the most probable next token — which is the quantity beam search maximises. The effect is measured: on HumanEval-X, the HumanEval programming problems with their unit tests translated into Java, Python, JavaScript and Go, beam search with $b = 3$ raises Pass@1 — the share of problems whose single generated program passes all tests — above greedy search in all four languages, for Go from 5.5 % to 14.0 % (Fedrushkov et al., 2024); on HumanEval, $b = 3$ raises accuracy from 0.60 to 0.65 for Llama-3.1-8B (Chan et al., 2025). A wider beam is not automatically better: at $b = 8$, Java and Python return to the greedy value or fall below it, an effect the source reports without explaining.
Where tests exist, likelihood is the wrong objective. Beam search never executes the program. With the same budget of 256 generations on the APPS programming benchmark, sampling programs and keeping the one that passes most public tests more than doubles the pass rate of beam search (25.19 % against 11.95 %; Zhang et al., 2023).
Improved methods.
Breadth-first search under a breadth limit $b$: expand the whole frontier, score it, keep the best $b$. It is a beam search whose beams are thoughts and whose score is an LLM judgement rather than a token log-probability.
$$ \begin{array}{l} \textbf{ToT-BFS}(x, p_\theta, G, k, V, T, b) \\ \hline \textbf{Input: } \text{input } x \text{, LM } p_\theta \text{, generator } G \text{ with size limit } k \text{, evaluator } V \text{, step limit } T \text{, breadth limit } b \\ \textbf{Output: } \text{one final answer} \\ \hline 1: \quad S_0 \gets \{x\} \\ 2: \quad \textbf{for } t = 1, \dots, T \textbf{ do} \\ 3: \quad \qquad S_t' \gets \{\, [s, z_t] \mid s \in S_{t-1},\; z_t \in G(p_\theta, s, k) \,\} \\ 4: \quad \qquad V_t \gets V(p_\theta, S_t') \\ 5: \quad \qquad S_t \gets \arg\max_{S \subset S_t',\, |S| = b} \sum_{s \in S} V_t(s) \\ 6: \quad \textbf{return } G(p_\theta, \arg\max_{s \in S_T} V_T(s), 1) \\ \hline \end{array} $$
Line 3 expands every state of the frontier by every candidate thought; line 5 is the beam — the $b$-subset of highest total value; line 6 takes the best surviving state and asks the generator for the answer itself. Cost is explicit in the parameters: $O(b\,k\,T)$ LLM calls, each a full forward pass over a growing prompt.
Depth-first search that explores the most promising child first, prunes on the evaluator's verdict, and backtracks. Called on the root as $\textbf{ToT-DFS}([x],\, 1,\, \dots)$.
$$ \begin{array}{l} \textbf{ToT-DFS}(s, t, p_\theta, G, k, V, T, v_{th}) \\ \hline \textbf{Input: } \text{state } s \text{, step } t \text{, LM } p_\theta \text{, generator } G \text{ with size limit } k \text{, evaluator } V \text{, step limit } T \text{, threshold } v_{th} \\ \textbf{Output: } \text{outputs recorded at depth } T \text{ — the search continues past each one} \\ \hline 1: \quad \textbf{if } t > T \textbf{ then record } G(p_\theta, s, 1) \qquad \text{// a leaf, not a return} \\ 2: \quad \textbf{for each } s' \in G(p_\theta, s, k) \textbf{ do} \qquad \text{// best-valued candidate first} \\ 3: \quad \qquad \textbf{if } V(p_\theta, \{s'\})(s') > v_{th} \textbf{ then} \\ 4: \quad \qquad \qquad \textbf{ToT-DFS}(s', t+1, p_\theta, G, k, V, T, v_{th}) \\ 5: \quad \qquad \textbf{else prune } s' \text{ and continue with the next sibling} \\ \hline \end{array} $$
Line 1 records, it does not return — reaching depth $T$ yields one candidate output and the recursion carries on, so a run collects several. Line 2's ordering is a priority queue: candidates are scored as they are proposed and kept sorted, so the search descends into the most promising child first (Yao et al., 2023, Fig. 6a) — the heuristic controls the order here, where in ToT-BFS it controls the survivors.
ToT-DFS adds the two things a chain cannot do: prune a state the evaluator calls hopeless ($V \le v_{th}$), trading exploration for exploitation, and backtrack to its parent.
Solution Handling & Yield in ToT-DFS
What backtracking actually is. Line 4 returns, and line 2 continues with the next sibling — that is the whole mechanism. There is no "undo" operation: DFS gets backtracking for free from the call stack, which is why it costs no LLM call to go back (the state is already stored; what is sunk is the tokens spent in the abandoned subtree).
And that is exactly what a chain cannot have. Backtracking needs the earlier state to still exist somewhere. A generation only ever grows — a token cannot be un-appended — so a chain that goes wrong at step 1 carries its mistake in the context for every step after it. Storing the tree outside the generation is what buys the return. ToT-BFS holds $b$ states simultaneously and pays in frontier memory; ToT-DFS holds them implicitly on the stack and pays by revisiting a parent later — the classical space/revisit trade-off, with LLM calls as the cost unit.
Input $x =$ 4 9 10 13; reach $24$ using each number exactly once with $+\;-\;\times\;\div$. One
thought is one equation, written together with the numbers it leaves behind, so a state is
readable at a glance. Depth $T = 3$, because three binary operations consume four numbers.
Read it as a ToT-DFS trace; the circled numbers are the order in which the lines of the algorithm execute, and the row a node sits in is its depth $t$.
4+9=13. The parenthesised numbers are the state made legible — they are what the next
propose prompt is conditioned on.4+9=13 (left: 10 13 13) is generated, valued impossible — a short lookahead finds no
route to 24 from three numbers that large — and so fails line 3 and is never recursed into: not
one generator call is spent inside its subtree. The pruning happens at the node, not on the way
out of it.13-10=3 (left: 3 5) two numbers reach only $8$, $2$, $15$ or $3/5$, never $24$; line 4 returns
into $B$'s own loop (④), $B$'s remaining candidates fail in the same way, and only then does the
recursion return to the root (⑤), which continues with 13-9=4.4*6=24 ✓ sits at depth $3$ and is checkable exactly. That is what makes Game of
24 the paper's cleanest task: the final verifier is arithmetic, and only the intermediate
heuristic is a model's opinion.Search algorithms differ in a single decision — which node leaves the frontier next — and the classification follows from what that decision is permitted to look at.
Both expand a min-heap; they differ in what they minimise, and therefore in when they may stop.
| Dijkstra / uniform-cost | $A^*$ | |
|---|---|---|
| priority | $f(s) = g(s)$ — the cost paid so far | $f(s) = g(s) + h(s)$ — paid plus estimated remaining |
| what it computes | shortest paths from $s_0$ to every vertex, in order of distance | a shortest path to one goal |
| termination | the heap empties | the goal is extracted from the heap |
| what it expands | every vertex nearer than the goal | the nodes $h$ makes look worth it |
$A^*$ is Dijkstra plus a bias towards the goal: adding $h$ re-orders the heap so that nodes pointing at the goal come off it earlier, which is why the two coincide at $h \equiv 0$. With an admissible $h$ neither returns a worse path than the other; the saving is entirely in expansions, which over thoughts means the saving is in LLM calls.
The bias is safe only under two conditions on $h$ (Russell & Norvig, 2021, §3.5.2):
Over-estimation is the failure that matters. A single node whose $h$ is too large pushes a genuinely optimal branch behind a worse one, and the search returns the worse one — one node suffices, and the heuristic need not be wrong on average.
The ToT paper explicitly compares itself to this family of classical search:
"Our approach can be treated as a modern rendition of classical search methods … it can be considered as a heuristic search algorithm like $A^$, in which the heuristic at each search node is provided by the LM's self-assessment."* — Yao et al. (2023), §5
Constructing the search over thoughts makes that reading precise. Line 4 of $\textbf{Tree-Search}$
becomes an ExtractMin on a global min-heap ordered by $f(s) = g(s) + h(s)$ — global being the
operative word: the heap holds every unexpanded node of the entire tree, so the search may jump
between branches, where DFS is confined to one stack frame and BFS to one level.
The heuristic is the easy half: it is the evaluator, $h(s) = 1 - V(p_\theta, S)(s)$ as a first approximation; the type conversion that complement glosses over is taken up below. $g$ is the problem. A thought edge has no given length, so $g$ must be chosen, and the natural choices are the resources the search consumes:
| Choice of $g(s)$ | What $f = g + h$ then optimises |
|---|---|
| the depth of $s$ (one per thought) | the shortest chain of reasoning that still looks promising |
| the LLM calls or tokens spent reaching $s$ | promise per call — the cheapest search, not the shortest answer |
| $g \equiv 0$ | nothing but the judge — greedy best-first |
That last row places the two published algorithms. ToT-DFS's "best-valued candidate first" is greedy best-first restricted to one branch, and ToT-BFS is greedy best-first restricted to one level at a time. Both use $h$; neither tracks $g$ — so neither is able to prefer a cheap mediocre state to an expensive promising one, which is the comparison $f = g + h$ exists to make.
The control flow transfers; the optimality proof does not. An LLM judge is neither admissible nor consistent, and it is not even deterministic — re-scoring the same state can return a different number, so $h$ is not a function of the state at all. It has no access to $h^{*}$ either: it scores the plausibility of a piece of text on a scale it invented, unrelated to the number of steps still needed (Hart, Nilsson & Raphael, 1968; Russell & Norvig, 2021, §3.5.2). Best-first search over thoughts is therefore a heuristic that usually helps, never a shortest path.
And before admissibility even comes up, the types do not match. $g$ is minimised; the ToT heuristic is a success likelihood, which is maximised. So $f = g + h$ is not well-formed until $h$ is inverted into a cost — $-\log h_{\text{success}}$ being the principled choice, since it makes the scale additive like $g$. Sharon (2026) makes this the first step of his formalisation.
Monte-Carlo tree search (MCTS) covers the case none of the algorithms above does: no usable heuristic exists, and the only thing that can be said about a state is what the continuations sampled from it achieve. It estimates the value of a state by sampling one such continuation to a finished outcome — a roll-out — averaging that outcome into every state on the path back to the root, and spending the next sample where the average is either the highest or the least certain. No evaluation function is ever written down: the value of a state is collected rather than designed.
Transferred to thoughts, an expansion samples one further thought, and the roll-out either continues to an answer or asks the evaluator once. What this adds over ToT-BFS and ToT-DFS is a revisable judgement: both score a state once and never correct that score, whereas an average over repeated visits lets evidence found deep in a subtree correct a shallow judgement made earlier. What it costs is breadth — the budget is spent revisiting states instead of generating new ones.
Yao et al. (2023, §3.4) name the method as future work, and it has not become standard. Two of its preconditions fail here. The space branches over sampled text rather than over the legal moves of a game, so any affordable budget covers a vanishing fraction of it; and a half-finished thought has no unambiguous outcome to average — a game ends in a win or a loss, a chain of reasoning ends in whatever the model wrote, so the number a roll-out returns is itself a judgement. DeepSeek-AI (2025) report abandoning the method for exactly these two reasons. That is the diagnosis this lecture reaches from the algorithms' side as well: the heuristic is the hard part, not the algorithm.
The evaluator limits deliberate search, not the choice of search rule. ToT's own ablations price the mechanisms separately — pruning and backtracking each carry a measurable share of the gain — and the bill for them is 5–100× the tokens of a single chain. The paper's failure cases and the independent measurements then converge on one component: wherever the search underperforms, the evaluator is what is wrong.
One line is missing from every box in this lecture. $\textbf{Tree-Search}$ stops at a goal test and ToT-DFS records a candidate at $t > T$, but neither ends the run on having found a solution: ToT-BFS walks $T$ levels, ToT-DFS exhausts its unpruned candidates. The stopping rule belongs to the instance, and this part closes by putting it there.
This part draws on seven tasks — the three designed for the Tree-of-Thoughts paper, and four taken from the existing literature:
| Task | What one instance asks for |
|---|---|
| Game of 24 | reach $24$ from four given numbers, each used exactly once, with $+\,-\,\times\,\div$ |
| 5×5 mini crosswords | fill a $5 \times 5$ grid from five across and five down clues — ten words, one placed per step |
| Creative writing | write a four-paragraph passage whose paragraphs end in four given sentences; there is no ground truth, so a coherency score stands in for one |
| GSM8K | a grade-school mathematics word problem, solved in a few arithmetic steps (Cobbe et al., 2021) |
| StrategyQA | a yes/no question whose intermediate reasoning steps are left implicit and whose facts have to be known |
| Spider, Bird | text-to-SQL: translate a question into an SQL query over a given database schema. Bird is the harder of the two, over large real-world databases |
The first three are the paper's own, chosen because GPT-4 fails at them with straight prompting. The remaining four are where the evaluator rather than the search is measured: GSM8K and StrategyQA in Yao et al. (2023), and Spider, Bird and GSM8K in Chen et al. (2024).
Game of 24 — three thought steps, one equation each; a propose prompt as the generator,
ToT-BFS as the search algorithm, and a value prompt returning sure / maybe / impossible,
sampled 3× per state, as the evaluator:
| Method | Success | |
|---|---|---|
| IO prompt | single sample | 7.3 % |
| CoT prompt | single sample | 4.0 % |
| CoT-SC | majority of $k=100$ | 9.0 % |
| IO + refine | $k=10$, with ground-truth feedback | 27 % |
| IO, best of 100 | oracle selection | 33 % |
| CoT, best of 100 | oracle selection | 49 % |
| ToT-BFS | $b=1$ | 45 % |
| ToT-BFS | $b=5$ | 74 % |
~60 % of the CoT samples had already failed after their first step — after roughly three words,
e.g. "4 + 9". Nothing in a left-to-right decoder notices, and nothing can go back.
5×5 crosswords (DFS, up to 10 steps) supplies the ablation that isolates which part pays:
| Variant | Word-level success |
|---|---|
| CoT | 15.6 % |
| ToT-DFS (prune + backtrack) | 60 % |
| ToT-DFS − prune | 41.5 % |
| ToT-DFS − backtrack (greedy $b=1$, overwrites allowed) | 20 % |
What the ablation isolates is the ability to un-commit. Take backtracking away — the same generator, the same evaluator, just no return to a parent — and DFS is back near CoT. Breadth does the same job in the other algorithm: ToT-BFS never backtracks, it simply keeps $b$ states alive at once, which is why $b=5$ beats $b=1$ by 29 points. Holding alternatives is the point; simultaneously (BFS) and on the stack (DFS) are two ways to pay for it. What never works is committing to the first step and hoping.
Yao et al. (2023) are unusually explicit about the bill, and about where their own heuristic breaks.
The rule the authors give. Use deliberate search on tasks that require it and on which CoT struggles. Everywhere else it is an expensive way to get the same answer.
Every algorithm this week — ToT-BFS's beam, ToT-DFS's threshold, $A^*$'s $h$, MCTS's roll-out value — assumes a cheap, trustworthy evaluation of a partial solution. That assumption, and not the choice of search rule, is the weak point:
So: where a deterministic, cheap check exists (tests pass, a proof checks, the game is won), search over thoughts works well and is worth its cost. Where the only scorer is a model judging a half-finished thought, expect much less — and measure it rather than assuming it.
That claim has been measured. Chen et al. (2024) factor a language agent into a generator, a discriminator and a planning method, freeze the generator, and vary only the discriminator — sweeping its accuracy by corrupting an oracle on purpose: compute the true score by executing the candidate, and return it with probability $\tau$, the inverted score otherwise.
Their baseline is re-ranking, the method that does not search: sample $k$ complete candidate solutions, score each finished candidate once, and return the best-scored one. It evaluates no partial state and revises no decision, and it is the bar tree search has to clear.
| $\tau$ | What happens |
|---|---|
| $\le 0.8$ | tree search is worse than plain re-ranking — and iterative correction is indistinguishable from it |
| $0.9$ | tree search jumps sharply and begins to win |
| $1.0$ | the advanced methods finally separate, significantly |
Re-ranking improves linearly in $\tau$ throughout; the sophisticated methods commit to a highly-scored branch, so a wrong score is propagated rather than averaged away — which is why their ranking inverts as the evaluator degrades. And no LLM discriminator tested clears the bar. Pairwise discrimination accuracy — the share of (correct, incorrect) candidate pairs in which the correct candidate is given the higher score, so that 50 % is chance — reads, on the two text-to-SQL benchmarks and the mathematics one: CodeLlama-13B 58.2 / 49.4 / 62.2, GPT-3.5-Turbo 67.0 / 64.3 / 72.1, GPT-4-Turbo 76.5 / 76.2 / 93.8 (Spider / Bird / GSM8K respectively).
The rule this hands you. Before building a search or a refinement loop, measure the scorer you plan to put inside it: below ~90 % pairwise accuracy the loop is expected to be worse than sampling a handful of candidates and keeping the best-scored one, and much more expensive. The fix is not a bigger model but a grounded one — refusing to score a program that does not run and putting its execution result into the scorer's prompt moves discrimination accuracy by +25.4 (Spider), +30.2 (Bird) and +8.4 (GSM8K), enough for a 13B open-weight scorer to beat naive GPT-4. Grounding the judge bought more than ten times the model did.
Every procedure this week spends a constant: ToT-BFS runs $T$ levels keeping $b$ states, ToT-DFS recurses to depth $T$, the crosswords search caps itself at 100 steps, self-consistency draws a fixed $k$ chains. A constant is wrong in both directions at once — it overspends on the instance that was easy and gives up on the one that needed more — and which kind an instance is is not known before the run starts.
The alternative is to let the evaluator decide when the run is over. Liang et al. (2024) measure that in a two-agent debate whose judge is asked after every round whether a solution has been reached (an adaptive break): forcing an answer at a fixed round is worse at every value of that round, most instances finish after one, and the ones consuming more are measurably the harder ones.
So the budget is a variable the evaluator sets per instance, not a hyper-parameter tuned once over a benchmark. ToT owns half the machinery already — $v_{th}$ is a stopping rule for a branch; what the pseudocode has no line for is a stopping rule for the run. Webb et al. (2025) price that omission, justifying a dedicated goal-test module against a ToT baseline that "was able to solve each of the … problems on at least one attempt … but was not able to do so reliably", because it "had no method for reliably determining when a goal had been achieved." Best-of-$k$ that cannot recognise its own success is a sampler, not a search: the answer is in the tree and the procedure walks past it.
A tree node has one parent, so branches can be compared and abandoned but never combined. Graph-of-Thoughts lifts that restriction and models reasoning as an arbitrary directed graph — at the price of the search itself: the graph is written down in advance rather than explored.
Graph-of-Thoughts (Besta et al., 2024) takes the last structural step: drop the tree and model reasoning as an arbitrary directed graph. A thought is a vertex; an edge $(t_1, t_2)$ means $t_2$ was produced with $t_1$ in its prompt — "by explicitly instructing the LLM to use $t_1$ for generating $t_2$" (§3.1). A reasoning step is then a graph rewrite: it adds vertices and edges (and may delete some, to free context). Three of them; only the first needs a shape a tree cannot draw:
| Transformation | Graph signature | In ToT? |
|---|---|---|
| Aggregation — merge $k$ thoughts, or the last thoughts of $k$ whole chains, into one | in-degree $k$ | no |
| Refining — iterate on a thought | a self-loop $(v,v)$ | not as a tree edge — it is the refinement loop (Self-Refine, Reflexion) |
| Generation — branch $k$ successors | out-degree $k$ | yes: $G(p_\theta, s, k)$ |
In-degree is the whole argument. A tree node has one parent, so ToT can compare branches and abandon them — but never combine two. A partial result computed in one branch is unreachable from every other, and backtracking discards the work instead of reusing it. Aggregation keeps it. That is the same DAG vocabulary as a task-decomposition plan graph, now over thoughts rather than tasks.
The graph is not searched — it is written down in advance. The user authors a static Graph of
Operations (GoO) — which transformations apply to which thoughts, in what order — and execution
keeps a Graph Reasoning State, the thoughts so far with their validity and scores, which a
controller walks (Generate, Aggregate, Score, Validate, KeepBest, Repeat). The paper's
worked example is merge sort over digits the model cannot sort in one pass: split the input into
blocks, sort each one ($k$ candidates, scored, KeepBest(1)), then merge the sorted blocks pairwise
by Aggregate. The LLM does the comparisons; merge sort supplies the control flow — KeepBest
prunes inside a fixed plan, it does not decide where to look next. GoT therefore sits outside the
search family this week is about: a plan DAG with an LLM in each node, with no backtracking, since
a bad subresult is merged upward.
Why the graph pays, argued graph-theoretically rather than by benchmark. The volume of a thought $t$ is the number of thoughts with a directed path to $t$ — how much of the reasoning could have influenced it — and its latency is the number of hops to the final thought. At a fixed budget of $\Theta(N)$ thoughts:
| Scheme | Latency | Volume |
|---|---|---|
| CoT | $N$ | $N$ |
| CoT-SC | $N/k$ | $N/k$ |
| ToT | $\log_k N$ | $O(\log_k N)$ |
| GoT | $\log_k N$ | $N$ |
Branching bought ToT depth in parallel, but a leaf's ancestry is only its own root-path, so most of the work done in the tree cannot reach the answer; GoT's second half is a reduction tree, in which every intermediate thought has a path to the final one. Measured on sorting 128 digits: median error ≈62 % below ToT at >31 % lower cost.
Where this leaves the ladder. IO → CoT → CoT-SC → ToT adds breadth and a verifier; GoT adds reuse — and pays for it by giving up search. Choose by the task: a fixed decomposition you can name in advance is a GoO; a space you must explore is a tree.
Where the deliberation lives is an architectural decision rather than a prompting one. Search can sit in the runtime, as an explicit tree the harness expands, or in the weights, as a habit trained into a reasoning model. Read that way the course's three planning paradigms — ReAct, plan-then-execute and deliberate search — are one control structure with three fillings, and what distinguishes them is who decides and what stops the loop.
ToT and MCTS put the search in the runtime: an explicit tree, expanded by an algorithm, backtracking by returning to a stored node. Today's reasoning models put it in the weights: RL against an outcome reward produces one long chain of thought that checks and corrects itself, backtracking in text — no tree, no value model, one generation. Same word, different machinery. Every technique this course teaches as a prompt has made that migration — chain of thought, self-consistency, reflection, tool calling — which relocates the bug: a bad chain in the prompt is a harness problem, a bad chain in the weights is a model-selection problem (→ L-Eval).
Which part of the runtime. Runtime here is the agent architecture of → L-Intro, which deliberate search leaves unchanged. That architecture is the common shape of an agent runtime and not a law — a simple agent discharges several of its duties in one place — but wherever the components are separate, the placement is this: the search algorithm and the tree of states are data structures of Agent Control, and the model is called twice per node in two roles — as the generator $G$ and as the evaluator $V$. The tool gateway still executes every tool and is still the security boundary (→ L-Gateway). Nothing architectural distinguishes a ToT agent from the ReAct loop; what moved is where the deliberation lives — out of the model's context, through a plan object in the agent loop, to a search procedure in the agent loop.
The agent loop is a control structure, not a reasoning method. Written with the deliberation factored out, it commits to nothing — it is the classical agent program of Russell & Norvig (2021, §2.4), a persistent state updated from percepts with one action selected per cycle (see L-Intro):
$$ \begin{array}{l} \textbf{Agent-Loop}(\textit{goal}) \\ \hline \textbf{Input: } \textit{goal} \text{; a runtime providing the tools; a step budget } B \\ \textbf{Output: } \text{the result} \\ \hline 1: \quad \textit{state} \gets \text{Init}(\textit{goal}) \\ 2: \quad \textbf{while } \neg\,\text{Done}(\textit{state}) \textbf{ and } \text{steps} < B \textbf{ do} \\ 3: \quad \qquad a \gets \text{Decide}(\textit{state}) \qquad \text{// the only line a strategy changes} \\ 4: \quad \qquad o \gets \text{Execute}(a) \qquad \text{// the runtime acts; the model never does} \\ 5: \quad \qquad \textit{state} \gets \text{Update}(\textit{state}, a, o) \\ 6: \quad \textbf{return } \text{Result}(\textit{state}) \\ \hline \end{array} $$
Three names carry everything a paradigm decides: what state is, how Decide picks, and
what makes Done true. The picture and the table below fill them in — same loop, three
strategies.
The three paradigms as one picture. The skeleton is the same in all three — decide, execute, update, test for termination; what differs is who decides and what stops the loop.
Only (c) has more than one state alive at the top of its loop; (a) and (b) carry a single committed trace through theirs. The diamond is where they differ again: in (a) the model decides it, in (b) and (c) the runtime does — a counter against $N$ resp. $T$.
The first three rows are the skeleton's three slots — state, Decide, Done:
| (a) ReAct | (b) Plan-and-Solve | (c) Deliberate search | |
|---|---|---|---|
state |
the growing history |
the plan $+$ the step results | the frontier $S$ (or the DFS stack) |
Decide |
one LLM call on the whole history | read step $i$ off the plan, solve it | generate $k$, score, keep $b$ |
Done |
the model emits no tool call | the runtime: plan exhausted | the runtime: $t > T$, or a goal test |
| the loop runs over | turns | the steps of a plan — $N$ fixed at line 1 | the depth of a tree — $T$ fixed in advance |
| candidates per step | 1, committed at once | 1, fixed before the run | $k$ generated, $b$ kept |
| LLM calls | $O(\text{steps})$ | $1 + N + 1$ | $O(b\,k\,T)$ |
| can un-commit a step | no — the trace only grows | only by replanning | yes — backtrack / keep the frontier |
Two rows carry the whole lecture arc. Decide moves out of the model and into the harness, one
week at a time — from a turn of generation (a), to a data structure (b), to a search procedure (c).
And can un-commit is the capability none of the earlier weeks had: (a) cannot un-append a token,
(b) can only throw the plan away and write a new one.
The decision rule is the same one as for decomposition. Autonomy is not a virtue: if the how is already known, fix the workflow; if it must be discovered, ask how expensive a wrong step is. Search is what you buy when a wrong step is expensive and detectable — never the default.
lecture-ToT-exercises.ipynb — by hand: trace ToT-BFS and ToT-DFS (pruning,
backtracking, the threshold), break $A^*$'s optimality with an inadmissible LLM heuristic, choose a
generator and an evaluator per task, and compute the cost per solved task.
lecture-ToT-lab.ipynb: build a ToT explorer from scratch — a propose prompt, a value prompt, and
ToT-BFS over the states — then add a refinement step and a producer–critic loop as the third
thought generator.
state, Decide, Execute,
Update, Done (Russell & Norvig, 2021, §2.4) — and ReAct, plan-then-execute (least-to-most, Zhou
et al., 2023) and deliberate search differ only in the first three. Decide migrates out of the
model and into the harness: a turn of generation, then a data structure, then a search procedure.
Only search can un-commit a step.Peer-reviewed papers
Preprints & other non-peer-reviewed papers
Textbooks & monographs