Skip to main content

Tree of Thoughts

Deliberate Problem Solving with Large Language Models

中文文档 | English

Architecture • Installation • Quick Start • API Reference


A Python implementation of Tree of Thoughts (ToT), a framework that frames problem solving as a search over a tree of thoughts.

Architecture

Tree of Thoughts Architecture
│
├── ToTAgent (Main Agent)
│   ├── Orchestrates the search process
│   ├── Uses thought generators and evaluators
│   └── Applies search strategies
│
├── ThoughtTree (Tree Structure)
│   ├── ThoughtNode - Individual reasoning steps
│   ├── Path tracking from root to leaf
│   └── Pruning and visualization
│
├── SearchStrategy (Traversal Methods)
│   ├── BFSStrategy - Breadth-First Search
│   ├── DFSStrategy - Depth-First Search
│   ├── BestFirstStrategy - Value-based prioritization
│   └── BeamSearchStrategy - Memory-efficient search
│
└── Types (Data Structures)
    ├── Problem - Task to solve
    ├── Thought - Single reasoning step
    ├── Solution - Final answer with reasoning
    └── SearchResult - Search statistics

Core Workflow

┌─────────────────────────────────────────────────────────────────┐
│                    Tree of Thoughts Process                      │
├─────────────────────────────────────────────────────────────────┤
│                                                                  │
│   Problem ──▶ Generate Thoughts ──▶ Evaluate ──▶ Select Best    │
│       │              │                │              │          │
│       │              └────────────────┴──────────────┘          │
│       │                       │                                  │
│       │                       ▼                                  │
│       │              Check Solution? ──▶ Yes ──▶ Return Answer   │
│       │                       │                                  │
│       │                       No                                 │
│       │                       │                                  │
│       └───────────────────────┴──▶ Expand Tree ──▶ Repeat       │
│                                                                  │
└─────────────────────────────────────────────────────────────────┘

Installation

pip install tree-of-thoughts

Quick Start

from tot import ToTAgent, Problem, BestFirstStrategy

# Define thought generator (typically uses an LLM)
def generate_thoughts(state: str, n: int) -> list:
    # Generate n possible next thoughts
    # response = llm.generate(f"Generate {n} next thoughts: {state}")
    return ["Thought 1", "Thought 2", "Thought 3"][:n]

# Define thought evaluator
def evaluate_thought(state: str, thought: str) -> float:
    # Return value between 0-1 (higher is better)
    # response = llm.generate(f"Rate this thought: {thought}")
    return 0.8

# Define solution checker
def check_solution(state: str, thought: str) -> tuple:
    # Return (is_solution, answer)
    # Check if we've reached a valid solution
    return (False, "")  # or (True, "final answer")

# Create and run agent
agent = ToTAgent(
    generate_thoughts,
    evaluate_thought,
    check_solution,
    strategy=BestFirstStrategy(),
    max_depth=5,
)

result = agent.solve(Problem(question="Solve this problem"))
print(f"Success: {result.success}")
print(f"Answer: {result.solution.answer if result.solution else 'Not found'}")

Search Strategies

Strategy Description Best For
BFS Explores all nodes at depth before going deeper Shortest solution path
DFS Explores as deep as possible before backtracking Quick any solution
Best-First Prioritizes highest-value nodes Quality solutions
Beam Search Keeps only top-k candidates per level Memory efficiency

CLI Usage

# Run with different strategies
tot run "What is 15 * 17?" --strategy best
tot run "Solve this puzzle" --strategy bfs

# Interactive mode
tot interactive --strategy dfs

# Export trajectory
tot run "Question" --export result.json

API Reference

ToTAgent

Parameter Description
generate_thoughts Function to generate next thoughts
evaluate_thought Function to score thoughts (0-1)
check_solution Function to check if solved
strategy Search strategy (default: BestFirst)
max_depth Maximum tree depth
max_nodes Maximum nodes to explore
branching_factor Thoughts per node

Academic Reference

Implementation of the Tree of Thoughts paper:

Tree of Thoughts: Deliberate Problem Solving with Large Language Models

Shunyu Yao, Dian Yu, Jeffrey Zhao, Izhak Shafran, Thomas L. Griffiths, Yuan Cao, Karthik R. Narasimhan

NeurIPS 2023

Paper: https://arxiv.org/abs/2305.10601

@inproceedings{yao2023tree,
  title={Tree of Thoughts: Deliberate Problem Solving with Large Language Models},
  author={Yao, Shunyu and Yu, Dian and Zhao, Jeffrey and Shafran, Izhak and Griffiths, Thomas L and Cao, Yuan and Narasimhan, Karthik R},
  booktitle={Advances in Neural Information Processing Systems (NeurIPS)},
  year={2023}
}

License

MIT License


Made with ❤️ by AI Agent Research Team

Metadata

Release files for tree-of-thoughts-ai 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tree-of-thoughts-ai 0.1.0
File Size Uploaded Provenance Actions
tree_of_thoughts_ai-0.1.0.tar.gz 15.9 kB Without provenance Details for tree_of_thoughts_ai-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for tree-of-thoughts-ai 0.1.0
File Interpreter ABI Platform Actions
tree_of_thoughts_ai-0.1.0-py3-none-any.whl Python 3 none any Details for tree_of_thoughts_ai-0.1.0-py3-none-any.whl

Total release size: 29.9 kB

Release files / tree_of_thoughts_ai-0.1.0.tar.gz

Download URL tree_of_thoughts_ai-0.1.0.tar.gz
Size 15.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b9ccee6ad9771f29524fd9c36d0546d0cfc156da405c127c4d5efede25ef6822
BLAKE2b-256 checksum
How to use checksums
7a3d2f40388bd9ed88f1d2768483c65761ca7270b28d47bcdb483708430ea028
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Release files / tree_of_thoughts_ai-0.1.0-py3-none-any.whl

Download URL tree_of_thoughts_ai-0.1.0-py3-none-any.whl
Size 14.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
97bd7978ba5aaa065f2fdf19be50b043eec0a116a42ab35f776ead5422c54e58
BLAKE2b-256 checksum
How to use checksums
6d025f045d28237c4f3fc114ec995228ff0fe60d8a8fd8dfd03b325a7fb4abef
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.12

Security for tree-of-thoughts-ai 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page