liveBook · Manning

Token limit

As explained in Build a Large Language Model (From Scratch)

The fixed number of token IDs included in each sampled input sequence for the loaders in this section.

max_length sets the fixed token-ID length for each sampled input sequence. In text generation loaders, it defines the size of each input chunk and its shifted target chunk; in classification loaders, it can cap or equalize sequence length for batching. section 2.6 section 6.3

For sliding-window language modeling, the dataset slices tokenized text into rows of exactly max_length token IDs, then forms targets by shifting those rows by one position. For classification, the dataset pads shorter sequences or truncates longer ones to a chosen maximum so batches stay rectangular. section 2.6 section 6.3

Used here to control these data-shaping steps:

  • Creates fixed-size input chunks for each dataset row. section 2.6
  • Defines the target window by shifting inputs one token ahead. section 2.6
  • Lets batches stack cleanly in PyTorch DataLoader. section 2.6 section 6.3
  • Caps or matches sequence length in classification data preparation. section 6.3

Sliding-window batch example

With max_length set to 4, one batch contains input IDs [40, 367, 2885, 1464] and targets [367, 2885, 1464, 1807]. The next batch shifts forward by one token when stride is 1. section 2.6

Padding and truncation example

A classification dataset can set max_length from the longest training sequence, then truncate validation and test examples to that length and pad shorter ones with a pad token. section 6.3

How Build a Large Language Model (From Scratch) covers it

How deep it goes
Explains slicing, shifting, padding, and truncation, but stops short of model internals beyond batch shapes.
Examples
Uses token-ID windows, shifted next-token targets, and padded text-message batches as concrete shapes.
What it assumes
Assumes basic tokenization and tensor batching; introduces Dataset and DataLoader usage from the ground up.
What you can do after
Apply it to build rectangular token batches or padded classification batches with consistent sequence length.
How it connects
Connects to Dataset, DataLoader, stride, padding token, tokenization, context length, and batch size.

Also explained in

Compare how 7 books explain token limit →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Large Language Model (From Scratch). The section links point to the parts of the book it draws on.