02.02

Tokens, chat templates, and sampling

skillmaxingInference EngineeringTokens, chat templates, and sampling
Visual lesson

Tokens, chat templates, and sampling

Included with Pro · 5:13
Lessons · 1,174 words

A model never sees text. On the way in, a tokenizer cuts a string into pieces from a fixed vocabulary and replaces each piece with an integer. On the way out, the model produces one score, called a logit, for every entry in that vocabulary, and a sampler turns those scores into a single chosen id, which is turned back into text. Everything the serving engine counts, bills and caches is measured in those ids.

Text in, ids through, text out
Figure 1Text in, ids through, text outEverything an engine counts, bills and caches is measured in token ids.

Between the two sits the chat template: the exact text, special markers included, that wraps a list of messages into one string. It is easy to treat as plumbing. It is actually part of the prompt, and getting it wrong breaks a model in ways that look like the model being bad.

Pro

Continue reading

Pro unlocks every video lesson, the full notes, worked sizing and cost calculations, and runnable configs across the Inference Engineering course, from GPUs and kernels to quantization, speculative decoding, disaggregation and production autoscaling.

Unlock full access to
Python with AIInference EngineeringAI researchComplete system design
30% off with LAUNCH30first payment
Buy now
View all plans ·Already Pro? Sign in