> ## Documentation Index
> Fetch the complete documentation index at: https://docs-v1.latitude.so/llms.txt
> Use this file to discover all available pages before exploring further.

# Tree of Thoughts

> Implement multiple branching reasoning paths to solve complex problems with Latitude

## What is Tree of Thoughts?

Tree of Thoughts (ToT) is an advanced prompting technique that enables AI models to explore multiple reasoning paths in parallel, evaluate their potential, and select the most promising branches to develop further—similar to how humans explore different solutions when tackling complex problems.

## Why Use Tree of Thoughts?

* **Improved Problem-Solving**: Systematically explore multiple solution pathways
* **Better Planning**: Map out different approaches before committing to one
* **Enhanced Creativity**: Generate diverse solutions to open-ended problems
* **Reduced Errors**: Catch mistakes by comparing different reasoning branches
* **Complex Decision-Making**: Break down complex decisions into evaluable components

## Basic Implementation in Latitude

Here's a simple Tree of Thoughts example for solving a complex problem:

<CodeGroup>
  ```markdown Basic ToT theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.3
  ---

  # Tree of Thoughts Problem-Solving

  Let's solve this problem by exploring multiple lines of reasoning:

  **Problem**: {{ problem_statement }}

  ## Initial Thought Branches:

  ### Branch A: [First Approach]
  1. Initial premise: ...
  2. Reasoning step: ...
  3. Intermediate conclusion: ...
  4. Further implications: ...
  5. Potential outcome: ...

  ### Branch B: [Alternative Approach]
  1. Initial premise: ...
  2. Reasoning step: ...
  3. Intermediate conclusion: ...
  4. Further implications: ...
  5. Potential outcome: ...

  ### Branch C: [Creative Approach]
  1. Initial premise: ...
  2. Reasoning step: ...
  3. Intermediate conclusion: ...
  4. Further implications: ...
  5. Potential outcome: ...

  ## Branch Evaluation:
  - Branch A strength: ...
  - Branch B strength: ...
  - Branch C strength: ...

  ## Final Solution Path:
  [Select most promising branch and develop it further]
  ```
</CodeGroup>

## Advanced Implementation with Parameters

<CodeGroup>
  ```markdown Advanced ToT theme={null}
  ---
  provider: OpenAI
  model: gpt-4.1
  temperature: 0.4
  ---

  # Tree of Thoughts: Advanced Problem-Solving

  Let's solve this complex problem using a Tree of Thoughts approach with {{ thought_branches }} initial branches.

  **Problem**: {{ problem_statement }}

  ## Thought Generation:

  {{ for branch in thought_branches }}
    ### Branch {{branch}}: [Name this approach]

    {{ for level in levels}}
      **Level {{level}} thinking:**
      - [Reasoning steps at this level]
      - [Interim conclusions]

      **Branch {{branch}} Outcomes:**
      - [Describe expected outcomes of this reasoning path]
      {{ '\n\t'}}
    {{ endfor }}

    ## Branch Evaluation:

    {{ for criteria in evaluation_criteria}}
    ### Criterion: {{ criteria }}
      - Branch {{branch}}: [Score 1-10] - [Justification]
      {{ '\n'}}
    {{ endfor }}
  {{endfor}}

  ## Solution Development:

  {{ if branch_selection_method === "best_single" }}
    **Selected Branch**: [Identify best overall branch]
    **Development**: [Fully develop this single branch to conclusion]
  {{ else if branch_selection_method === "hybrid" }}
    **Hybrid Solution**: [Combine elements from multiple branches]
    **Integration Points**: [Explain how different branch elements connect]
  {{ else if branch_selection_method === "weighted" }}
    **Weighted Solution**: [Proportionally represent branches based on scores]
    **Weighting Factors**: [Explain the weights applied to each branch]
  {{ endif }}

  ## Final Answer:
  [Complete solution with justification]

  ```
</CodeGroup>

<Note>You can use `{{ '\n\t' }}` to give indentation in the code block. Is more easy to follow what's doing the prompt</Note>

## Implementing ToT With Chains

Latitude's chain feature allows for structured Tree of Thoughts reasoning:

````markdown chain theme={null}
---
provider: OpenAI
model: gpt-4o
temperature: 0.4
---

# Tree of Thoughts with Chains

