Skip to content
AgenticAIWiki
  1. Learn
  2. Part 5
  3. 30. Local inference
  4. 30.8

Lesson 30.8 · 8 of 8 · 7 min read · Reviewed 24 Nov 2026

Speculative decoding and other serving speed-ups

By the end, you can

  • Explain how speculative decoding speeds up generation without changing the output.
  • Estimate the speed-up from the acceptance rate and draft length.
  • Name other common serving speed-ups and what each one saves.

Lesson 30.4 showed that generating each tokenToken: The unit a model reads and writes: a word, part of a word, a space or a symbol, mapped to an integer id. means reading the model’s weights from memory. For one user, the GPU spends much of its time waiting on memory, not doing maths. Speculative decoding uses that spare compute.

The idea

A large model checking five tokens in one forward pass costs about the same as generating one, because the weights are read once either way. So:

  1. A small, fast draft model guesses the next few tokens, say 4.
  2. The large target model scores all 4 positions in a single pass.
  3. Guesses are accepted left to right until the first one the target disagrees with. At that point the target’s own token is used instead.
  4. Repeat from the new position.

Every pass of the large model yields at least one token, and often several. With the acceptance rule from the original papers, the text has the same distribution the target model alone would produce. You get speed, not a different answer.

A worked example

Suppose the draft proposes 4 tokens and each guess is accepted with probability 0.8. The expected number of tokens per target pass is:

(1 - a^(k+1)) / (1 - a)   with a = 0.8, k = 4
= (1 - 0.8^5) / 0.2
= (1 - 0.33) / 0.2
= about 3.4 tokens per pass

Without drafting, you get 1 token per pass. The draft model costs something too, so the real speed-up is lower, often around 2x for predictable text such as code, JSON or a summary that quotes its source. When acceptance falls to 0.4, the same formula gives about 1.6 tokens per pass, and the draft’s own cost can wipe out the gain.

Variations

Variant Where the guesses come from
Separate draft model A small model from the same family, sharing the tokenizer
Extra prediction heads Small heads added to the target model predict several tokens ahead
Prompt lookup (n-gram) Copies likely continuations from the prompt itself, useful for editing and extraction

Other serving speed-ups

Technique What it saves
Continuous batching Idle GPU time: new requests join a running batch at each step
Paged KV cache Wasted memory, so more requests fit at once
Prefix caching Re-reading a shared system prompt or document for each request
Quantized KV cache Memory for long contexts, at a small quality cost

Speculative decoding mostly helps latencyLatency: The time from sending a request until you get the response, or until the first token arrives when streaming. for each user. Batching mostly helps throughputThroughput: How much work a system completes per unit of time, such as requests per second or tokens per minute. across users. A busy server with full batches has less spare compute, so speculative decoding gains less there.

When to use it

  • Single-user or low-concurrency setups where tokens per second matters.
  • Predictable outputs: code, structured data, rewriting a given text.
  • When a small model from the same family exists, with the same tokenizerTokenizer: The component that splits text into tokens and maps each token to an id the model can process..

Measure on your real prompts. Track the acceptance rate: it tells you directly whether the draft is earning its keep.

Key points

  • Speculative decoding lets a small draft model guess several tokens, and the large model checks them all in one pass.
  • Accepted guesses are free extra tokens; the verification rule keeps the output the same as the large model alone would give.
  • The speed-up depends on how often the draft is right: high on predictable text, low on surprising text.
  • Other speed-ups include continuous batching, paged KV cache, prefix caching and a quantized KV cache.
Hands-on labRuns in the Playground

Simulate speculative decoding acceptance

Check the speculative decoding formula against a simulation and find when drafting pays off.

You will have: A table of simulated and exact tokens per pass, and net speed-ups, for two acceptance rates and three draft lengths.

Steps

  1. Fill in the acceptance loop: accept guesses left to right while the random draw is below a, up to k.
  2. Fill in the formula (1 - a^(k+1)) / (1 - a).
  3. Run it and check the simulated and exact values agree.
  4. Compare the net speed-up at a = 0.8 and a = 0.4, and find the draft length you would choose for each.

Done when

  • Simulated and formula values agree to within about 0.05.
  • The check line prints 3.36 tokens per pass.
  • At a = 0.4 the net speed-up falls as k grows.

Open in the Playground No API key needed.

Starter code (Python)
# Simulate speculative decoding. A draft model guesses k tokens; the target accepts each guess with probability a.
import random

rng = random.Random(7)


def tokens_per_pass_sim(a, k, passes=20000):
    total = 0
    for _ in range(passes):
        accepted = 0
        # TODO: accept guesses left to right while rng.random() < a, stopping at k
        total += accepted + 1  # the target always adds one token of its own
    return total / passes


def tokens_per_pass_formula(a, k):
    # TODO: return (1 - a ** (k + 1)) / (1 - a)
    return 1.0


DRAFT_COST = 0.06  # each guess costs 6% of a target pass (illustrative)
for a in (0.8, 0.4):
    for k in (2, 4, 8):
        sim = tokens_per_pass_sim(a, k)
        exact = tokens_per_pass_formula(a, k)
        speedup = exact / (1 + DRAFT_COST * k)
        print(f"a={a} k={k}: simulated {sim:.2f}, formula {exact:.2f}, net speed-up {speedup:.2f}x")

print(f"Check: a=0.8, k=4 gives {tokens_per_pass_formula(0.8, 4):.2f} tokens per pass")
Show a solution
# Simulate speculative decoding. A draft model guesses k tokens; the target accepts each guess with probability a.
import random

rng = random.Random(7)


def tokens_per_pass_sim(a, k, passes=20000):
    total = 0
    for _ in range(passes):
        accepted = 0
        while accepted < k and rng.random() < a:
            accepted += 1
        total += accepted + 1  # the target always adds one token of its own
    return total / passes


def tokens_per_pass_formula(a, k):
    return (1 - a ** (k + 1)) / (1 - a)


DRAFT_COST = 0.06  # each guess costs 6% of a target pass (illustrative)
for a in (0.8, 0.4):
    for k in (2, 4, 8):
        sim = tokens_per_pass_sim(a, k)
        exact = tokens_per_pass_formula(a, k)
        speedup = exact / (1 + DRAFT_COST * k)
        print(f"a={a} k={k}: simulated {sim:.2f}, formula {exact:.2f}, net speed-up {speedup:.2f}x")

print(f"Check: a=0.8, k=4 gives {tokens_per_pass_formula(0.8, 4):.2f} tokens per pass")
Quick checkA team adds speculative decoding, but generation gets slower on creative writing tasks. What is the most likely cause?
How sure are you?
Pick an answer and how sure you are

Finished the chapter? Take the chapter check.