liveBook · Manning

Context window

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

The number of preceding tokens the model conditions on when predicting the next token.

A context window is the maximum number of tokens an LLM can use as input while predicting the next token. The context_length setting defines this supported input size: longer text must be truncated. In a GPT pipeline, token IDs are embedded, positional embeddings are added, and the resulting sequence enters the model. section 2.8 section 5.1.1

Context size affects configuration, computation, and generation:

  • Training can use a shorter window to reduce computational demands; a 256-token setting enables training on a standard laptop. section 5.1.1
  • A GPT-2 model with 124 million parameters was originally configured for up to 1,024 tokens, while pretrained weights can later be loaded with that context length. section 5.1.1
  • Positional embeddings are created for every supported position, so their length matches the configured context window. section 2.8

A configured generation window

Set GPT_CONFIG_124M["context_length"] to 256 and pass it as context_size to generate_text_simple. The model receives token IDs, produces logits, and decodes generated token IDs back into text. section 5.1.1

import torch
from chapter04 import GPTModel

GPT_CONFIG_124M = {
    "vocab_size": 50257,
    "context_length": 256,    #1
    "emb_dim": 768,
    "n_heads": 12,
    "n_layers": 12, 
    "drop_rate": 0.1,       #2
    "qkv_bias": False
}
torch.manual_seed(123)
model = GPTModel(GPT_CONFIG_124M)
model.eval()

Inspect the GPT configuration showing context_length alongside vocabulary size, embedding width, attention heads, and layers. section 5.1.1

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

How deep it goes
Explains configuration, tensor-size implications, and truncation, but does not detail attention internals.
Examples
Demonstrates 256-token training configurations, 1,024-token GPT-2 settings, and 8 × 4 token batches.
What it assumes
Assumes tokenization basics and introduces how token IDs become embeddings with positional information.
What you can do after
Operate and configure GPT context sizes for training and text generation.
How it connects
Connects context length with token IDs, positional embeddings, GPT models, logits, and text generation.

Also explained in

Compare how 22 books explain context window →

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.