Beam Search is a best-first search algorithm that maintains only a fixed number, k (the beam width), of the most promising nodes at each level of the search tree. Unlike breadth-first search, which expands all nodes at a given depth, Beam Search prunes less promising branches, significantly reducing memory and computational cost. It is not guaranteed to find an optimal solution, as its performance depends heavily on the quality of the heuristic function used to evaluate nodes.
Glossary
Beam Search

What is Beam Search?
Beam Search is a heuristic search algorithm that explores a graph by expanding the most promising node in a limited set, called the beam width, to reduce memory usage compared to breadth-first search.
The algorithm is fundamental in sequence generation tasks for large language models, where it is used to decode high-probability text sequences token-by-token. By tracking multiple candidate sequences (the beam), it balances exploration and efficiency, outperforming greedy decoding while being far less computationally intensive than an exhaustive search. Its parameters, beam width and length normalization, are critical for tuning the trade-off between output quality and inference latency.
Key Characteristics of Beam Search
Beam Search is a heuristic search algorithm that explores a graph by expanding the most promising node in a limited set, called the beam width, to reduce memory usage compared to breadth-first search. It is a core technique for balancing exploration and computational efficiency in sequence generation tasks.
Beam Width (k)
The beam width (k) is the algorithm's primary hyperparameter, defining the number of most promising partial sequences (or nodes) retained at each step of the search. This creates a memory- and compute-efficient approximation of breadth-first search.
- k=1: Equivalent to a greedy search, following the single best option at each step.
- k=n (full breadth): Equivalent to exhaustive breadth-first search, exploring all possibilities but at high computational cost.
- Typical values: Range from 4 to 10 for tasks like machine translation or text summarization, providing a practical trade-off between quality and speed.
Pruning & Candidate Selection
At each decoding step, Beam Search performs a pruning operation. It generates all possible next-step extensions for the k sequences in the current beam, scores them (e.g., using log probabilities), and then selects only the top-k highest-scoring sequences to form the new beam. All other candidates are permanently discarded.
This selective pruning is what differentiates it from best-first search algorithms that maintain a global priority queue, making Beam Search far more memory-efficient for long sequences common in natural language generation.
Time & Space Complexity
Beam Search offers a predictable computational profile, making it suitable for production systems.
- Time Complexity: Approximately O(k * V * T), where
kis the beam width,Vis the vocabulary size (for language models), andTis the target sequence length. This is a dramatic reduction from the exponential complexity of exploring all possible sequences. - Space Complexity: O(k * T), as the algorithm must store the
kcandidate sequences, each of length up toT. This is its key advantage over breadth-first search, which has a space complexity that grows exponentially with depth.
Termination & Output Selection
Beam Search typically terminates when all k candidate sequences in the beam have reached an end-of-sequence token or when a predefined maximum length T is reached. The final output is selected from the completed sequences in the beam.
The standard selection criterion is the sequence with the highest cumulative score, often the sum of log probabilities. However, this can favor shorter sequences. Length normalization (dividing the score by sequence length) is a common technique to correct for this bias and is critical for comparing sequences of different lengths.
Comparison to Greedy & BFS
Beam Search occupies a strategic middle ground between other fundamental search strategies.
- vs. Greedy Decoding (k=1): Greedy search is faster but can commit to a suboptimal early token, leading to locally optimal but globally poor sequences (a greedy trap). Beam Search maintains multiple hypotheses, offering a chance to recover from early mistakes.
- vs. Breadth-First Search (BFS): BFS is guaranteed to find the optimal sequence but is computationally infeasible for large state spaces (like language model vocabularies). Beam Search provides a bounded approximation of BFS, trading optimality guarantees for tractability.
Primary Use Case: Sequence Generation
Beam Search is the de facto standard decoding algorithm for autoregressive sequence generation tasks where quality is prioritized over pure speed. Its core application is guiding large language models (LLMs) and sequence-to-sequence models.
Common Applications:
- Machine Translation (e.g., Google's Neural Machine Translation systems)
- Text Summarization
- Image Captioning
- Speech Recognition (decoding over phoneme or word sequences)
- Code Generation
It is less suitable for tasks requiring highly diverse outputs or creative exploration, where sampling-based methods (like top-k or nucleus sampling) are often preferred.
Beam Search vs. Other Search Algorithms
A feature comparison of Beam Search against other common heuristic and uninformed search algorithms, highlighting trade-offs in memory, optimality, and computational efficiency relevant to agent decision-making.
| Feature / Metric | Beam Search | Breadth-First Search (BFS) | Depth-First Search (DFS) | A* Search |
|---|---|---|---|---|
Primary Use Case | Pruning large state spaces (e.g., text generation, planning) | Finding shortest path in unweighted graphs, exhaustive exploration | Exploring deep branches, cycle detection, topology sorting | Finding optimal cost path with a known admissible heuristic |
Memory Complexity (Worst-Case) | O(b * w) | O(b^d) | O(b * d) | O(b^d) |
Optimality Guarantee | No (suboptimal due to beam pruning) | Yes (for unweighted graphs) | No | Yes (with admissible heuristic) |
Completeness Guarantee | No (can prune goal) | Yes | No (in infinite spaces) | Yes |
Time Complexity (Worst-Case) | O(b * w * d) | O(b^d) | O(b^d) | O(b^d) |
Guided by Heuristic? | ||||
Key Parameter(s) | Beam Width (w) | None | Depth Limit (optional) | Heuristic Function h(n) |
Best For Agentic Systems When... | Generating high-probability sequences under strict memory constraints | Guaranteeing shortest action plans in small, unweighted state spaces | Exploring deep, unbounded reasoning chains where memory is limited | Finding provably optimal action sequences with a reliable cost model |
Frequently Asked Questions
Beam Search is a core heuristic search algorithm for navigating vast decision spaces efficiently. These questions address its mechanics, trade-offs, and practical applications in AI systems.
Beam Search is a heuristic graph search algorithm that explores a state space by expanding only the most promising k nodes at each depth level, where k is the beam width. It operates by maintaining a beam—a limited-size priority queue of the best nodes—rather than the full frontier used in breadth-first search. At each step, it generates all successors for the nodes in the current beam, evaluates them using a scoring function (often a heuristic plus path cost), and selects the top k successors to form the new beam. This process repeats until a goal state is found or a maximum depth is reached. The algorithm provides a memory-efficient compromise between greedy search (beam width of 1) and exhaustive breadth-first search.
Enabling Efficiency, Speed & Accuracy
Intelligent Analysis, Decision & Execution
We build AI systems for teams that need search across company data, workflow automation across tools, or AI features inside products and internal software.
Talk to Us
Search across company data
Give teams answers from docs, tickets, runbooks, and product data with sources and permissions.
Useful when people spend too long searching or get different answers from different systems.

Automate internal workflows
Use AI to route work, draft outputs, trigger actions, and keep approvals and logs in place.
Useful when repetitive work moves across multiple tools and teams.

Add AI to products and internal tools
Build assistants, guided actions, or decision support into the software your team or customers already use.
Useful when AI needs to be part of the product, not a separate tool.
Related Terms
Beam Search operates within a broader ecosystem of algorithms designed for efficient state space exploration. These related concepts define its core mechanisms, trade-offs, and alternatives.
Greedy Best-First Search
A search algorithm that expands the node deemed closest to the goal based solely on a heuristic function h(n). It prioritizes immediate promise without considering the path cost incurred to reach the node. This makes it fast but often suboptimal and prone to getting stuck, as it lacks the cumulative cost consideration of algorithms like A* or the breadth consideration of Beam Search.
- Core Mechanism: Always expands the node with the lowest heuristic value.
- Trade-off: Extreme speed for potential loss of optimality.
- Relation to Beam Search: Beam Search with a beam width of
k=1is equivalent to Greedy Best-First Search, as it only keeps the single most promising node at each depth level.
Breadth-First Search (BFS)
A graph traversal algorithm that explores all nodes at the present depth level before moving to the next level. It uses a queue to manage the frontier and guarantees finding the shortest path in an unweighted graph. Its primary limitation is memory consumption, as it stores all nodes at the current frontier, which grows exponentially with depth.
- Exhaustive Nature: Systematically explores all possibilities level by level.
- Memory Complexity:
O(b^d), wherebis branching factor,dis depth. - Relation to Beam Search: Beam Search is a memory-constrained approximation of BFS. Instead of keeping all nodes at a level, it prunes all but the top-
k(the beam width), trading completeness and optimality guarantees for tractability.
Beam Width (k)
The hyperparameter k that defines the core constraint of the Beam Search algorithm. It is the maximum number of most promising nodes (the "beam") retained for expansion at each step or depth level of the search. This parameter directly controls the trade-off between search quality and computational cost.
- Small
k(e.g., 1-10): Highly memory and compute efficient, but risks pruning the globally optimal path early ("search error"). - Large
k: Approximates breadth-first search, improving solution quality at the cost of increased memory (O(k)) and time (O(k * b)). - Tuning Context: In sequence generation (e.g., machine translation), typical values range from 4 to 10. In complex planning, it may be tuned based on available compute.
Pruning
A general technique in search and optimization algorithms to eliminate branches or nodes from consideration that are deemed unlikely to be part of an optimal solution. This reduces the effective size of the search space, making exploration tractable.
- Beam Search Pruning: At each step, all generated successor nodes are scored (e.g., by a heuristic or model probability). Only the top-
kare kept; the rest are permanently discarded. - Contrast with Alpha-Beta Pruning: Used in game trees (Minimax), where branches are cut because they cannot affect the final decision, guaranteeing the same result as an exhaustive search.
- Key Insight: Beam Search uses aggressive, heuristic-based pruning that sacrifices guarantees for efficiency, unlike optimal pruning methods.
Viterbi Algorithm
A dynamic programming algorithm for finding the most likely sequence of hidden states (the Viterbi path) in a Hidden Markov Model (HMM). It efficiently computes the optimal path through a trellis diagram by maintaining the maximum probability path to each state at each time step.
- Optimality Guarantee: Finds the single globally optimal sequence.
- Mechanism: Uses dynamic programming to avoid recomputation, storing backpointers.
- Relation to Beam Search: Beam Search is often viewed as an approximation of the Viterbi algorithm for models where the state space is too large (e.g., neural sequence models). Instead of keeping paths to all states, Beam Search keeps only the top-
kpaths, offering a tractable but potentially suboptimal alternative.
Local Beam Search
A variant of Beam Search where the k best states are selected from the entire set of successors generated by all current beam states, not just within independent depth levels. It operates more like a parallel, stochastic hill-climbing search.
- Mechanism: Generate successors from all
kcurrent states. From the combined pool, select thekbest unique states as the next beam. Repeat. - Contrast with Standard (Global) Beam Search: Standard Beam Search progresses depth-by-depth, akin to a level-synchronous search. Local Beam Search is asynchronous and can "jump" between branches more freely.
- Use Case: Often applied in optimization and constraint satisfaction problems where a strict sequence length isn't defined, unlike in sequence generation tasks.

About the author
Prasad Kumkar
CEO & MD, Inference Systems
Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.
His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.
Partnered with leading AI, data, and software stack.
How We Work
Custom AI workflows for your Business
One-fit-all AI don't work for modern businesses. At Inferensys, we aim to understand your business & custom requirements; which we use to define most efficient agentic workflows, the data, and the tools for your business.
01
Review the use case
We understand the task, the users, and where AI can actually help.
Read more02
Pick the right approach
We define what needs search, automation, or product integration.
Read more03
Build the first useful version
We implement the part that proves the value first.
Read more04
Improve from there
We add the checks and visibility needed to keep it useful.
Read moreThe first call is a practical review of your use case and the right next step.
Talk to Us