WRITE · TRACE · UNDERSTAND

Stop token decoding across chunk boundaries

chunks and stops are lists of nonnegative integer-token lists; stop sequences cannot be empty.

Read lesson: Stop token decoding across chunk boundaries →

Your experiment

chunks and stops are lists of nonnegative integer-token lists; stop sequences cannot be empty. Consume chunks incrementally, retaining output across chunk boundaries. After each token, first check complete stop suffixes and remove the longest matching stop; otherwise stop when output length reaches integer max_tokens>=0. Return tokens, reason (stop,budget,eof), consumed. Stop tokens count as consumed. A hard budget may expose an incomplete stop prefix; this contract does not promise prefix buffering.

Python runs in your browser. Your draft stays on this device.

What to look for

Review overlapping stops, chunk splitting, budget precedence and incomplete prefixes. Token identities are fixtures; no model tokenizer or network stream is claimed to run here.

Watch your code work

Ready to experiment

Write a solution or load the worked answer, then choose Trace & check.

Step through arrays, variables and training curves produced by your own Python.

About this algorithm

How Stop token decoding across chunk boundaries works

The idea

A model server streams tokens to the client in chunks, and it must stop when the output ends with a stop sequence. A stop sequence can be several tokens long, so it may arrive split between two chunks. Looking for it inside each chunk separately will miss it.

The remedy is to keep one output list across all chunks and, after every single token, compare the tail of that list with each stop sequence. Chunk boundaries then have no effect on the result. On a match the longest matching stop is cut from the output and decoding ends with reason stop. The stop test runs before the budget test. This Lab does not hold back partial matches, so a hard budget can end the output on an incomplete stop prefix.

How it works

  1. Reject a bad max_tokens, empty stop sequences and tokens that are not nonnegative integers; return reason budget at once when max_tokens is 0.
  2. Loop over chunks and their tokens, appending each token to the shared output list and adding one to consumed.
  3. After each token, collect in matches every stop sequence equal to the tail of output.
  4. On a match, cut the longest matching stop off the end and return with reason stop.
  5. Otherwise, when len(output) reaches max_tokens, return the output with reason budget.
  6. When the chunks run out, return with reason eof.

What it costs

Time
O(n·S)
Space
O(n) output buffer

For each of n tokens the code compares the output tail with every stop sequence, so the work per token is bounded by S, the total length of all stops.

Where it is used

  • The OpenAI Chat Completions API accepts up to four stop sequences and leaves the matched sequence out of the returned text.
  • Anthropic's Messages API takes stop_sequences and reports a stop_reason such as stop_sequence, max_tokens or end_turn.
  • Hugging Face Transformers provides StoppingCriteria classes, including StopStringCriteria, to end generate() on given strings.

Watch out for

  • Searching for stops inside each chunk separately: the stop [7, 8] split across the chunks [1, 7] and [8, 9] is never found.
  • Testing the budget before the stop: when the last allowed token completes a stop sequence, the reason must be stop, not budget.
  • Removing the first matching stop instead of the longest: with the stops [8] and [7, 8], the 7 would stay in the output.