Depth-first (default)
All steps
Parts of this pattern
- Problem Input / output
- The puzzle to solve: make 24 from 4, 9, 10 and 13 using + - * /.
- Thought 1 LLM call
- First candidate move, 4+9=13, proposed alongside two alternatives.
- Thought 2 LLM call
- Second candidate move, 10-4=6, which the evaluator rates promising and the search expands.
- Thought 3 LLM call
- Third candidate move, 13*4=52, which looks too large to reach 24 and gets pruned.
- Thought 2a LLM call
- A follow-up from Thought 2, 6+9=15; the search tries it first, but every continuation fails.
- Thought 2b LLM call
- A follow-up from Thought 2, 13-9=4, leaving 6 and 4; the search backtracks here and finds 6*4=24.
- Evaluator Evaluator
- Rates each candidate sure, maybe or impossible so the search can expand good branches and prune dead ends.
- Solution Input / output
- The finished path of moves that reaches 24, returned once a branch solves the puzzle.
- Problem → Thought 1
- The proposal prompt yields a first candidate move from the starting numbers.
Propose 3 different next steps. A: 4+9=13 (left: 13, 10, 13)
- Problem → Thought 2
- The same prompt yields a second, different candidate, so the model does not commit to one line.
B: 10-4=6 (left: 6, 9, 13)
- Problem → Thought 3
- A third distinct candidate rounds out the breadth of three.
C: 13*4=52 (left: 52, 9, 10)
- Thought 2 → Thought 2aexpand
- The promising Thought 2 is expanded with a new proposal prompt; this is one follow-up.
Progress: 10-4=6 (left: 6, 9, 13). Next step A: 6+9=15 (left: 15, 13)
- Thought 2 → Thought 2bexpand
- A second follow-up from Thought 2, a different next move.
Next step B: 13-9=4 (left: 6, 4)
- Thought 1 → Evaluatorrate?
- Thought 1 is sent to the evaluator to ask whether it can still reach 24.
Candidate progress: 4+9=13 (left: 13, 10, 13). Answer sure, maybe or impossible.
- Thought 2 → Evaluatorrate?
- Thought 2 is sent to the evaluator for a rating.
Candidate progress: 10-4=6 (left: 6, 9, 13). Answer sure, maybe or impossible.
- Thought 3 → Evaluatorrate?
- Thought 3 is sent to the evaluator for a rating.
Candidate progress: 13*4=52 (left: 52, 9, 10). Answer sure, maybe or impossible.
- Thought 2a → Evaluatorrate?
- The follow-up Thought 2a is rated before the search explores it.
Candidate progress: 10-4=6, 6+9=15 (left: 15, 13). Answer sure, maybe or impossible.
- Thought 2b → Evaluatorrate?
- The follow-up Thought 2b is rated before the search explores it.
Candidate progress: 10-4=6, 13-9=4 (left: 6, 4). Answer sure, maybe or impossible.
- Thought 2b → Solutionsolved
- The last move from Thought 2b makes 24, so the whole path is returned as the solution.
10-4=6; 13-9=4; 6*4=24
Breadth-first beam
Expand every state on the frontier together, rate all the children, and keep only the best b for the next level; there is no backtracking.
All steps
Parts of this pattern
- Problem Input / output
- The puzzle to solve: make 24 from 4, 9, 10 and 13 using + - * /.
- Proposer LLM call
- Proposes several next thoughts for every state on the frontier at once, giving one whole level of children.
- Evaluator Evaluator
- Rates every child sure, maybe or impossible and turns the ratings into scores for ranking.
- Beam Memory
- The frontier: only the top-scoring b states of each level are kept; the rest are discarded for good.
- Solution Input / output
- The best final state after the last level, checked to make 24.
- Problem → Proposer
- The starting numbers form the first, one-state frontier.
Input: 4 9 10 13
- Proposer → Evaluatorlevel
- Every proposed child of every frontier state goes to the evaluator together.
10-4=6 (left: 6 9 13); 4+9=13 (left: 10 13 13); 13*4=52 (left: 9 10 52); 13-9=4 (left: 4 4 10) ...
- Evaluator → Beamtop b
- The children are ranked by score and only the best b are kept as the new frontier.
Keep: 10-4=6 (sure), 13-9=4 (sure). Drop: 13*4=52 (impossible), 4+9=13 (maybe, ranked lower).
- Beam → Proposernext level
- Every state in the beam is expanded at the next level, side by side.
Frontier: [10-4=6 (left: 6 9 13), 13-9=4 (left: 4 4 10)]
- Beam → Solutionbest
- After the last level, the best-scoring state that passes the check is returned.
10-4=6; 13-9=4; 6*4=24
← → neighboursesc atlas
Tree of Thoughts
Also known as ToT · Deliberate search
Generate several candidate thoughts at each step, score them, expand the promising branches and backtrack at dead ends.
Introduced by Yao et al. (2023) · Princeton University & Google DeepMind
NeurIPS 2023. Jieyi Long's 'Large Language Model Guided Tree-of-Thought' (https://arxiv.org/abs/2305.08291) appeared concurrently, the same month.
Measured over one run
Depth-first
- Nodes
- 8. Boxes in the diagram: models, tools, stores, people and plain code.
- LLM calls
- 6. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
- Tool calls
- 0. Messages sent to a tool node in the first scenario.
- Steps
- 8. Steps in the first scenario, with repeated steps counted every time they run.
- Scenarios
- 1. Distinct example runs you can switch between on the diagram.
- Loop
- no. Whether the flow cycles back on itself until a condition is met.
- Parallel
- yes. Whether two or more messages are in flight at the same time.
- Branch
- yes. Whether the flow takes one path out of several, chosen at run time.
- Human in the loop
- no. Whether a person appears as a node, to approve, correct or answer.
- Autonomy
- medium. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
- Cost
- high. Relative token spend per request.
- Latency
- high. Relative wall-clock time per request.
Breadth-first beam
- Nodes
- 5. Boxes in the diagram: models, tools, stores, people and plain code.
- LLM calls
- 6. Model invocations in the first scenario: each time a model or agent node starts thinking, plus each branch it decides.
- Tool calls
- 0. Messages sent to a tool node in the first scenario.
- Steps
- 4. Steps in the first scenario, with repeated steps counted every time they run.
- Scenarios
- 1. Distinct example runs you can switch between on the diagram.
- Loop
- yes. Whether the flow cycles back on itself until a condition is met.
- Parallel
- no. Whether two or more messages are in flight at the same time.
- Branch
- yes. Whether the flow takes one path out of several, chosen at run time.
- Human in the loop
- no. Whether a person appears as a node, to approve, correct or answer.
- Autonomy
- medium. How much the model decides the control flow itself, from fixed code paths (low) to open-ended (high).
- Cost
- high. Relative token spend per request.
- Latency
- high. Relative wall-clock time per request.
Counted from the first scenario's timeline when the site is built.
Problem
Left-to-right generation commits to every token as it writes. For problems that need exploration, such as puzzles, planning or creative constraints, the first idea is often a dead end, and a model that has already written three confident steps down the wrong path has no way to retreat. Chain-of-thought gives the model room to reason but still only one path. Take the Game of 24: from 4, 9, 10 and 13, making 24 requires trying several first moves, and most of them fail.
How it works
Tree of Thoughts, from Yao et al., treats reasoning as search over a tree whose nodes are partial solutions. A “thought” is a coherent chunk of progress, such as one equation, one paragraph plan or one move.
From the problem, the model proposes several candidate thoughts: Thought 1, Thought 2 and Thought 3. The evaluator, usually the same model under a different prompt, rates each one, for example sure, maybe or impossible. Hopeless branches are pruned and never expanded, which is why thoughts 1 and 3 turn red in the diagram.
The search then expands the promising branch, here producing Thought 2a and Thought 2b. These get rated too, and both look possible, so the search tries 2a first. Nothing it proposes from there works, so the call for 2a returns nothing and the search backtracks to the parent and tries 2b. The path through 2b reaches the solution.
Pruning and backtracking are different. Pruning discards a candidate the evaluator already rates impossible, before spending anything on it. Backtracking happens after a branch looked fine and then failed.
The paper explores breadth-first and depth-first variants. The main diagram is depth-first: when a branch returns nothing, the search falls back to its parent. The breadth-first tab expands every surviving state of a level together and keeps only the best few, a beam, so it never backtracks; the paper used this form for the Game of 24.
When to use
- The problem needs exploration or lookahead, and early mistakes are costly.
- Partial progress can be judged: a half-built equation, a plan with a clear conflict.
- Single-pass or chain-of-thought attempts fail often enough to justify the cost.
When to avoid
- Most inputs are easy. Paying for a search on every request is wasteful.
- Partial states cannot be evaluated, so the scores would be noise.
- You need a low-latency or low-cost answer.
Trade-offs
Search multiplies model calls: each level proposes several thoughts and rates each of them. The quality gain on hard problems can be large. In the paper’s Game of 24 experiments, a tree search solved far more problems than chain-of-thought prompting, but at many times the tokens. Tune breadth and depth to your budget, and cache or deduplicate states.
Failure modes
- Unreliable evaluator: it prunes the right branch or keeps a doomed one. Use a deterministic check wherever one exists.
- Duplicate thoughts: the proposer repeats itself and wastes breadth. Ask for distinct options and deduplicate.
- Budget blowout: branching grows exponentially. Cap breadth, depth and total calls.
- Premature pruning: a “maybe” gets discarded. Prune only on confident negatives.
Build it with your AI
Depth-first
Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.
Implement the Tree of Thoughts pattern in this codebase, using the LLM client and language we already use.
## Goal
Solve a problem by depth-first search over partial solutions: propose several next thoughts, rate each, expand the most promising, and backtrack when a branch dead-ends.
## How it works
1. Represent a state as the problem plus the list of thoughts taken so far; push the empty state onto a stack.
2. Pop the top state. If a deterministic check says it solves the problem, return its thoughts as the solution.
3. If the state is below the depth limit, call the model with the Thought proposal prompt, passing the problem and the state, and parse up to breadth distinct candidate thoughts.
4. Rate each candidate with the Thought evaluation prompt and read the first word as sure, maybe or impossible.
5. Drop the impossible candidates, and push the rest onto the stack so the best-rated is popped next (sure before maybe).
6. When a state has no surviving children or is at the depth limit, do nothing more with it; popping the next state is the backtrack to its parent's next-best sibling.
## Prompts
### Thought proposal prompt
```markdown
Problem: {{problem}}
Progress so far:
{{state}}
Propose {{breadth}} different next steps. Each must be a single, concrete
move that follows from the progress above, and they must not duplicate
each other. Label them A, B, C.
```
### Thought evaluation prompt
```markdown
Problem: {{problem}}
Candidate progress:
{{thought}}
Can this candidate still lead to a full solution? Check it carefully
against the problem's constraints.
Answer with exactly one word: sure, maybe or impossible.
Then give one sentence of reasoning.
```
## Stop rule
Stop when a state passes the solution check, when the stack is empty, or after 100 model calls. If no solution is found, return the deepest state reached with a status saying the search failed.
## Requirements
- Make the breadth (default 3), the depth limit (default 3) and the total model-call budget (default 100) parameters.
- If a rating reply does not start with sure, maybe or impossible, treat it as maybe: never prune a branch on a parse failure.
- Drop proposed thoughts that duplicate a sibling or a state already visited, so the breadth is not wasted.
- Use a deterministic solution check (here, evaluate the arithmetic) wherever the task allows, instead of asking the model.
- Add a Game of 24 test with a stub LLM rating 10-4=6 sure, 4+9=13 and 13*4=52 impossible, then 6+9=15 sure, 13-9=4 maybe, and every child of 6+9=15 impossible; assert 4 proposal calls and the path 10-4=6, 13-9=4, 6*4=24.
Breadth-first beam
Paste this into Claude Code, Cursor, ChatGPT or any AI coding assistant.
Implement the Tree of Thoughts pattern (Breadth-first beam variant) in this codebase, using the LLM client and language we already use.
## Goal
Solve a problem by breadth-first search with a beam: at each level expand every kept state, rate every child, and keep the best few, for a fixed number of levels.
## How it works
1. Start the frontier as a list holding one empty state: the problem with no thoughts taken.
2. For every state on the frontier, call the model with the Thought proposal prompt and parse up to breadth candidate thoughts, giving the next level's children.
3. Rate every child with the Thought evaluation prompt, mapping sure, maybe and impossible to scores of 20, 1 and 0.001 as the paper does, and summing over the samples if you rate more than once.
4. Sort the children by score and keep the top beam-width as the new frontier; dropping impossible children outright is a simplification that saves calls.
5. Repeat for the configured number of levels, then return the best-scoring final state that passes the solution check.
## Prompts
### Thought proposal prompt
```markdown
Problem: {{problem}}
Progress so far:
{{state}}
Propose {{breadth}} different next steps. Each must be a single, concrete
move that follows from the progress above, and they must not duplicate
each other. Label them A, B, C.
```
### Thought evaluation prompt
```markdown
Problem: {{problem}}
Candidate progress:
{{thought}}
Can this candidate still lead to a full solution? Check it carefully
against the problem's constraints.
Answer with exactly one word: sure, maybe or impossible.
Then give one sentence of reasoning.
```
## Stop rule
Stop after the last level, or earlier if the frontier is empty. If no final state passes the solution check, return the best-scoring one with a status saying it is unverified.
## Requirements
- Make the beam width (default 5), the breadth per state (default 5) and the number of levels (default 3) parameters.
- Run the proposal and rating calls for one level concurrently, with a cap on calls in flight (default 8).
- If a rating reply does not start with sure, maybe or impossible, score it as maybe; never prune on a parse failure.
- Deduplicate children with the same state across the whole level before rating, so the beam is not filled with copies.
- Add a test with a stub LLM, beam width 2 and 3 levels; assert 5 proposal calls (1 + 2 + 2), 2 states kept after each of the first two levels, and the exact path 10-4=6, 13-9=4, 6*4=24.
Prompt templates
Depth-first · Breadth-first beam
Problem: {{problem}}
Progress so far:{{state}}
Propose {{breadth}} different next steps. Each must be a single, concretemove that follows from the progress above, and they must not duplicateeach other. Label them A, B, C.Problem: {{problem}}
Candidate progress:{{thought}}
Can this candidate still lead to a full solution? Check it carefullyagainst the problem's constraints.
Answer with exactly one word: sure, maybe or impossible.Then give one sentence of reasoning.Related patterns
Composes well with:
Sources
From the Agentic Atlas by Joran Kikke · Edit on GitHub · Suggest a correction