Skip to content

Language models, retrieval, prompting, and language adaptation · Reasoning, decoding, and discrete diffusion language · LNG073 · Algorithm

Tree of Thoughts

Searches over intermediate text-based problem states, using generation and evaluation to explore alternative solution paths.

Explained here Component demo, not a trained model CodeSplash's own run

In CodeSplash

Documented in the Academy only. There's no Set up button.

Availability
Explained here
Code example
Component demo, not a trained model
Who ran it
CodeSplash's own run
CodeSplash's run
The reference's example ran here in one language.
The reference's record
Python: The chapter's authors report running it; there is no record for this entry alone. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Searches over intermediate text-based problem states, using generation and evaluation to explore alternative solution paths. It generalizes single-chain prompting into an explicit search procedure with branching and pruning.

Question / objective

Can explicit search over intermediate text states improve problem solving within a fixed budget?

Architecture / computational structure

A generator expands nodes, an evaluator scores states, and a search controller prunes/backtracks. Generator/evaluator weights may be frozen; branching, depth, state definitions, and search rules are algorithm controls.

Assumptions

Model-generated values can be miscalibrated or reward superficial plausibility.

Formula / computational definition

Ft+1=TopB⁡s∼Expand⁡(Ft)V(s)(one beam-style search variant).\mathcal F_{t+1}=\operatorname{TopB}_{s\sim\operatorname{Expand}(\mathcal F_t)}V(s)\qquad\text{(one beam-style search variant).}

A state s expands into candidate thoughts s'; a value function V(s') or comparative evaluator scores them, and a search policy retains promising nodes. Breadth-first, depth-first, and task-specific choices change the algorithm's behavior.

Parameters and notation

Learned parameters: identified in the architecture subsection. Hyperparameters, design and inference choices: Thought granularity, branching factor, search depth, beam/retention count, evaluator prompt/model, backtracking rules, and total budget.

Fitting, fine-tuning, and adaptation

The basic method needs no model-weight updates; tune search settings on held-out tasks. If training a value model, use independently labeled states and validate its ranking/calibration. Measure total computation and avoid evaluating only successful branches; fine-tuned evaluators can overfit problem templates.

How to use and calibrate

Measure solved-problem rate, evaluator accuracy, nodes expanded, model calls, total tokens, and comparisons with equal-budget sampling. Use development data for settings and an independent test set for the final estimate. Report uncertainty across examples or seeds when the evaluation design supports it; model scores and self-assessments are not automatically calibrated probabilities of correctness.

Example application

Explore alternate steps in a constrained puzzle where intermediate states can be checked.

Interpretation / pitfalls

Watch out

Search amplifies cost and can prune the correct path early; success depends strongly on defining valid states and transitions. The code below isolates one stated component; successful execution does not validate model quality or reproduce the cited experiment.

Python and R code examples

Full implementation route: Implement the published search structure around a documented generation API; R can orchestrate native search with reticulate calls for generation. The example performs one scored frontier-pruning step, not a complete task-solving system.

Example scope: One Tree-of-Thoughts frontier-pruning step from supplied evaluator scores; no generator.

Dependencies: Python 3 standard library; base R; shared setup above. No model downloads or external services.

Validation: Python component executed in the combined example script; R counterpart supplied but not runtime-tested here. All numbers are synthetic.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — One Tree-of-Thoughts frontier-pruning step from supplied evaluator scores; no generator.

Shared setup for this chapter
import math

def softmax(xs):
    m = max(xs)
    es = [math.exp(x-m) for x in xs]
    return [e/sum(es) for e in es]

def dot(a, b):
    return sum(x*y for x, y in zip(a, b))

def matvec(matrix, vector):
    return [dot(row, vector) for row in matrix]

def meanvec(rows):
    return [sum(row[j] for row in rows)/len(rows) for j in range(len(rows[0]))]

def sigmoid(x):
    return 1/(1+math.exp(-x))

def nll(probabilities):
    return -sum(math.log(p) for p in probabilities)
frontier = [("state_A",0.6),("state_B",0.9),("state_C",0.4)]
beam_width = 2
print(sorted(frontier,key=lambda item:item[1],reverse=True)[:beam_width])
R

Component demo, not a trained model — One Tree-of-Thoughts frontier-pruning step from supplied evaluator scores; no generator.

Shared setup for this chapter
softmax <- function(x) {
  shifted <- exp(x-max(x))
  shifted/sum(shifted)
}
frontier <- data.frame(state=c("state_A","state_B","state_C"),score=c(0.6,0.9,0.4))
beam_width <- 2L
print(frontier[order(frontier$score,decreasing=TRUE)[1:beam_width],])

Sources

From the CodeSplash reference, entry LNG073.