Courses / System Design Interview / Chapter 24
Module 5 · AI Building Blocks and Review · Chapter 24 of 49
AI Building Blocks: Inference, Embeddings, Retrieval and Agents
Builds on Rate Limiters, Distributed Search and Distributed Task Schedulers. Next up: Putting the Blocks Together: Timed Practice.
Part 1 · Tokens, context windows and what a model call costs
Tokens and the context window
A shop's help page now answers questions with a language model, and each answer costs compute that a database lookup never did. The parts below are the ones every such design reuses.
Tokens are the pieces of text a language model reads and writes, and every call to a model is priced in them. A design that uses a model has to start from how many tokens one call uses.
A tokeniser splits text into tokens: a whole common word, part of a rarer word, a digit group or a punctuation mark. The model works on the numbers that stand for those pieces, not on characters. Most current models use subword tokenisers such as byte-pair encoding. The split depends on the model, so a count made for one model does not carry over to another, and text in some languages or scripts can need more tokens per word than plain English. Count with the model you will run.
The context window is the largest number of tokens the model accepts in one call, with the prompt and the reply counted together. The prompt is everything you send: instructions, retrieved documents, the conversation so far and the new question. The window is limited because the attention step of a transformer compares each token with the others in the window, and in a plain implementation the work of that step grows with the square of the length.