What is beam search?
Beam search is a decoding method that keeps the few most likely partial sequences at each generation step and finally picks the kept sequence with the highest total probability; the number kept is the beam width.
Unlike greedy decoding, which takes only the most likely token each step and never backtracks, beam search keeps several alternatives open, so it can find a sequence slightly less likely at first but better overall. Unlike sampling methods such as top-p sampling, it is deterministic.
- Topic
- Prompts, context, reasoning and generation
- Also known as
- Beam decoding
How beam search works
- A beam width is chosen, say 4; at the first step the 4 most likely tokens are kept as beams.
- At each step possible next tokens extend every beam and all candidates are scored by total log probability.
- Only the 4 highest-scoring sequences among all candidates remain; the rest are dropped.
- Sequences reaching an end token are set aside; scores are usually length-normalised, since otherwise short sequences are favoured.
In more detail
Length penalties and repetition penalties
Standard beam search maximises sequence likelihood, which naturally favours shorter sequences because every added token multiplies in a probability below 1. In practice, length normalisation divides the total log probability by the sequence length raised to a tunable exponent; a repetition or coverage penalty can also be added to prevent loops.
KV cache overhead and the effect on inference engines
During LLM decoding, beam search multiplies active sequence states by the beam width B, increasing KV-cache memory pressure and reducing maximum concurrent batch size on GPU. Modern serving engines mitigate prefill overhead via shared prefix KV blocks, yet divergent decoding steps still consume roughly B times the per-token cache of greedy decoding.
Step-level beam search in reasoning models
While token-level beam search has largely faded from open-ended chat, step-level beam search has re-emerged in reasoning pipelines. In test-time compute scaling, candidate reasoning steps are scored by a Process Reward Model (PRM) and the top B partial reasoning paths are expanded iteratively rather than single tokens.
Commonly confused terms
| Term | How it differs |
|---|---|
| Diverse beam search | Diverse beam search adds a dissimilarity penalty across beam groups to prevent near-identical candidates; standard beam search often produces beams differing by only one or two tokens. |
Why beam search matters for on-premises AI
Beam search is common in tasks expecting output close to one right answer, such as machine translation, summarisation and speech recognition. Being deterministic, the same input gives the same output, which eases testing and audit. In open-ended chat it can produce repetitive, bland text, so sampling has largely replaced it there. Cost rises with beam width, a multiplier to include when planning in-house GPU capacity.
An automotive supplier uses beam search in an in-house model translating technical manuals from English to Turkish; since a paragraph always gets the same translation, checking output against the approved terminology list is easier.
Common questions
Why is beam search rarely used in chat models?
It seeks the most likely sequence, which in open-ended text can give repetitive, bland answers. Sampling with temperature and top-p gives more natural text in chat.
How is beam width chosen?
A wider beam tries more alternatives, but cost grows with width while gains shrink past a point. The value is chosen on the task's own validation data.
Related terms
- Greedy decoding
- Top-p sampling
- Top-k sampling
- Best-of-N sampling
- Decode phase
- Machine translation
- Monte Carlo tree search
Sources
- Jurafsky and Martin, Speech and Language Processing, 3rd edition draft
- Sutskever, Vinyals and Le, Sequence to Sequence Learning with Neural Networks, 2014
In the glossary: Glossary, A to C