Tree of Thoughts

By the Syncfusion DevTutorials team · Last updated

Explore several lines of reasoning: generate distinct options, score them with weighted criteria, keep the best with a beam search, and a single-prompt version for everyday use.

Chain of thought follows one line of thinking from start to end. If the first step is a bad choice, every step after it builds on that mistake, and there is no going back.

People don't solve hard problems that way. They think of a few options, compare them, drop the weak ones, and then look more closely at the best one. Tree of thoughts, or ToT, asks the model to do the same thing: branch out into options, judge each one, and keep the best.

The main idea: when a problem can be solved in many different ways, come up with several options, score them against clear rules, and build on the best one.

A Real-World Example

Think about planning a weekend trip.

  • You don't book the first idea that comes into your head.
  • You list three options: the beach, the mountains, or a city.
  • You judge each one on how much it costs, how long it takes to get there, and what the weather will be like.
  • You choose the best one, and then plan it in detail: where to stay, and what to do.

That is a tree. The trip is the root, the three options are the branches, and only the best branch grows further.

What You Will Learn

In this lesson, you will learn:

  1. How tree of thoughts is different from chain of thought.
  2. The three moves: generate, evaluate, and expand.
  3. How to ask for several options, and read them with code.
  4. How to score options against rules that have weights.
  5. How to search a tree, keeping only the best branches.
  6. A one-prompt version that you can use in any chat.

A full tree of thoughts needs code that calls the model many times. This lesson shows the loop in plain JavaScript, so you can see how it works. It also shows a one-prompt version that you can try in the Prompt sandbox.

From a Chain to a Tree

Chain of thought follows one path. Tree of thoughts comes up with several options at each step, scores them, and only builds on the best ones.

Chain of thoughtTree of thoughts
How many paths it triesOneSeveral
Can it drop a bad idea?NoYes, at each step
Calls to the modelOneMany
Best forProblems that have one clear way to solve themPlanning, design, puzzles, and choices where each option has pros and cons

A tree has three moves, which repeat at each level:

  1. Generate a few possible next steps.
  2. Evaluate each one by giving it a score.
  3. Expand only the best ones, and repeat.

Generate: Ask for Several Candidates

The first move is a prompt that asks for a fixed number of different options, in a numbered list that your code can read.

Ask for several distinct options

function generatePrompt(problem, n) {
  return problem + "\nPropose " + n + " different approaches. Make each one clearly different from the others.\nReply as a numbered list, one approach per line, under 20 words each.";
}

console.log(generatePrompt("Our app's sign-up page loses 60% of visitors. How could we fix it?", 3));

Expected output:

Our app's sign-up page loses 60% of visitors. How could we fix it?
Propose 3 different approaches. Make each one clearly different from the others.
Reply as a numbered list, one approach per line, under 20 words each.

Then read the list with code. Replies may use 1. or 1), and may add a line before the list. The code keeps only the numbered lines. The reply below is a sample written for this example.

Read the numbered options from a reply

function parseOptions(reply) {
  return reply
    .split("\n")
    .map((line) => line.match(/^\s*\d+[.)]\s*(.+)$/))
    .filter(Boolean)
    .map((m) => m[1].trim());
}

const reply = "Here are three ideas:\n1. Cut the form to email and password only.\n2) Add sign-in with Google.\n3. Show a short demo video before the form.";
const options = parseOptions(reply);
console.log(options.length + " options");
options.forEach((o, i) => console.log(i + 1, o));

Expected output:

3 options
1 Cut the form to email and password only.
2 Add sign-in with Google.
3 Show a short demo video before the form.

Evaluate: Score Each Option

Next, judge each option. You can ask the model to rate each one, or you can score them yourself. Either way, use clear rules with weights, so that "best" always means the same thing.

Score options against weighted criteria

const weights = { impact: 0.5, effort: 0.3, risk: 0.2 };

// Ratings from 1 (bad) to 10 (good). For effort and risk, 10 means low effort or low risk.
const options = [
  { name: "Shorter form", impact: 8, effort: 9, risk: 9 },
  { name: "Google sign-in", impact: 7, effort: 5, risk: 7 },
  { name: "Demo video", impact: 4, effort: 6, risk: 9 },
];

const scored = options
  .map((o) => ({ name: o.name, score: Math.round((o.impact * weights.impact + o.effort * weights.effort + o.risk * weights.risk) * 10) / 10 }))
  .sort((a, b) => b.score - a.score);

scored.forEach((o) => console.log(o.score, o.name));
console.log("Expand:", scored[0].name);

Expected output:

8.5 Shorter form
6.4 Google sign-in
5.6 Demo video
Expand: Shorter form

The weights are where your own judgement comes in. If speed mattered more than impact, you would give effort a higher weight, and a different option might win.

When the model does the rating, ask it to end with one fixed line that your code can read, like Score: 7.

Build a rating prompt and read the score

function ratePrompt(problem, option) {
  return "Problem: " + problem + "\nProposed approach: " + option + "\nRate how well this solves the problem from 1 to 10. Give one sentence of reason, then the last line as: Score: number";
}

function readScore(reply) {
  const m = reply.match(/Score:\s*(\d+)/i);
  return m ? Math.min(10, Number(m[1])) : null;
}

console.log(ratePrompt("Sign-up page loses visitors.", "Cut the form to email and password only."));
console.log("---");
console.log(readScore("Fewer fields means less effort for users.\nScore: 8"));

Expected output:

Problem: Sign-up page loses visitors.
Proposed approach: Cut the form to email and password only.
Rate how well this solves the problem from 1 to 10. Give one sentence of reason, then the last line as: Score: number
---
8

Easy Way to Remember: a tree of thoughts means come up with options, score them, keep the best, and repeat. The scoring step is what makes it better than just asking for one answer.

Expand: Search the Tree

Each option you keep can branch out again. To stop the tree from growing too big, keep only the best few options at each level. This is called a beam search, and the number of options you keep is called the beam width.

The next box searches a small tree whose scores are already filled in. In a real app, each score would come from a rating prompt.

Keep the best two branches at each level

const tree = {
  "Shorter form": { score: 8.5, next: { "Remove phone field": 8, "Ask name later": 9 } },
  "Google sign-in": { score: 6.4, next: { "Add Apple too": 6, "Google only": 5 } },
  "Demo video": { score: 5.6, next: { "30-second video": 6 } },
};

const beam = 2;
const level1 = Object.entries(tree).sort((a, b) => b[1].score - a[1].score).slice(0, beam);
console.log("Level 1 kept:", level1.map(([name]) => name).join(", "));

const level2 = [];
for (const [parent, node] of level1) {
  for (const [child, score] of Object.entries(node.next)) level2.push({ path: parent + " > " + child, score });
}
level2.sort((a, b) => b.score - a.score);
console.log("Best plan:", level2[0].path, "(" + level2[0].score + ")");

Expected output:

Level 1 kept: Shorter form, Google sign-in
Best plan: Shorter form > Ask name later (9)

"Demo video" was dropped at level 1, so its branch was never explored. This saves calls to the model, but it also means that an idea that looked weak at first can't come back. A wider beam explores more options, but it costs more.

Tree of Thoughts in One Prompt

A full tree needs many calls and some code. For everyday use, a one-prompt version gets much of the benefit. Ask the model to come up with options, judge them, and then develop the best one, all in one reply.

Compare it with a plain request:

Prompt A usually gives you one idea, or a long list in no order. Prompt B shows you the options, their scores, and a clear choice that you can question.

Scores from a model are opinions, not measurements. A small model may give every option an 8, or give different scores each time you run it. Use the scores to organise your thinking, and then use your own judgement, especially for important decisions.

Common mistakes

MistakeWrong ideaCorrect ideaWhat goes wrong
Options that are too alike"Three options are enough."Ask for approaches that are clearly different.You get three versions of the same idea, so there is nothing to compare.
No rules for scoring"Just choose the best one."Score against named rules that have weights."Best" means something different every time.
No limit on branches"Explore everything."Keep a beam of 2 or 3 options at each level.You get dozens of calls to the model, and long waits.
Trusting the scores"It scored 9, so it's the right choice."Use the scores as a guide, then decide for yourself.A poor choice gets made because it looked sure.
ToT for simple tasks"More thinking is always better."Use it only when there are real options to choose from.Easy questions get slow replies.
Output that code can't read"I'll read the options myself."Ask for a numbered list and a Score line.Code can't read the options or the scores.

Exercises

In each exercise, the last line should give back a value. Press Check to compare your value with the expected answer.

What to Remember

  • Tree of thoughts explores several options, instead of one line of thinking.
  • It repeats three moves: generate, evaluate, and expand.
  • Ask for options that are clearly different, in a numbered list.
  • Score the options with rules that have weights, so that "best" always means the same thing.
  • Keep only the best few options at each level. The beam width keeps the cost down.
  • A one-prompt version works well for everyday decisions.
  • The model's scores are opinions. Use your own judgement too.

Next: Self-Consistency Prompting, where you ask the same question several times and trust the most common answer.