Bird
Raised Fist0
Agentic AIml~5 mins

Tree-of-thought for complex decisions in Agentic AI

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction

Tree-of-thought helps break down big, tricky decisions into smaller steps. It makes thinking clearer and helps find better answers.

When you need to solve a problem with many possible steps or choices.
When you want to explore different ways to reach a goal before deciding.
When a decision depends on several smaller decisions in order.
When you want to explain your thinking process clearly.
When you want to improve AI reasoning by showing step-by-step thoughts.
Syntax
Agentic AI
tree_of_thought = {
    'current_state': initial_state,
    'children': [
        {'action': action1, 'result_state': state1, 'children': [...]},
        {'action': action2, 'result_state': state2, 'children': [...]},
        # ...
    ]
}

# To explore, recursively expand children nodes with possible actions and results.

The tree starts from the current situation (state).

Each child node represents a possible action and its outcome.

Examples
This tree shows two choices from the start: go left or go right.
Agentic AI
tree = {
    'current_state': 'start',
    'children': [
        {'action': 'go left', 'result_state': 'left room', 'children': []},
        {'action': 'go right', 'result_state': 'right room', 'children': []}
    ]
}
This function adds next possible steps to a node in the tree.
Agentic AI
def expand_node(node):
    # Add possible next steps as children
    for action in possible_actions(node['result_state']):
        new_state = apply_action(node['result_state'], action)
        node['children'].append({'action': action, 'result_state': new_state, 'children': []})
Sample Model

This program builds a tree of possible decisions and outcomes up to 2 steps deep starting from 'start'. It shows how choices lead to new states.

Agentic AI
def possible_actions(state):
    if state == 'start':
        return ['go left', 'go right']
    elif state == 'left room':
        return ['open door', 'look around']
    elif state == 'right room':
        return ['climb ladder', 'search drawer']
    else:
        return []

def apply_action(state, action):
    if state == 'start' and action == 'go left':
        return 'left room'
    if state == 'start' and action == 'go right':
        return 'right room'
    if state == 'left room' and action == 'open door':
        return 'secret chamber'
    if state == 'left room' and action == 'look around':
        return 'left room'
    if state == 'right room' and action == 'climb ladder':
        return 'attic'
    if state == 'right room' and action == 'search drawer':
        return 'found key'
    return state

def build_tree(node, depth):
    if depth == 0:
        return
    for action in possible_actions(node['current_state']):
        new_state = apply_action(node['current_state'], action)
        child_node = {'current_state': new_state, 'action': action, 'children': []}
        node['children'].append(child_node)
        build_tree(child_node, depth - 1)

tree = {'current_state': 'start', 'children': []}
build_tree(tree, 2)

import json
print(json.dumps(tree, indent=2))
OutputSuccess
Important Notes

Tree-of-thought helps visualize all possible paths before choosing.

It works well when decisions depend on previous steps.

Depth controls how far ahead you think.

Summary

Tree-of-thought breaks complex decisions into smaller steps.

It builds a tree where each branch is a possible action and result.

This helps explore options and find better solutions.

Practice

(1/5)
1.

What is the main purpose of using a tree-of-thought approach in complex decisions?

easy
A. To avoid making any decision
B. To randomly select an action without analysis
C. To break down decisions into smaller, manageable steps
D. To speed up decisions by ignoring options

Solution

  1. Step 1: Understand the concept of tree-of-thought

    Tree-of-thought breaks complex decisions into smaller steps to simplify the process.
  2. Step 2: Identify the purpose of breaking down decisions

    This helps explore options carefully and find better solutions.
  3. Final Answer:

    To break down decisions into smaller, manageable steps -> Option C
  4. Quick Check:

    Tree-of-thought = smaller steps [OK]
Hint: Think of breaking big problems into small parts [OK]
Common Mistakes:
  • Confusing tree-of-thought with random choice
  • Thinking it avoids decisions
  • Assuming it speeds up by ignoring options
2.

Which of the following correctly represents a step in building a tree-of-thought?

