LLM Inference Evolved: A Guide to Decoding Algorithms
A technical look at how decoding algorithms — from greedy search to nucleus sampling and speculative decoding — shape LLM inference quality, latency, and cost in modern generative AI systems.
Large language models may grab headlines for their training scale, but the quality, speed, and cost of their outputs are determined largely by a less-glamorous component: the decoding algorithm. A new technical primer published on Towards AI walks through the evolution of these algorithms — the procedures that turn raw probability distributions into the tokens you actually read — and why each generation of techniques exists.
For practitioners building everything from chatbots to AI video script generators and voice-cloning pipelines, understanding decoding is essential. The same model can produce dull, repetitive boilerplate or vivid, coherent prose depending entirely on how tokens are sampled at inference time.
From Greedy Search to Probabilistic Sampling
At its core, an LLM outputs a probability distribution over its vocabulary at each step. Greedy decoding simply picks the highest-probability token every time. It is fast and deterministic, but notoriously prone to repetition and lifeless output — exactly the symptoms users complain about when an AI assistant feels robotic.
Beam search extends greedy decoding by maintaining several candidate sequences (beams) in parallel and keeping the highest-scoring combinations. It improves coherence in tasks like translation and summarization where there is a "correct" answer, but it tends to collapse to safe, generic phrasing for open-ended generation. Beam search also scales poorly: doubling the beam width roughly doubles compute.
Top-k and Nucleus Sampling
To inject diversity without devolving into gibberish, researchers introduced stochastic methods. Top-k sampling restricts the candidate set to the k most likely tokens before sampling, cutting off the long tail of low-probability words that often produce nonsense. Nucleus (top-p) sampling, introduced by Holtzman et al. in 2019, dynamically adjusts the candidate pool to include the smallest set of tokens whose cumulative probability exceeds a threshold p (commonly 0.9 or 0.95).
Nucleus sampling became the de facto default for creative generation because it adapts to the shape of the distribution: when the model is confident, the pool is small; when it is uncertain, more options compete. Combined with a temperature parameter that flattens or sharpens the distribution, top-p gives developers fine-grained control over the creativity-coherence tradeoff.
Why Decoding Matters Beyond Text
These algorithms are not confined to chatbots. Modern text-to-video, text-to-image, and text-to-speech systems rely heavily on autoregressive or diffusion-based decoders that face the same fundamental problem: choosing the next token, frame, or latent given a probability distribution. Voice cloning systems like those powering ElevenLabs and OpenAI's Voice Engine use sampling strategies derived from LLM decoding to balance naturalness against artifact-free output. Video generation pipelines from Runway, Pika, and Sora apply analogous logic to latent-space sampling.
Speculative Decoding and the Inference Cost Crisis
As models grew to hundreds of billions of parameters, raw inference cost became the bottleneck. Speculative decoding, popularized by DeepMind and adopted across the industry, uses a small "draft" model to propose several tokens ahead, which the large target model then verifies in a single forward pass. When the draft is correct, multiple tokens are accepted at once, yielding 2-3x speedups without changing output distribution.
Variants like Medusa, EAGLE, and lookahead decoding push this further by training auxiliary heads or using n-gram caches to predict draft tokens without a separate model. These techniques are now standard in production stacks like vLLM, TensorRT-LLM, and SGLang.
Constrained and Structured Decoding
The latest frontier is constrained decoding, which forces outputs to conform to a JSON schema, regular expression, or grammar. Libraries like Outlines, Guidance, and XGrammar mask the logits at each step so that only valid tokens are sampled. This is critical for agent harnesses, tool-calling, and any system where downstream code must parse the model's output reliably — including synthetic data pipelines used to train deepfake detectors.
The Strategic Picture
Decoding algorithms occupy a strategic sweet spot: they require no retraining, can be swapped at runtime, and directly affect both user experience and per-token cost. For companies building on top of foundation models, mastering decoding is often where real product differentiation happens — whether that means tuning nucleus parameters for a creative writing tool or deploying speculative decoding to cut GPU bills on a video captioning service.
As inference workloads continue to dwarf training in aggregate compute spend, expect decoding research to remain one of the highest-leverage areas in applied AI.
Stay informed on AI video and digital authenticity. Follow Skrew AI News.