Beam Search
Beam search is a heuristic decoding algorithm that generates text by tracking the top-scoring k partial sequences (the "beam width") at each step, expanding each and keeping only the highest-probability overall candidates.
Definition
Beam search is a heuristic decoding algorithm that generates text by tracking the top-scoring k partial sequences (the "beam width") at each step, expanding each and keeping only the highest-probability overall candidates.
Overview
Beam search sits between two extremes of text generation: greedy decoding, which picks only the single most probable next token at each step and can get stuck in a locally optimal but globally poor sequence, and an exhaustive search over every possible sequence, which is computationally intractable. Beam search approximates a better global solution by maintaining a fixed number of candidate sequences — the beam width, often denoted k — at every generation step. At each step, every candidate in the beam is expanded with all possible next tokens, the resulting sequences are scored by cumulative probability, and only the top k are kept for the next step. A wider beam explores more of the search space and tends to find higher-probability full sequences, at the cost of significantly more computation, since the number of forward passes scales with beam width. Beam search was widely used in earlier neural sequence-to-sequence systems, particularly for machine translation and summarization, where it reliably improved output quality over greedy decoding by avoiding some short-sighted token choices. However, for modern large language models used in open-ended conversational generation, beam search has fallen out of favor relative to sampling-based methods like top-p sampling and temperature-controlled sampling. This is because beam search tends to produce overly generic, repetitive, or bland text — it optimizes for the single highest-probability sequence, which in open-ended generation is often a bureaucratic, safe-sounding continuation rather than an interesting or diverse one. Beam search remains relevant for tasks with a single clearly correct or near-correct output, such as translation, where maximizing likelihood aligns well with output quality, and it is often paired with techniques like length normalization to avoid unfairly penalizing longer sequences.
Key Concepts
- Maintains the top-k highest-scoring partial sequences at each decoding step
- Balances between greedy decoding and exhaustive search
- Beam width (k) controls the trade-off between quality and compute cost
- Commonly paired with length normalization to avoid bias against longer outputs
- Historically dominant in neural machine translation and summarization
- Tends to produce more generic or repetitive text for open-ended generation
- Largely superseded by sampling methods for conversational LLMs
- Computation scales with beam width due to parallel sequence expansion
Use Cases
Frequently Asked Questions
From the Blog
RAG Explained: Retrieval-Augmented Generation
RAG is how you give an LLM access to your own private data without training a new model. This guide explains the full pipeline — chunking, embeddings, vector search, and augmented generation — with a working Python example using open-source tools.
Read More AI & TechnologyVector Databases Explained: The Memory Layer Powering AI Apps
Vector databases are the storage layer behind RAG systems, semantic search, and AI- powered recommendations. This guide explains what they are, how they differ from traditional databases, and how to choose and use one in a real application.
Read More Career GrowthHow to Build a Developer Portfolio That Gets You Hired
A developer portfolio is your most powerful job-search tool — more important than your degree, and often more persuasive than your resume. This guide explains what to build, how to present it, and how to make recruiters stop scrolling.
Read More Career GrowthLinkedIn Tips for Developers: Turn Your Profile Into an Inbound Machine
Most developers treat LinkedIn as an online CV and wonder why recruiters don't reach out. This guide explains how to optimise your profile for recruiter search, what to post to build visibility, and how to use LinkedIn to land interviews without cold-applying.
Read More