PromptABCD
FeaturesLearnHow it worksUse casesFAQGuideBlogContext Blocks
Sign inGet started free
Sign inSign up
PromptABCD

A calm home for your best AI prompts. Save them once, find them in seconds, reuse them forever.

Product

  • Features
  • Chrome Extension
  • Free Courses
  • How it works
  • Use cases
  • Blog
  • Context Blocks
  • Export Anywhere
  • FAQ

Resources

  • User guide
  • Learn prompting
  • Sign in
  • Get started free

© 2026 PromptABCD. All rights reserved.

Privacy PolicyTerms and Conditions
Home/Blog/Agent Loop Engineering/Tree of Thoughts Inside the Agent Loop
Agent Loop Engineering

Tree of Thoughts Inside the Agent Loop

A tree of thoughts agent explores several reasoning paths and keeps the best, instead of committing to the first. Here's a real case where branching turned a stuck agent into a solver.

August 22, 2026·8 min read
ShareShare
⚡Featured Prompt— copy and use right now
Think very carefully and thoroughly about the best strategy
before you begin, then execute it completely.

Picture this: you're a data scientist, and your agent keeps failing a scheduling optimization the same way. It commits to an approach in its first reasoning step, follows that approach to a dead end, and — because a plain loop only ever holds one line of thought — has no way to back up and try a different opening. It doesn't fail because it's not smart enough. It fails because it can only think in a straight line. A tree of thoughts agent fixes exactly this: instead of one path, it explores several, evaluates them, and keeps the promising ones.

Tree of thoughts turns reasoning into search. At each decision point the agent generates several candidate next steps, scores them, and expands the best — pruning the dead ends instead of being trapped by them. This is the story of a stuck agent that started solving once it could branch.

The shift is less exotic than it sounds. A plain loop is depth-first with no backtracking — pick a move, commit, never reconsider. Tree of thoughts adds the two things that turn blind commitment into search: alternatives (more than one move considered) and evaluation (a way to tell them apart). Everything else is bookkeeping. Once you see it as "the loop, plus a beam of alternatives, plus a scorer," it stops being a research paper and becomes a modest change to code you already run.

The Problem the Team Faced

The team's agent assigned shifts to staff under a tangle of constraints — availability, skills, labor rules, fairness. Framed as a single-path reasoning problem, the agent would pick a strategy early ("fill the hardest-to-staff shifts first"), commit to it, and when that strategy painted it into a corner near the end, it had no mechanism to reconsider the opening move. It could only push forward from a bad start.

Success hovered around fifty percent — fine when the easy strategy happened to work, hopeless when the problem needed a different initial approach. And because the agent committed so early, the fix wasn't a better prompt for the single path. It was the ability to hold several paths at once.

The Wrong Approach

The team first tried more reasoning per step — longer chains of thought, more deliberation before each move.

Think very carefully and thoroughly about the best strategy
before you begin, then execute it completely.

What this does: asks for deeper single-path reasoning — which produces a more elaborate first guess but still commits to one path, so a wrong opening is now wrong in more detail.

It didn't help, and the reason is instructive: more thinking along one path doesn't rescue you from the wrong path. The agent reasoned itself more confidently into the same dead ends. Depth on a single branch can't substitute for exploring multiple branches, because the failure was never insufficient depth — it was premature commitment.

There's a broader lesson in that. Reasoning has two dimensions people routinely conflate: how deeply you think along a path, and how many paths you consider. Chain-of-thought buys depth. Tree of thoughts buys breadth. They solve different failures, and pouring more of one into a problem that needs the other is the single most common wasted effort in agent reasoning. When you're stuck, first ask which dimension you're actually short on before you spend tokens on the other.

⚠️ Common mistake: Treating a premature-commitment failure as a not-enough-reasoning failure. When an agent fails because it locked onto a bad approach early, more chain-of-thought on that approach makes it more committed, not less. The fix is breadth — trying several openings — not depth on the one it already chose.

The Correct Prompt

The fix was to branch. At each key decision, generate several candidate approaches, score each, keep the best few, and expand those — a small search over reasoning paths.

At each decision point:
1. Propose 3 distinct next moves (genuinely different, not variants).
2. For each, briefly predict where it leads and rate 0-10.
3. Keep the top 2. Discard the rest.
4. Expand each kept move. Repeat until one path solves it
   or all paths are exhausted.

What this does: converts single-path reasoning into a breadth-first search over ideas — proposing genuinely different moves, scoring them, and keeping only the promising branches so the agent can abandon a bad opening instead of riding it to failure.

hljs python
[object Object], ,[object Object],(,[object Object],):
    frontier = [state]
    ,[object Object], _ ,[object Object], ,[object Object],(depth):
        candidates = []
        ,[object Object], s ,[object Object], frontier:
            ,[object Object], move ,[object Object], propose(s, n=,[object Object],):
                candidates.append((score(s, move), advance(s, move)))
        candidates.sort(reverse=,[object Object],)
        frontier = [s ,[object Object], _, s ,[object Object], candidates[:beam]]   ,[object Object],
        ,[object Object], s ,[object Object], frontier:
            ,[object Object], s.solved:
                ,[object Object], s
    ,[object Object], best(frontier)

