Back to Prompting Recipes

Tree of Thought

ReasoningExploration

Reasoning

Explore multiple reasoning paths before selecting a solution.

Summary

Tree of Thought (ToT) extends Chain of Thought by exploring multiple reasoning paths simultaneously. Instead of following a single line of reasoning, ToT creates a tree-like structure where different approaches are considered, evaluated, and either pursued further or abandoned. This technique is particularly effective for complex problems where the best approach isn't immediately obvious.

Implementation

  1. Generate multiple approaches by instructing the model to "explore several different ways to solve this problem" or "consider at least 3 different approaches."
  2. Evaluate each approach by asking the model to list pros and cons or assign a score to each path based on relevant criteria.
  3. Expand promising branches by requesting further development of the most promising approaches while abandoning less effective ones.
  4. Combine insights from different branches to create a solution that incorporates the strengths of multiple approaches.
  5. Structure your prompt to encourage clear labeling of different approaches and their evaluations.

Evaluation criteria

  • Feasibility: Is this approach implementable?
  • Efficiency: Does this path minimize steps/resources?
  • Correctness: Does this lead to the right answer?
  • Novelty: Does this offer unique insights?

Best for

  • Open-ended problems with multiple valid solutions
  • Creative tasks requiring diverse perspectives
  • Decision-making scenarios with trade-offs
  • Complex planning where multiple approaches exist
Code View

Tree of Thought Implementation

// Tree of Thought recipe using OpenAI
// Install: bun add openai

import OpenAI from "openai";

async function main() {
  const input = "Add your prompt here.";
  const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
  const system = "You are a senior AI engineer and technical writer. Use the prompting technique to answer the request clearly and precisely. Recipe: Tree of Thought. Description: Explore multiple reasoning paths before selecting a solution. Focus: Reasoning Provide actionable, implementation-ready guidance.";
  const user = `Request: ${input}`;

  const openaiResponse = await openai.chat.completions.create({
    model: "gpt-4o-mini",
    messages: [
      { role: "system", content: system },
      { role: "user", content: user },
    ],
  });

  const openaiText = openaiResponse.choices[0]?.message?.content?.trim() ?? "";

  console.log(openaiText);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});