1. Start with initial state
2. Generate possible actions
3. Evaluate outcomes
4. Choose best path
easy
A. Choose best path, generate actions, start with initial state, evaluate outcomes
B. Start with initial state, generate actions, evaluate outcomes, choose best path
C. Evaluate outcomes, choose best path, start with initial state, generate actions
D. Generate actions, start with initial state, choose best path, evaluate outcomes

Solution

  1. Step 1: Identify the logical order of steps

    We start from the initial state, then generate possible actions.
  2. Step 2: Follow with evaluation and choice

    After generating actions, we evaluate outcomes and choose the best path.
  3. Final Answer:

    Start with initial state, generate actions, evaluate outcomes, choose best path -> Option B
  4. Quick Check:

    Logical step order = B [OK]
Hint: Follow the natural decision flow order [OK]
Common Mistakes:
  • Mixing up the order of steps
  • Starting with choice before generating actions
  • Evaluating before generating actions
3.

Given the following simplified tree-of-thought code snippet, what is the printed output?

def tree_of_thought(state, depth):
    if depth == 0:
        return [state]
    results = []
    for action in ['A', 'B']:
        next_state = state + action
        results.extend(tree_of_thought(next_state, depth - 1))
    return results

print(tree_of_thought('', 2))
medium
A. ['AA', 'AB', 'BA', 'BB']
B. ['A', 'B']
C. ['', 'A', 'B']
D. ['AA', 'AB', 'BA']

Solution

  1. Step 1: Understand recursion and depth

    At depth 2, the function appends two actions at each step, building strings of length 2.
  2. Step 2: Trace the recursive calls

    Starting with '', actions 'A' and 'B' add to form 'A' and 'B', then again add 'A' or 'B' to form 'AA', 'AB', 'BA', 'BB'.
  3. Final Answer:

    ['AA', 'AB', 'BA', 'BB'] -> Option A
  4. Quick Check:

    All 2-length action combos = C [OK]
Hint: Count depth levels and action combinations [OK]
Common Mistakes:
  • Confusing depth with number of actions
  • Returning partial strings
  • Missing recursive expansion
4.

Identify the error in this tree-of-thought function that prevents it from exploring all branches:

def tree_of_thought(state, depth):
    if depth == 0:
        return [state]
    results = []
    for action in ['A', 'B']:
        next_state = state + action
        results.append(tree_of_thought(next_state, depth - 1))
    return results

print(tree_of_thought('', 2))
medium
A. Using append instead of extend causes nested lists
B. Missing base case for depth == 0
C. Looping over wrong actions
D. Returning results before recursion

Solution

  1. Step 1: Analyze list operations in recursion

    Using append adds the entire recursive list as a single element, creating nested lists.
  2. Step 2: Correct method to flatten results

    Using extend adds elements individually, flattening the list as intended.
  3. Final Answer:

    Using append instead of extend causes nested lists -> Option A
  4. Quick Check:

    append vs extend affects list shape [OK]
Hint: Use extend to flatten recursive results [OK]
Common Mistakes:
  • Confusing append and extend
  • Ignoring base case presence
  • Misunderstanding recursion flow
5.

You want to use tree-of-thought to decide the best sequence of moves in a game where each move has a score. Which approach best fits this goal?

def tree_of_thought(state, depth):
    if depth == 0:
        return [(state, score(state))]
    results = []
    for action in possible_actions(state):
        next_state = apply_action(state, action)
        results.extend(tree_of_thought(next_state, depth - 1))
    return results

# How to choose best sequence?
hard
A. Stop recursion early without exploring all sequences
B. Pick the first sequence returned without comparing scores
C. Ignore scores and choose randomly
D. After collecting all sequences and scores, select the sequence with the highest score

Solution

  1. Step 1: Understand the goal of maximizing score

    The goal is to find the sequence with the highest score after exploring all options.
  2. Step 2: Choose the best sequence after full exploration

    Collecting all sequences and their scores allows selecting the best one reliably.
  3. Final Answer:

    After collecting all sequences and scores, select the sequence with the highest score -> Option D
  4. Quick Check:

    Best score selection after exploration = A [OK]
Hint: Explore all, then pick highest score [OK]
Common Mistakes:
  • Choosing first sequence without comparison
  • Ignoring scores
  • Stopping early and missing better options