Practice in Lab: Chunk token identities without losing the tail →
Chunk token identities without losing the tail
Chunking should use the same token representation as its declared budget. Splitting by character count and calling the result a token limit creates false capacity estimates. Overlap may preserve boundary context but increases index size and duplicate evidence.
The exercise receives token IDs directly and returns windows with starting offsets. It retains the final short window and stops when the last token has already been covered. These are mechanical boundaries, not a semantic chunking solution. Real document pipelines should preserve source offsets, document versions and permission metadata with every chunk.
tokens=list(range(7))
size,overlap=4,1
print([(i,tokens[i:i+size]) for i in range(0,len(tokens),size-overlap)])Review the design
Offsets make chunks auditable against the original token sequence. They must be mapped to the original source through a real tokenizer before showing quotations or citations to a user.
Your task
Implement token_windows(tokens,size,overlap) returning (start,list_of_tokens) windows. Require integer size>0 and 0<=overlap<size. Advance by size-overlap and stop once the last emitted window reaches the end. Empty input returns [].
The explanation opens here once every check passes.