{"skill":{"slug":"tree-of-thoughts","displayName":"Tree Of Thoughts","summary":"Multi-path reasoning for complex problems. Explore multiple solution branches → Evaluate each → Select optimal path. Use for: difficult decisions, creative p...","description":"---\nname: tree-of-thoughts\nversion: \"2.0.0\"\ndescription: \"Multi-path reasoning for complex problems. Explore multiple solution branches → Evaluate each → Select optimal path. Use for: difficult decisions, creative problems, ambiguous situations, optimization challenges.\\n\"\nmetadata:\n  openclaw:\n    emoji: \"🌳\"\n    os: [\"darwin\", \"linux\", \"win32\"]\n---\n\n# Tree of Thoughts (ToT) Reasoning (Enhanced v2.0.0)\n\n**v2.0.0 Enhancement:** Parallel execution + intelligent caching (FoT pattern)\n**Speed Improvement:** 3-5x faster for complex problems\n**Cache Benefit:** 50-200x faster for similar cached problems\n\nAdvanced reasoning through systematic exploration of solution spaces.\n\n## What is Tree of Thoughts?\n\n**Traditional reasoning** (Chain of Thought):\n```\nProblem → Step 1 → Step 2 → Step 3 → Solution\n(Single linear path)\n```\n\n**Tree of Thoughts**:\n```\n                    Problem\n                   /   |   \\\n              Path A Path B Path C\n               /  \\    |    /  \\\n            A1   A2   B1  C1   C2\n             |    |    |   |    |\n          [eval] [eval] ... [eval]\n             \\    |    |   /    /\n              \\   |    |  /    /\n               Best Solution\n```\n\n**Key differences**:\n- Explores MULTIPLE paths\n- Evaluates EACH branch\n- Can BACKTRACK from dead ends\n- SELECTS optimal solution\n- More robust than linear thinking\n\n## When to Use ToT\n\n**Use ToT for**:\n- Multiple possible solutions exist\n- Problem is ambiguous or complex\n- Need to compare approaches\n- High cost of failure\n- Creative/optimization problems\n- Uncertain which method works\n\n**Skip ToT for**:\n- Simple, clear problems\n- Single obvious solution\n- Time-critical decisions\n- Routine operations\n- Well-known procedures\n\n## Parallel Execution + Caching (v2.0.0)\n\n### Performance Improvements\n\n| Scenario | Before | After | Speedup |\n|----------|--------|-------|---------|\n| 3-branch exploration | 3.0s | 1.0s | 3x |\n| 5-branch exploration | 5.0s | 1.2s | 4.2x |\n| Deep tree (depth 4) | 8.0s | 2.0s | 4x |\n| Similar cached problem | 5.0s | 0.025s | 200x |\n\n### Parallel Tree Generation\n\n```python\nasync def parallel_tree_of_thoughts(problem, branches=5, depth=3):\n    \"\"\"\n    Generate and evaluate thought tree in parallel.\n    \n    Benefits:\n    - 3-5x faster than sequential\n    - All branches generated simultaneously\n    - Evaluation parallelized\n    \"\"\"\n    # Step 1: Generate all initial thoughts in parallel\n    initial_thoughts = await asyncio.gather(*[\n        generate_thought_async(problem) for _ in range(branches)\n    ])\n    \n    # Step 2: Evaluate all thoughts in parallel\n    evaluations = await asyncio.gather(*[\n        evaluate_thought_async(thought) for thought in initial_thoughts\n    ])\n    \n    # Step 3: Select top thoughts for expansion\n    top_thoughts = select_top_k(initial_thoughts, evaluations, k=3)\n    \n    # Step 4: Expand in parallel\n    expanded = await asyncio.gather(*[\n        expand_thought_async(thought, depth-1) for thought in top_thoughts\n    ])\n    \n    return select_best_solution(expanded)\n```\n\n### Intelligent Caching\n\n```python\ndef cached_tree_of_thoughts(problem, cache_ttl_hours=24):\n    \"\"\"\n    ToT with semantic caching for similar problems.\n    \n    Cache hits when:\n    - Same problem repeated (exact match)\n    - Similar problem (>85% semantic similarity)\n    - Related problem type (same domain)\n    \"\"\"\n    cache_key = semantic_hash(problem)\n    cached = cache_get(cache_key)\n    \n    if cached and semantic_similarity(problem, cached['problem']) > 0.85:\n        return {\n            \"solution\": cached['solution'],\n            \"from_cache\": True,\n            \"cache_age\": (now - cached['timestamp']).minutes\n        }\n    \n    # Generate fresh solution\n    result = parallel_tree_of_thoughts(problem)\n    \n    # Cache for future\n    cache_set(cache_key, {\n        'problem': problem,\n        'solution': result,\n        'timestamp': now()\n    })\n    \n    return {**result, \"from_cache\": False}\n```\n\n### CLI Flags\n\n```\n--parallel         Use parallel execution (default)\n--cached           Enable caching (default)\n--sequential       Disable parallel execution\n--no-cache         Disable caching\n--branches N       Set number of branches (default: 5)\n--depth N          Set tree depth (default: 3)\n```\n\n---\n\n## ToT Process\n\n### Step 1: GENERATE Thoughts\n```\nGiven problem, generate multiple initial approaches:\n- Thought A: [Approach 1]\n- Thought B: [Approach 2]\n- Thought C: [Approach 3]\n```\n\n### Step 2: EVALUATE Thoughts\n```\nFor each thought, assess:\n- Feasibility: Can this work?\n- Quality: How good would result be?\n- Cost: Time/resources needed?\n- Risk: What could fail?\n```\n\n### Step 3: EXPAND Promising Thoughts\n```\nTake best thoughts and expand:\n- Thought A → A1, A2, A3\n- Thought B → B1, B2\n(Expand only promising branches)\n```\n\n### Step 4: EVALUATE Branches\n```\nEvaluate each expanded branch:\n- A1: [score/10]\n- A2: [score/10]\n- B1: [score/10]\n```\n\n### Step 5: SEARCH for Solution\n```\nStrategies:\n- BFS: Explore all branches breadth-first\n- DFS: Dive deep into promising branches\n- Best-First: Always expand highest-rated\n- Beam Search: Keep top-K branches\n```\n\n### Step 6: SELECT Optimal Path\n```\nChoose path with best expected outcome:\n- Highest evaluation score\n- Most feasible\n- Best cost/benefit ratio\n```\n\n## Thought Evaluation\n\n### Evaluation Criteria\n\n**Feasibility** (0-10):\n```\n- 10: Definitely possible\n- 7-9: Likely possible\n- 4-6: Maybe possible\n- 1-3: Unlikely to work\n- 0: Impossible\n```\n\n**Quality** (0-10):\n```\n- 10: Perfect solution\n- 7-9: Great solution\n- 4-6: Adequate solution\n- 1-3: Poor solution\n- 0: Doesn't solve problem\n```\n\n**Cost** (0-10, inverse):\n```\n- 10: Negligible cost\n- 7-9: Low cost\n- 4-6: Moderate cost\n- 1-3: High cost\n- 0: Prohibitively expensive\n```\n\n**Risk** (0-10, inverse):\n```\n- 10: No risk\n- 7-9: Low risk\n- 4-6: Moderate risk\n- 1-3: High risk\n- 0: Certain failure\n```\n\n### Scoring Formula\n\n```\nScore = (Feasibility * 0.3) + (Quality * 0.3) + (Cost * 0.2) + (Risk * 0.2)\n```\n\nAdjust weights based on priorities:\n- Quality-focused: `Quality * 0.5`\n- Speed-focused: `Cost * 0.5`\n- Safety-focused: `Risk * 0.5`\n\n## Search Strategies\n\n### Breadth-First Search (BFS)\n```\nLevel 1: Explore all initial thoughts\nLevel 2: Expand all promising thoughts\nLevel 3: Continue breadth-wise\nGood for: Comprehensive exploration\n```\n\n### Depth-First Search (DFS)\n```\nLevel 1: Pick most promising thought\nLevel 2: Dive deep into that branch\nLevel 3: Continue depth-wise\nGood for: Quick deep solutions\n```\n\n### Best-First Search\n```\nAlways expand the highest-rated node\nUse priority queue\nGood for: Finding optimal quickly\n```\n\n### Beam Search\n```\nKeep only top-K branches at each level\nPrune low-rated branches early\nGood for: Efficiency with quality\n```\n\n## ToT Templates\n\n### Decision Problem\n```markdown\n## Problem: [Decision to make]\n\n### Initial Thoughts\n1. **Option A**: [Description]\n   - Feasibility: 8/10\n   - Quality: 7/10\n   - Cost: 9/10\n   - Risk: 8/10\n   - **Score**: 7.9/10\n\n2. **Option B**: [Description]\n   - Feasibility: 9/10\n   - Quality: 6/10\n   - Cost: 7/10\n   - Risk: 6/10\n   - **Score**: 7.1/10\n\n3. **Option C**: [Description]\n   - Feasibility: 6/10\n   - Quality: 9/10\n   - Cost: 5/10\n   - Risk: 4/10\n   - **Score**: 6.3/10\n\n### Expansion (Top 2)\n**Option A** → A1: [Refinement] (Score: 8.5/10)\n**Option B** → B1: [Refinement] (Score: 7.8/10)\n\n### Selected Path: A1\nReason: Highest score, good balance of feasibility and quality\n```\n\n### Creative Problem\n```markdown\n## Problem: [Creative challenge]\n\n### Thought Branches\n1. **Creative A**: [Idea]\n   - Novelty: 9/10\n   - Feasibility: 5/10\n   - Impact: 8/10\n   - **Score**: 7.4/10\n\n2. **Creative B**: [Idea]\n   - Novelty: 7/10\n   - Feasibility: 8/10\n   - Impact: 7/10\n   - **Score**: 7.3/10\n\n3. **Safe C**: [Conservative idea]\n   - Novelty: 4/10\n   - Feasibility: 9/10\n   - Impact: 6/10\n   - **Score**: 6.3/10\n\n### Hybrid Approach: A + B\nCombine novelty of A with feasibility of B\nScore: 8.2/10\n```\n\n## Integration Patterns\n\n### ToT + Task Decomposition\n```\n1. Use ToT to choose decomposition strategy\n2. Compare different breakdown approaches\n3. Select optimal decomposition\n```\n\n### ToT + Error Recovery\n```\n1. When error occurs\n2. Generate multiple recovery options via ToT\n3. Evaluate each recovery path\n4. Select best recovery strategy\n```\n\n### ToT + Self-Reflection\n```\n1. After completing task\n2. Reflect: Did I consider enough options?\n3. Should I have used ToT?\n4. Was my evaluation accurate?\n```\n\n## Backtracking\n\nWhen a path fails:\n```\n1. Mark branch as dead end\n2. Record why it failed\n3. Backtrack to decision point\n4. Try next best alternative\n5. Learn from failure\n```\n\nExample:\n```\nPath A → A1 → A1a [FAILED: X didn't work]\nBacktrack to A\nPath A → A2 → A2a [SUCCESS]\n```\n\n## Pruning Strategies\n\n**Early pruning**:\n- Score < 3/10: Drop immediately\n- Infeasible: Drop immediately\n- High risk + low quality: Drop\n\n**Continuous pruning**:\n- After expansion, keep only top 50%\n- Remove redundant branches\n- Merge similar thoughts\n\n## Practical Examples\n\n### Example 1: Choose Architecture\n\n```markdown\n## Problem: Design system architecture\n\n### Thoughts\n1. Monolith: Simple, fast to build, hard to scale\n   Score: 6/10 (good for MVP, bad for scale)\n\n2. Microservices: Scalable, complex, slow to build\n   Score: 7/10 (good for scale, overkill now)\n\n3. Modular Monolith: Balanced, medium complexity\n   Score: 8/10 (best of both)\n\n### Expansion\nModular Monolith →\n  - M1: Start modular, split later (8.5/10)\n  - M2: Full modules from start (7/10)\n\n### Decision: M1\nStart with modular monolith, design for future split\n```\n\n### Example 2: Fix Performance Bug\n\n```markdown\n## Problem: API too slow\n\n### Thoughts\n1. Cache everything: Fast, memory-heavy, stale data risk\n   Score: 6/10\n\n2. Optimize queries: Moderate speedup, accurate data\n   Score: 8/10\n\n3. Add indexes: Quick win, limited impact\n   Score: 7/10\n\n4. Rewrite in faster language: High impact, high cost\n   Score: 5/10\n\n### Expansion\nOptimize + Indexes → Combined approach\nScore: 9/10 (synergy)\n\n### Decision: Optimize queries + Add strategic indexes\n```\n\n## ToT Reasoning Log\n\nTrack ToT sessions in: `memory/tot-sessions.md`\n\n```markdown\n## [Date] ToT Session: [Problem]\n\n### Options Considered: [N]\n### Paths Explored: [N]\n### Depth: [N levels]\n### Decision: [Chosen path]\n### Rationale: [Why this won]\n### Outcome: [Did it work?]\n### Lesson: [What was learned]\n```\n\n## Metrics\n\n- Average thoughts generated per problem\n- Search depth average\n- Backtrack rate\n- Decision quality (outcomes)\n- Time to decision\n- Solution diversity\n\n## Quick Actions\n\n- `tot [problem]` - Run ToT reasoning\n- `compare [options]` - Evaluate multiple options\n- `decide [decision]` - Make decision with ToT\n- `branches` - Show current ToT tree\n\n## Best Practices\n\n1. **Generate many thoughts initially** (5-10)\n2. **Evaluate objectively** (use criteria)\n3. **Prune aggressively** (don't explore poor options)\n4. **Expand gradually** (depth vs breadth balance)\n5. **Backtrack when stuck** (dead ends happen)\n6. **Document reasoning** (learn from decisions)\n7. **Review outcomes** (improve evaluation accuracy)\n\n---\n\n**Remember**: The best solution is rarely the first one you think of. Explore, evaluate, select.\n\n## Real Usage Example\n\n**Scenario**: Choosing the best system improvement to implement\n\n### Problem\n\"What ONE improvement should I make to the OpenClaw system today?\"\n\n### Initial Thoughts (Generated 5 options)\n\n| Option | Description | Feasibility | Quality | Cost | Risk | Score |\n|--------|-------------|-------------|---------|------|------|-------|\n| A | Auto-create log files | 9 | 7 | 9 | 9 | **8.3** |\n| B | Add quick-action triggers | 4 | 8 | 3 | 5 | **5.0** |\n| C | Add integration examples | 9 | 6 | 8 | 9 | **7.6** |\n| D | Create error pattern detection | 7 | 9 | 6 | 7 | **7.4** |\n| E | Create skill test runner | 6 | 9 | 5 | 6 | **6.5** |\n\n### Scoring Formula\n```\nScore = (Feasibility × 0.3) + (Quality × 0.3) + (Cost × 0.2) + (Risk × 0.2)\n```\n\n### Expansion (Top 2)\n- **A → A1**: Auto-create all log files AND error folder → Score: **8.5**\n- **D → D1**: Simple regex-based error classifier → Score: **7.8**\n\n### Decision: A1 (Auto-create log files)\n**Rationale**: Highest score, lowest risk, immediate usability improvement\n\n### Outcome\nCreated:\n- `memory/criticism-log.md`\n- `memory/tot-sessions.md`\n- `memory/errors/error-log.md`\n\n**Lesson**: Simple infrastructure improvements often beat complex features.\n","tags":{"latest":"2.0.0"},"stats":{"comments":0,"downloads":939,"installsAllTime":3,"installsCurrent":3,"stars":0,"versions":1},"createdAt":1772243589315,"updatedAt":1778993795679},"latestVersion":{"version":"2.0.0","createdAt":1772243589315,"changelog":"**Tree of Thoughts 2.0.0 – Parallel reasoning & caching for major speedups**\n\n- Added parallel execution of tree branches, enabling 3–5x faster problem-solving.\n- Introduced intelligent caching using semantic similarity for 50–200x speedups on similar or repeated problems.\n- New CLI flags for controlling parallelism, caching, branch count, and depth.\n- Enhanced documentation with templates for decision and creative problems, evaluation criteria, and search strategies.\n- Recommended for complex, ambiguous, or creative problem-solving—skip for simple or routine tasks.","license":null},"metadata":{"setup":[],"os":["darwin","linux","win32"],"systems":null},"owner":{"handle":"tobisamaa","userId":"s170q475724mp8v6gygfgsqhh9885z9z","displayName":"tobisamaa","image":"https://avatars.githubusercontent.com/u/62774845?v=4"},"moderation":null}