Context Engineering

Layer 4 · Foundations

Context Engineering

The formal layer: attention cost and context length, retrieval math, and the statistics of retrieval quality.

15 min read180 XP

2k tokensrelative compute
8k tokens16
32k tokens64
128k tokens256
Self-attention compute scales quadratically with context length.

Self-attention computes a score between every pair of tokens, so both compute and memory for the attention matrix scale as in sequence length (for a fixed model width). Doubling the context roughly quadruples the attention cost — which is why simply 'stuffing more context in' has a real, nonlinear cost, not just a linear one, and why efficient-attention and KV-cache techniques exist as separate engineering efforts.