Skip to content
Inference Academy

Tokenizers and chat templates for real models

What an engine must get exactly right to run a real instruction-tuned model: loading the tokenizer from GGUF or tokenizer.json, special tokens, BOS handling, chat templates as Jinja programs, the generation prompt, stop tokens, and incremental detokenisation for streaming. With a runnable demo of the two most common bugs.

By

15 min · verified 2026-11Pro
Share

The forward pass turns token ids into logits. Everything around it, turning a conversation into exactly the ids the model was trained on and turning sampled ids back into text, is where engines most often go quietly wrong. A model with a mangled prompt still produces fluent text; it is just worse. This lesson covers the pieces an engine must get exactly right.

#Loading the tokenizer

A real model ships its tokenizer with the weights:

  • GGUF stores the vocabulary, merges or scores, token types and special-token ids in its metadata (tokenizer.ggml.* keys), plus the chat template (loading GGUF).
  • Hugging Face checkpoints ship tokenizer.json (the full tokenizer, including the pre-tokenisation regex and merges), tokenizer_config.json (special tokens, chat template) and generation_config.json (stop tokens, default sampling).

Most current models use byte-level BPE (BPE from scratch); some use SentencePiece. The algorithm is the easy part. The details that break engines are the pre-tokenisation regex (how text is split before merges), special tokens that must never be split, and normalisation (some tokenizers add a leading space).

Test your tokenizer by encoding a few hundred varied strings (code, emoji, other scripts, whitespace runs) and comparing ids with the reference implementation. Any mismatch is a bug.

Pro content

Keep reading with Inference Academy Pro

Unlock the rest of this lesson, plus every advanced lesson, hands-on lab, the larger playground models and completion certificates.

Still to come in this lesson

  1. Chat templates
  2. Running Jinja in an engine
  3. Detokenisation for streaming
  4. A checklist for a new model
  5. Exercises

You will be able to answer

  • A model's answers are noticeably worse in your engine than in the reference implementation, with no errors. What is a likely cause?
  • What is the 'generation prompt'?
  • Why must an engine know the template's end-of-turn token as a stop token?
  • Where do chat templates usually live?
  • Why can't a streaming server decode each token to text independently?