Lesson 4 of 5 · Planning
Search-Based Planning
Let AI agents explore several plans before committing: greedy, beam and full search, proposers and evaluators, Tree of Thoughts and best-of-N with a verifier.
- 9 min read
- Advanced
- Reviewed
- Explain why committing to the first idea can produce poor plans
- Describe the parts of a search: proposer, evaluator, strategy and budget
- Compare greedy, beam and full search on cost and quality
- Relate this to Tree of Thoughts and best-of-N sampling
- Keep exploration free of side effects
The first idea isn't always the best
A ReAct agent picks one next step, takes it, and moves on. That's greedy: it always takes the step that looks best right now. Often that's fine. But some problems punish early choices that look good, like a cheap first flight that leads to an expensive connection.
Every search has four parts:
- A proposer that suggests possible next steps from a partial plan (often an LLM).
- An evaluator that scores how promising a partial plan is (code, a heuristic, or an LLM).
- A strategy that decides which plans to keep exploring: greedy, beam, best-first, or full search.
- A budget: how many plans you can afford to generate and score.
Greedy, beam and full search
The task: travel from Lisbon to Berlin as cheaply as possible. Here are all the legs on offer:
Now the same search code runs with four settings:
# Search-based planning: explore several partial plans instead of committing to the first idea.
# Task: get from Lisbon to Berlin as cheaply as possible (prices in euros).
LEGS = {
"Lisbon": {"Porto": 25, "Seville": 35, "Madrid": 40, "London": 70, "Paris": 90},
"Porto": {"Madrid": 45, "London": 95, "Paris": 110},
"Seville": {"Madrid": 30, "Rome": 80},
"Madrid": {"Rome": 60, "Zurich": 120, "Berlin": 260},
"Rome": {"Zurich": 70, "Berlin": 90},
"Zurich": {"Berlin": 150},
"London": {"Amsterdam": 60, "Berlin": 80},
"Paris": {"Amsterdam": 40, "Berlin": 50},
"Amsterdam": {"Berlin": 70},
}
def propose(plan):
"""Possible next steps from the end of a partial plan (an LLM would suggest these)."""
route, cost = plan
return [(route + [city], cost + price) for city, price in LEGS.get(route[-1], {}).items()]
def cost_so_far(plan):
return plan[1] # lower is better
# A rough guess of what is still left to pay from each city (an LLM evaluator might judge this)
GUESS = {"Porto": 120, "Seville": 150, "Madrid": 140, "Rome": 90, "Zurich": 100,
"London": 80, "Paris": 50, "Amsterdam": 70, "Berlin": 0}
def cost_plus_guess(plan):
return plan[1] + GUESS[plan[0][-1]] # how good the WHOLE trip is likely to be
def beam_search(start, goal, width, score):
frontier, finished, evaluated = [([start], 0)], [], 0
while frontier:
candidates = [p for plan in frontier for p in propose(plan)]
evaluated += len(candidates)
finished += [p for p in candidates if p[0][-1] == goal]
candidates = sorted((p for p in candidates if p[0][-1] != goal), key=score)
frontier = candidates[:width] # keep only the most promising partial plans
best = min(finished, key=lambda p: p[1])
return " -> ".join(best[0]), best[1], evaluated
for name, width, score in [("Greedy, width 1", 1, cost_so_far),
("Beam, width 3", 3, cost_so_far),
("Beam, width 3 + guess", 3, cost_plus_guess),
("Full search", 100, cost_so_far)]:
route, cost, evaluated = beam_search("Lisbon", "Berlin", width, score)
print(f"{name:<22} {route:<44} {cost:>3} EUR ({evaluated} plans scored)")Look at what each setting got, and what it cost:
- Greedy grabbed the cheapest first leg, to Porto, and ended up at 220 euros.
- Beam, width 3, kept three options open, but scoring by cost so far threw away Paris at the first step, because 90 euros looked expensive. It found 190.
- Beam, width 3 + guess scored each partial plan by its cost so far plus a guess of what's left to pay. Paris now looked promising, and the search found the best route, 140, after scoring only 19 plans.
- Full search also found 140, but scored 46 plans.
The lesson: a better evaluator often matters more than a wider search. A good estimate of how a plan will turn out lets a small search find what a big one would.
Tree of Thoughts and best-of-N
On the “Game of 24” puzzle (combine four numbers with arithmetic to make 24), the Tree of Thoughts paper reported that GPT-4 with chain-of-thought prompting solved 4% of games, while Tree of Thoughts solved 74%. Later work, such as Language Agent Tree Search (Zhou et al., 2023), applied tree search to agents that take actions.
The simplest search of all is best-of-N: generate N complete answers, check each with a verifier, and keep the best. It needs no partial-plan scoring, works with any model, and is easy to run in parallel. A related idea, self-consistency (Wang et al., 2022), samples several reasoning paths and takes the majority answer.
Explore in your head, not in the world
Search means trying many options and throwing most away. That's only safe if trying an option has no side effects. You can score 46 possible routes; you can't book 46 flights and cancel 45.
Common mistakes
Write one step of beam search: from the partial plans an LLM proposed, keep only the most promising few for the next round.
# Partial plans an LLM proposed, with scores from an evaluator (higher = more promising)
candidates = {"search flights first": 0.62, "ask about budget": 0.81, "book hotel now": 0.15,
"check visa rules": 0.74, "pick dates": 0.55}
width = 3
# Keep the width best candidates (highest score first) and print one per line,
# in the form: <score> <plan>
In the demo, why did beam search with width 3 miss the best route when it scored plans by cost so far?
Summary
- Greedy agents commit to the step that looks best now; some problems punish that.
- A search has a proposer, an evaluator, a strategy and a budget.
- Beam search keeps the best few partial plans; a better evaluator often beats a wider beam.
- Tree of Thoughts applies search to reasoning steps; best-of-N with a verifier is the simplest search.
- Explore plans and read-only lookups, never actions with side effects.
Next: when plans fail, retrying, replanning and asking for help.
Finished reading? Tick it off to track your progress.