What this does: maintains a small beam of the most promising reasoning states, expands each with several proposed moves, scores and prunes to the top few, and repeats — searching the space of approaches instead of committing to the first one.

Results and What Changed

Success on the scheduling task climbed from about fifty percent to the mid-eighties. The agent stopped getting trapped by its opening move, because a bad opening was now just one low-scored branch among several, pruned in favor of a better one. Breadth bought it the ability to change its mind before committing.

The cost, honestly, went up — a tree of thoughts agent makes several proposals and scores at each level, so it spends more tokens than a single path. But for this task the trade was clearly worth it: a fifty-percent agent that's cheap is more expensive than an eighty-five-percent agent that costs more, once you count the failed runs and the human cleanup the cheap one generated.

That accounting is worth doing explicitly, because raw token cost makes tree search look bad in isolation. The honest comparison isn't tokens-per-run; it's cost-per-success. A cheap agent that fails half the time pays its token cost twice — once for the failed run, once for the retry — plus a human's time to notice and redo the failure. Priced per success, the branching agent was actually cheaper here, which is the calculation teams miss when they reject tree of thoughts on sticker price.

⚡ Pro tip: Keep the beam narrow — two or three kept branches is usually plenty. The cost of tree search grows fast with beam width, and most of the benefit comes from having any alternative to a bad path, not from exploring dozens. Narrow beam, few levels, big gain.

How to Apply This to Your Situation

Reach for tree of thoughts when the failure signature is premature commitment: the agent picks an approach early and can't recover when it's wrong. Puzzles, planning, optimization, and multi-step math are the classic fits — anywhere the first move disproportionately determines success.

Skip it when a single path already works, or when you can't score partial progress. The whole mechanism depends on rating branches, so if you can't tell a promising partial approach from a doomed one, you can't prune, and tree search collapses into expensive random exploration.

⚡ Pro tip: Demand that proposed branches be genuinely different, not paraphrases. Models love to offer three near-identical openings dressed in different words, which gives you the cost of branching with none of the benefit. Prompt for approaches that differ in strategy — "each must start from a different first move" — and check they actually do.

⚡ Pro tip: Cache scored branches you prune. A path that looked weak at depth two sometimes turns out to be the best remaining option once better-looking branches hit dead ends. Keeping pruned branches retrievable lets you revive one instead of re-deriving it, which softens the cost of a narrow beam.

⚡ Pro tip: Your scoring function matters more than your branching. A tree of thoughts agent with a sharp evaluator and a narrow beam beats one with a wide beam and a fuzzy evaluator every time. Invest in how you rate a partial path before you invest in exploring more of them.

Next Steps

Take one task your agent fails by committing early, and add a single layer of branching: propose three openings, score them, keep the best, and expand only that. Even one level of look-ahead often lifts success noticeably, and it's a small change to a loop you already have. Start with one branch point at the opening move — where premature commitment does the most damage — and only add deeper branching if that first layer proves it's worth the tokens. Most of the lift in that scheduling case came from the very first fork; the deeper levels added polish, not the breakthrough.

The propose/score/prune skeleton is reusable across every branching agent you build. I keep it saved and versioned in PromptABCD with the scoring-prompt language, so a new tree of thoughts agent starts from a search loop that already worked — instead of a single-path loop that reasons itself confidently into the same corner every time.

tree of thoughtsagent loopreasoningsearchai agentscase study

Continue Reading

How to Summarize History Mid-Loop Without Losing State
Agent Loop Engineering

How to Summarize History Mid-Loop Without Losing State

An agent summarized its own history mid-run and forgot it had already booked the flight — then booked it again. Good agent loop history summarization keeps state intact. Here's how.

August 22, 2026·8 min read
Context Compaction Between Agent Turns
Agent Loop Engineering

Context Compaction Between Agent Turns

Most advice on agent context compaction is backwards: it compresses on a timer and loses the wrong things. Here's how to compact by relevance, keep what matters, and do it safely.

August 22, 2026·8 min read
Managing the Context Window Across Loop Iterations
Agent Loop Engineering

Managing the Context Window Across Loop Iterations

Why does your agent get slower and dumber the longer it runs? The agent loop context window is filling with junk. Here's a bloated loop, why it degrades, and how to keep context lean.

August 22, 2026·8 min read

Save the prompts from this post

PromptABCD is a free prompt manager. Paste, organize, and reuse your best AI prompts — no more hunting through chat history.

Start free →
← PreviousHow to Add a Critic Step to Your Agent LoopNext →Managing the Context Window Across Loop Iterations
Share this post:
ShareShare