Beam Search
What Is Beam Search?
Beam search is a heuristic algorithm that keeps a fixed number of promising candidate paths while generating an output. In AI, it lets a model consider several token or decision sequences instead of choosing one immediately. It is approximate, so it can produce strong results efficiently without guaranteeing the globally best result.
How Beam Search Works
The number of retained candidates is the beam width. A wider beam considers more alternatives but requires more time and memory.
- Start with an initial state, such as an empty sentence or workflow.
- Expand each retained path with possible next tokens or actions.
- Score new paths using cumulative probability, cost, or a heuristic.
- Keep only the top paths allowed by the beam width.
- Repeat until a goal or end-of-sequence token appears.
For sequence generation, scores are accumulated across the full candidate, not just its latest word. The Dive into Deep Learning explanation of beam search describes its role between greedy and exhaustive decoding.
Beam Search Example in AI Text Generation
For “Please reset your,” a greedy decoder may choose “password” as the most likely next word. Beam search can also retain “account” and “device,” then compare complete continuations. A less likely early choice can produce the stronger sentence.
Beam Search vs. Greedy Search vs. Sampling
These decoding methods trade off speed, consistency, and variety.
| Method | What it keeps | Best fit |
|---|---|---|
| Greedy search | One best next choice | Fast, deterministic output |
| Beam search | Several top partial paths | Structured, high-confidence sequences |
| Sampling | Randomized choices from likely options | Creative or varied responses |
Benefits of Beam Search
Beam search supports controlled output quality without exhaustive computation.
- It explores more alternatives than greedy decoding.
- Beam width provides a predictable compute limit.
- It is reproducible when scores and settings are fixed.
- It supports translation, speech recognition, code generation, and constrained workflows.
Limitations of Beam Search
More candidates improve coverage but do not remove all search errors.
- A promising path can be pruned before its advantage appears.
- It does not guarantee the best final answer.
- Wider beams add latency and memory use.
- Without length normalization, scoring can favor short outputs.
- Similar candidates can create repetitive results.
How to Choose a Beam Width
Tune beam width using real task outcomes.
- Start small and measure quality and response time.
- Evaluate complete outputs with representative prompts or test cases.
- Use length normalization where length affects score fairness.
- Test diversity methods if candidates are too similar.
Where Beam Search Fits in AI Applications
Beam search fits applications that select sequences of tokens or decisions, including machine translation, speech-to-text, structured text generation, and constrained code or workflow generation. It differs from semantic search and vector search, which retrieve relevant information rather than decode next steps.
Beam Search in Emergent
In Emergent, teams can build AI-powered applications that expose and test settings such as beam width, then compare output quality, consistency, and response time. Build controlled AI outputs with Emergent.
on Emergent today


