DEV Community

Devanshu Biswas
Devanshu Biswas

Posted on

Tree of Thoughts: How to Make an LLM Explore Instead of Guess

Chain-of-Thought makes a model "think step by step" — but down a single path. One wrong first step and the whole answer is doomed. Tree of Thoughts fixes that by turning reasoning into search. Day 6 of my PromptFromZero series.

The idea: branch, score, prune

Instead of one line of reasoning, treat each partial solution as a node and generate several next steps from it:

const branches = await llm(`Give 3 different next steps for: ${state}`, { n: 3 });
Enter fullscreen mode Exit fullscreen mode

Now you're exploring a tree, not betting on one path.

Let the model judge itself

For each branch, ask the LLM to score how promising it is:

const score = await llm(`Rate 1-10 how likely this leads to the goal: ${thought}`);
Enter fullscreen mode Exit fullscreen mode

The model becomes its own evaluator — no external solver needed.

Prune to a beam

Keep only the top-b branches; throw the rest away. This stops the tree exploding:

const frontier = scored.sort((a, b) => b.score - a.score).slice(0, beamWidth);
Enter fullscreen mode Exit fullscreen mode

Search + backtrack

Repeat generate → score → prune on the survivors. Because you kept several options, if the best branch hits a dead end you fall back to the next-best — real backtracking that Chain-of-Thought can't do.

When to use it

ToT shines on multi-step problems with a checkable goal: puzzles (Game of 24), planning, code with tests, math. It costs many more LLM calls than CoT, so it's overkill for simple Q&A — but powerful when the answer needs search.

🌳 Step through it on Game of 24 (watch branches get kept/pruned): https://dev48v.infy.uk/prompt/day6-tree-of-thoughts.html

Day 6 of PromptFromZero.

Top comments (0)