Homeglossary

Beam Search

Beam search is a heuristic search algorithm that explores possible outputs while retaining only a fixed number of the strongest candidate paths at each step. In AI text generation, it helps a model consider several likely word sequences instead of committing immediately to one. Its beam width controls the tradeoff between output quality, computing cost, and the risk of discarding a better path too early.

Build controlled AI outputs with Emergent.

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.

  1. Start with an initial state, such as an empty sentence or workflow.
  2. Expand each retained path with possible next tokens or actions.
  3. Score new paths using cumulative probability, cost, or a heuristic.
  4. Keep only the top paths allowed by the beam width.
  5. 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.

MethodWhat it keepsBest fit
Greedy searchOne best next choiceFast, deterministic output
Beam searchSeveral top partial pathsStructured, high-confidence sequences
SamplingRandomized choices from likely optionsCreative 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.

Start Building
on Emergent today
Start Building