# Beam search

> Canonical: https://skillalmanac.com/glossary/beam-search | Last verified: 2026-11-25 | Index status: index-eligible

Beam search keeps several promising partial output sequences at each step and selects among retained candidates using a configured score. It explores alternatives to greedy decoding, but pruning means it does not guarantee the globally most probable sequence.

Also called: beam search decoding, beam search algorithm, beam width, num_beams, beam decoding.

## In plain language

When a language model writes text, it picks one word piece (called a token) at a time. A simple approach takes the most likely piece at every step, like taking the road that looks best at the next junction. Beam search instead follows a few promising routes in parallel and compares the whole routes at the end, so a route that started slightly worse can still win. It does not make answers true: a high-scoring sequence can still be wrong or dull, and following several routes takes more computation than following one.

## Technical definition

Beam search heuristically extends and prunes partial sequences to a beam width. Scoring commonly accumulates token log probabilities, with settings such as length penalties changing the ranking. In Hugging Face Transformers, num_beams=1 with do_sample=False is greedy decoding; with do_sample=True it is sampling. num_beams>1 enables beam search without sampling when do_sample=False, or beam sampling when do_sample=True. Pruning can discard a prefix leading to the global optimum. The cited translation paper also explores adaptive beam sizes.

## How it shows up when you use agents

Most users of these tools do not set a beam width. Beam search matters when you run an open model yourself, for example through the Hugging Face Transformers library, where `num_beams>1` selects a beam-based mode and `do_sample` distinguishes deterministic beam search from beam sampling.

Beam search sits in the [inference](/glossary/inference) stage: the model assigns probabilities to the next [token](/glossary/token), and the decoding method turns those probabilities into text. Hugging Face describes greedy search as the default, and says beam search is best suited to input-grounded tasks such as describing an image or speech recognition. The same documentation says sampling can generate more creative and diverse outputs.

What breaks when it is misunderstood: a reader may assume a wider beam gives a more correct answer. It selects among retained candidates under the configured score; a wider beam does not guarantee a higher-probability sequence or factual correctness. A wider beam also means more candidates are tracked per step, so budget more compute.

Next action: if you self-host a model, compare a small beam with `num_beams=1` under explicitly recorded sampling and scoring settings on your own evaluation cases before changing a default. See [autoregressive model](/glossary/autoregressive-model) for how text is produced one token at a time.

## Common confusions

- **Beam search versus greedy search.** Greedy search takes the most likely token at each step. Beam search keeps several sequences and can pick one whose first tokens were less likely. In the Transformers API, num_beams=1 is greedy only when do_sample=False; otherwise it samples.
- **Beam search versus sampling.** Sampling picks tokens at random from the probability distribution. Beam search is a search for a high-probability sequence. The two can be combined, but beam search still prunes low-probability sequences between steps.
- **Highest probability versus best answer.** The score derives from model probabilities and decoding settings, including possible length penalties. It is not a check against facts, so see [hallucination](/glossary/hallucination).
- **Fixed versus flexible beams.** The classic version keeps a fixed number of candidates per step. The Freitag and Al-Onaizan paper proposes varying the number by score for translation decoding.

## Worked examples in this catalogue

### [What Is an LLM? The Facts Agent Users Need](https://skillalmanac.com/learn/what-is-an-llm-for-agent-users)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: A guide explaining how a language model relates to the application around it and why generated answers need checking.

Why it exercises the concept: Decoding settings such as beam width belong to the layer between model and application. The guide gives the context for that split; it does not cover beam search itself, and the catalogue has no decoding skill.

### [llm-eval-test-sets](https://skillalmanac.com/skills/skill-almanac/llm-eval-test-sets)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill builds and validates an eval dataset for any LLM feature, with a checker for duplicates, missing graders, imbalance and leakage.

Why it exercises the concept: Comparing a beam setting with another decoding setting needs a fixed set of test cases. This skill is about building that set; it does not run beam search. Illustrative only.

### [local-model-sizing](https://skillalmanac.com/skills/skill-almanac/local-model-sizing)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: The skill works out whether an open-weight language model will run on a given GPU, Mac or PC, with a script that computes weight, KV-cache and overhead memory per quantization.

Why it exercises the concept: Local runs are where a reader can choose decoding options. Per the Hugging Face guide, beam search tracks several sequences at once; this skill does not model that cost, so treat its estimate as a starting point.

## Related terms

- [Inference](https://skillalmanac.com/glossary/inference.md)
- [Token](https://skillalmanac.com/glossary/token.md)
- [Transformer](https://skillalmanac.com/glossary/transformer.md)
- [Autoregressive model](https://skillalmanac.com/glossary/autoregressive-model.md)
- [Natural language processing (NLP)](https://skillalmanac.com/glossary/natural-language-processing.md)
- [Large language model (LLM)](https://skillalmanac.com/glossary/large-language-model.md)

## Sources

- [Generation strategies (Transformers documentation)](https://huggingface.co/docs/transformers/generation_strategies), Hugging Face (accessed 2026-11-24)
- [Beam Search Strategies for Neural Machine Translation - Freitag and Al-Onaizan](https://arxiv.org/abs/1702.01806), arXiv (accessed 2026-11-24)
- [Generation: decoding modes and length_penalty](https://huggingface.co/docs/transformers/main_classes/text_generation), Hugging Face Transformers (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
