Modern AI Engineering

Speculative Decoding Explained: Faster LLM Inference, Same Output

By Modern AI Engineering · · 8 min read

A big LLM spends most of each decoding step waiting on memory, not doing math. Speculative decoding turns that idle compute into extra tokens: a cheap guesser drafts several tokens, and the big model checks them all in one pass.

In short: a small, fast draft model proposes the next k tokens. The large target model scores all k positions in a single forward pass and keeps the longest run of drafts it agrees with, plus one token of its own. With the standard acceptance rule, the final text follows exactly the same probability distribution as the target model alone. You get roughly 2× to 3× faster decoding in good cases, with no change in quality.

Why plain decoding is slow

Each new token needs a full forward pass, and each pass must read all the weights and the KV cache from GPU memory. For one sequence, that reading takes far longer than the arithmetic, so the GPU's compute units sit mostly idle.

The useful fact: checking 5 tokens in one pass costs almost the same time as generating 1, because the weights are read once either way. Speculative decoding exploits exactly that.

How speculative decoding works, step by step

Every round produces at least one token, so it is never worse in token count than normal decoding. In a good round it produces k + 1.

A worked example. Prompt: "The capital of France is". The draft proposes Paris . It has. The target agrees with Paris, the full stop and It, but prefers is over has. Result: four tokens (Paris . It is) for one target pass instead of four.

  1. Draft: the small model generates k tokens one by one, for example k = 4. It is cheap, so this is fast.
  2. Verify: the target model runs one forward pass over the prompt plus all k drafted tokens, which gives its own probability at every drafted position at once.
  3. Accept or reject: walk through the drafts in order and accept each with probability min(1, p_target / p_draft). Stop at the first rejection.
  4. Correct: at the rejected position, sample one token from an adjusted target distribution. If every draft was accepted, the same pass gives one bonus token for free.
  5. Repeat from the new position.

Why the output quality does not change

This surprises most people. The accept-or-reject rule is a form of rejection sampling. Accepting each draft with probability min(1, p_target / p_draft), and resampling from the leftover probability on rejection, makes the final distribution exactly equal to the target model's. A bad draft model only makes things slower; it cannot make answers worse.

With greedy decoding (temperature 0) the rule is simpler: accept a draft token only if it is the target's top choice.

How much faster? The formula

Let α be the probability that a draft token is accepted and k the number of drafted tokens. The expected number of tokens per target pass is (1 − α^(k+1)) / (1 − α).

Two lessons jump out. The acceptance rate matters more than k: at α = 0.6, drafting 8 tokens instead of 4 adds almost nothing. And these are tokens per target pass, not wall-clock speedup. Subtract the draft model's time and real speedups usually land around 1.5× to 3×.

def expected_tokens(alpha, k):
"""Expected tokens produced per target-model forward pass."""
return (1 - alpha ** (k + 1)) / (1 - alpha)
Acceptance rate αk = 2k = 4k = 6k = 8
0.61.962.312.432.47
0.72.192.773.063.20
0.82.443.363.954.33
0.92.714.105.226.13

Ways to get draft tokens

The draft must use the same tokenizer as the target, or the token positions cannot be compared.

MethodWhere drafts come fromGood for
Separate draft modelA small model from the same family and tokenizerGeneral use; simple to set up
Extra decoding heads (Medusa-style)Small heads on the target model predict several future tokensNo second model to host
Feature-level drafting (EAGLE-style)A light head predicts from the target's hidden statesHigh acceptance rates
N-gram or prompt lookupCopies matching spans from the prompt or contextCode edits, RAG answers, summaries that reuse input text
Self-speculationThe target with some layers skipped acts as its own drafterOne set of weights

When speculative decoding helps, and when it hurts

Measure it on your real traffic. The only numbers that matter are end-to-end latency and throughput at your actual batch size.

  • Helps: few requests at a time where latency matters; predictable output such as code, JSON or text that repeats the input; low temperature.
  • Hurts: a GPU already full with large batches, so no idle compute is left; creative, high-temperature output with low acceptance; a draft model so large that drafting costs more than it saves.

Common mistakes

  • Assuming quality drops. It does not with the correct acceptance rule. If outputs change, the implementation is wrong.
  • Picking the biggest draft model you can fit. A slow draft cancels the gain. Small and well-aligned beats big.
  • Tuning k before measuring α. Find the acceptance rate on your data first.
  • Testing at batch size 1 and deploying at batch size 64. The speedup shrinks as batches grow.

Frequently asked questions

Does speculative decoding change the model's answers?

No. With the standard rejection-sampling rule, the output distribution is identical to the target model's. Only speed changes.

What is a draft model?

A smaller, faster model that guesses the next few tokens. It must share the target model's tokenizer.

How much faster is speculative decoding?

Typically 1.5× to 3× in wall-clock decoding time, depending mainly on how often the draft is right and how busy the GPU already is.

Can I use speculative decoding with any LLM?

With an open model you serve yourself, usually yes; most major inference engines support at least one form. With a hosted API, it is up to the provider.

Where does the formula come from?

From Leviathan, Kalman and Matias, Fast Inference from Transformers via Speculative Decoding (ICML 2023), which introduced the acceptance rule.

Learn it properly: the AI Engineering Bootcamp

More articles