```markdown
<step>
# Step 1: Generate Multiple Thought Branches

**Problem**: {{ problem_statement }}

Let me generate three distinct approaches to solving this problem:

## Branch A: [First Approach]
1. Initial premise: ...
2. Reasoning: ...
3. Implications: ...

## Branch B: [Second Approach]
1. Initial premise: ...
2. Reasoning: ...
3. Implications: ...

## Branch C: [Third Approach]
1. Initial premise: ...
2. Reasoning: ...
3. Implications: ...
</step>

<step>
# Step 2: Evaluate Each Thought Branch

Evaluating the branches generated in previous step:

## Branch A Evaluation:
- Strengths: ...
- Weaknesses: ...
- Confidence score (1-10): ...

## Branch B Evaluation:
- Strengths: ...
- Weaknesses: ...
- Confidence score (1-10): ...

## Branch C Evaluation:
- Strengths: ...
- Weaknesses: ...
- Confidence score (1-10): ...
</step>

<step>
# Step 3: Select and Develop Best Branch

Based on my evaluation:

The most promising approach is **Branch [X]** because:
[Justification for selection]

Let me develop this branch further:

## Detailed Development:
1. [Further reasoning steps]
2. [Handling edge cases]
3. [Addressing potential objections]
4. [Additional insights]

## Final Solution:
[Complete answer to the original problem]
</step>
````

## Multi-Agent ToT

Implement Tree of Thoughts with agent collaboration, you can [play with it here](https://app.latitude.so/share/d/6daaa005-cc77-4009-9150-f905505233eb).

<CodeGroup>
  ```markdown Multi-Agent theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.3
  type: agent
  agents:
    - agents/creator
    - agents/critic
    - agents/synthesizer
  ---

  # Multi-Agent Tree of Thoughts

  Solve the following problem using a collaborative multi-agent Tree of Thoughts approach:

  **Problem**: {{ problem_statement }}

  ## Process:
  1. The **creator** agent will generate multiple solution branches
  2. The **critic** agent will evaluate each branch's strengths and weaknesses
  3. The **synthesizer** agent will select and refine the most promising approach

  Let's begin solving this step by step.
  ```

  ```markdown Creator Agent theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.7
  type: agent
  path: agents/creator
  ---

  # Creator Agent: Thought Branch Generation

  Generate three distinct approaches to solving the problem.

  For each approach:
  1. Use different first principles or starting assumptions
  2. Explore creative and unexpected angles
  3. Trace the logical steps from premise to conclusion

  Don't evaluate the branches yet - focus on diversity of thought.
  ```

  ```markdown Critic Agent theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.1
  type: agent
  path: agents/critic
  ---

  # Critic Agent: Branch Evaluation

  Carefully evaluate each of the thought branches provided.

  For each branch:
  1. Identify logical fallacies or unwarranted assumptions
  2. Check alignment with known facts and constraints
  3. Consider edge cases and exceptions
  4. Assign a confidence score and explain reasoning

  Be rigorous and analytical in your assessment.
  ```

  ```markdown Synthesizer Agent theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.3
  type: agent
  path: agents/synthesizer
  ---

  # Synthesizer Agent: Solution Development

  Based on the generated branches and their evaluations:

  1. Select the most promising approach OR
  2. Create a hybrid solution incorporating the strongest elements

  Then develop this approach in detail, addressing:
  - Any weaknesses identified by the critic
  - Practical implementation steps
  - Expected outcomes
  ```
</CodeGroup>

## Best Practices for Tree of Thoughts

<AccordionGroup>
  <Accordion title="Thought Branch Design">
    **Effective Branch Creation**:

    * Create branches that start from genuinely different premises or approaches
    * Ensure sufficient diversity between branches to explore the solution space
    * Balance breadth (number of branches) with depth (steps in each branch)
    * Use structured formats that make branches easy to compare

    **Branch Evaluation**:

    * Define clear evaluation criteria upfront
    * Assign quantitative scores when possible
    * Document reasoning for evaluations
    * Consider both short-term solutions and long-term implications
  </Accordion>

  <Accordion title="Implementation Tips">
    **Technical Implementation**:

    * Use parameters to control branch count, depth, and evaluation criteria
    * Balance temperature settings - higher for branch generation, lower for evaluation
    * Use larger context models (GPT-4) for complex ToT problems
    * Store intermediate results in variables for complex multi-step ToT

    **Process Optimization**:

    * Start with 2-3 branches for simpler problems, 4-5 for complex ones
    * Consider 3-5 steps of reasoning per branch as a starting point
    * Use Latitude's chain feature for structured ToT implementation
    * Try different branch combination methods (best single, hybrid, weighted)
  </Accordion>

  <Accordion title="Problem Selection">
    **Ideal Problem Types**:

    * **Strategic Planning**: Multiple viable approaches with complex tradeoffs
    * **Creative Challenges**: Open-ended problems with no clear "right" answer
    * **Analysis Tasks**: Situations requiring consideration of multiple perspectives
    * **Decision Making**: Complex decisions with many factors to weigh
    * **Troubleshooting**: Problems where the root cause isn't immediately obvious

    **Less Suitable Problems**:

    * Simple factual queries with definitive answers
    * Highly constrained problems with limited solution paths
    * Routine tasks with established procedures
  </Accordion>

  <Accordion title="Advanced Techniques">
    **ToT Variations**:

    * **Recursive ToT**: Apply ToT within branches of a larger ToT structure
    * **Adversarial ToT**: Intentionally create opposing branches to stress-test solutions
    * **Collaborative ToT**: Distribute branches across multiple specialized agents
    * **Time-Horizon ToT**: Create branches exploring short, medium, and long-term impacts
    * **Probabilistic ToT**: Assign probability weights to different branches

    **Integration with Other Techniques**:

    * Combine with Chain-of-Thought within branches
    * Use Few-shot examples to guide branch generation
    * Apply Self-Consistency to evaluate branch quality
  </Accordion>
</AccordionGroup>

## Applications in Different Domains

<CodeGroup>
  ```markdown Strategic Planning ToT theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.4
  ---

  # Strategic Planning Tree of Thoughts

  Let's evaluate different strategic approaches for {{ business_scenario }}:

  ## Branch 1: Market Expansion Strategy
  1. **Current Market Analysis**: [Assessment of current position]
  2. **Target Market Identification**: [New markets to enter]
  3. **Entry Strategy**: [How to penetrate new markets]
  4. **Resource Requirements**: [What's needed for execution]
  5. **Risk Assessment**: [Potential challenges and mitigation]

  ## Branch 2: Product Innovation Strategy
  1. **Current Product Evaluation**: [Assessment of product lineup]
  2. **Innovation Opportunities**: [Areas for new development]
  3. **R&D Framework**: [How to approach innovation]
  4. **Go-to-Market Strategy**: [Bringing innovations to customers]
  5. **Competitive Advantage Analysis**: [How this creates distinction]

  ## Branch 3: Operational Optimization Strategy
  1. **Efficiency Assessment**: [Current operational bottlenecks]
  2. **Process Redesign**: [New operational models]
  3. **Technology Integration**: [Leveraging new technologies]
  4. **Cost Structure Impact**: [Financial implications]
  5. **Implementation Roadmap**: [Execution timeline and milestones]

  ## Strategy Evaluation:
  [Comparative analysis of the three strategic paths]

  ## Recommended Approach:
  [Final strategic recommendation with implementation plan]
  ```

  ```markdown Creative Problem-Solving ToT theme={null}
  ---
  provider: OpenAI
  model: gpt-4o
  temperature: 0.7
  ---

  # Creative Problem-Solving Tree of Thoughts

  Let's generate innovative solutions for {{ creative_challenge }}:

  ## Branch 1: Conventional Approach Reimagined
  1. **Existing Patterns**: [Identify current solutions]
  2. **Pattern Breaking**: [Ways to challenge assumptions]
  3. **Novel Combinations**: [Unexpected element combinations]
  4. **Refinement**: [Shaping the concept]
  5. **Practical Application**: [Making it work in reality]

  ## Branch 2: First Principles Approach
  1. **Problem Deconstruction**: [Break into fundamental elements]
  2. **First Principles**: [Identify core truths/needs]
  3. **Solution Building**: [Construct from basics upward]
  4. **Theoretical Evaluation**: [Testing against principles]
  5. **Practical Translation**: [Moving from theory to practice]

  ## Branch 3: Lateral Thinking Approach
  1. **Analogous Domains**: [Find parallel situations elsewhere]
  2. **Metaphorical Thinking**: [Apply metaphors to problem]
  3. **Random Stimulus**: [Introduce unrelated concepts]
  4. **Connection Building**: [Forge new relationship paths]
  5. **Solution Crystallization**: [Form coherent solution]

  ## Idea Evaluation:
  [Compare novelty, feasibility, and impact of each approach]

  ## Selected Creative Solution:
  [Final concept and implementation considerations]
  ```
</CodeGroup>

## Common Pitfalls and Solutions

<Warning>
  **Avoid These Common Mistakes**:

  * **Shallow Branches**: Creating branches that aren't meaningfully different from each other
  * **Premature Evaluation**: Judging branches before they're fully developed
  * **Confirmation Bias**: Favoring branches that align with preconceptions
  * **Neglecting Constraints**: Failing to consider real-world limitations
  * **Excessive Complexity**: Creating too many branches or too much depth for the problem
</Warning>

<Tip>
  **Pro Tips**:

  * Start with a clear problem statement before branching
  * Use different cognitive approaches for each branch (analytical, creative, critical)
  * Consider allocating more tokens to the most promising branches
  * Document your reasoning at each step for transparency
  * Try different branch-recombination methods for complex problems
</Tip>

## Next Steps

Now that you understand Tree of Thoughts, explore these related techniques:

* [Chain-of-Thought](/examples/techniques/chain-of-thought) - Step-by-step reasoning within branches
* [Self-Consistency](/examples/techniques/self-consistency) - Verify solutions through multiple attempts
* [Role Prompting](/examples/techniques/role-prompting) - Assign different thinking styles to branches
* [Multi-Agent Collaboration](/examples/techniques/multi-agent-collaboration) - Distribute reasoning across agents
