0
0
Agentic AIml~5 mins

Tree-of-thought for complex decisions in Agentic AI

Choose your learning style9 modes available
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.