Batches, Stopping, and Context Limits Complicate Decode

Track per-row lengths, masks, cache ownership, EOS state, and context exhaustion when batched sequences stop at different times. Keep request state isolated and report distinct stopping reasons.

A single sequence has one cache length and one stopping state. A batch may contain different prompt lengths, choose <eos> at different steps, or reach context limits at different times.

Keep State per Sequence

For each batch row, track:

  • prompt and generated IDs;
  • valid cached length;
  • position index for the next consumed token;
  • active or finished state;
  • stopping reason;
  • decoding-policy random state where independent streams are required.

Do not let one finished row append ordinary tokens merely because another row is still active.

Common Handling Strategies

An implementation can:

  • pad cache tensors and use validity masks;
  • group sequences with similar lengths;
  • remove finished rows from an active batch and remap state;
  • use a runtime that supports variable-length cache blocks.

Each strategy changes bookkeeping and performance, not the model's intended logits for a given valid prefix.

Context Limit and EOS Are Different

  • eos means the policy selected a learned stopping token.
  • context means the model has no valid next input position under this configuration.

A sequence can reach the context limit without selecting <eos>. Report that distinction to the caller.

Padding Must Not Become Evidence

If cache tensors are padded to a common length, prevent queries from reading unused cache slots. A causal relation alone does not identify which earlier slots are padding in another batch row.

Q1. Handle a partly finished batch

Two rows are decoding together. Row A selects <eos>; row B selects X and still has context capacity. What should happen next?

Choose the next action

Select one choice, then check.

Hint
One row's EOS should not stop or extend the other row.
Solution
Record eos for A and exclude it from further token consumption. Append and process X only for B with B's cache length and position.
Not attempted
Review

Not marked done.

Serving Systems Are a Later Layer

Continuous batching, cache paging, scheduling, request admission, and memory reclamation deserve a separate engineering treatment. This lesson establishes the state they must preserve.

Pause and reflect

In your own words, note what you understood, what remains unclear, or what you want to revisit. The note stays with this lesson.

0 of 1 exercises marked done

Review

Not marked done.

LLM PrimerBatches, Stopping, and Context Limits Complicate Decodehttps://llmprimer.com/transformers/autoregressive-generation-and-efficient-inference/batched-decoding-and-stopping© 2026 LLM Primer