Answers for LLM inference engineers

GGUF token ids differ from Hugging Face transformers? Find the cause

A correctly converted GGUF normally gives the same token ids as the Hugging Face tokenizer. llama.cpp b12899 gave the Hugging Face ids on all 708 reference strings of the 15 vocabularies in its own test set. When your ids differ, the cause is in the GGUF header or in the build, and it is usually one of seven things: a different vocabulary, a special token typed NORMAL, a missing tokenizer.ggml.pre, a wrong pre-tokenizer rule, NFC normalization that llama.cpp skips for BPE, a different BOS setting, or whitespace handling around special tokens. tokcheck.py tokenizes the same strings on both sides, shows the first token that differs, and names the cause.

Published 2026-12-01 · llama-tokenize built from llama.cpp b12899 (2026-12-01) and b12738 (2026-11-25), CPU. References: tokenizers 0.23.3 with transformers 5.24.1, and the slow SentencePiece tokenizer of transformers 4.57.6. The Hugging Face tokenizers were rebuilt from llama.cpp's vocabulary files, because the machine could not reach huggingface.co (details under How this was measured). Issue states were read on 2026-12-01 · tokcheck.py 1.0

Why does a GGUF tokenize differently from the Hugging Face tokenizer?

Because the GGUF header says something different from the tokenizer files, or because the llama.cpp build implements one rule differently. The table lists the seven causes in the order to rule them out; the first column is the name tokcheck.py prints.

CauseWhat you seeWhyFix
vocab
The vocabulary is not the same
Different vocabulary size, or a different token text at the same id. Everything after it differs.The GGUF was converted from another revision, or from a fine-tune or merge that added tokens.Reconvert from the exact model directory you compare against.
special-type
A special token is typed NORMAL
The token's text is split into pieces (<, |, im, _start ...) even with special-token parsing on. Chat prompts reach the model as fragments, with no error.llama.cpp only matches a token as a whole when its tokenizer.ggml.token_type is CONTROL (3), USER_DEFINED (4) or UNKNOWN (2). The converter writes NORMAL (1) when the token is missing from the added tokens of the directory it reads.Reconvert from a directory whose tokenizer.json / tokenizer_config.json still list the token as an added token, or rewrite the type entries.
pre-missing
No tokenizer.ggml.pre
Contractions, runs of spaces, digits and punctuation split differently. The log says missing pre-tokenizer type, using: 'default' and GENERATION QUALITY WILL BE DEGRADED!.A BPE GGUF made by an old or third-party converter. llama.cpp falls back to the default rule.Reconvert, or write the name into a copy: gguf-new-metadata in.gguf out.gguf --pre-tokenizer NAME.
pre-tokenizer
Wrong pre-tokenizer name, or a rule that differs in this build
Plain text without special tokens differs, usually only one kind: digits (1234567), whitespace before punctuation, or letters with combining marks.The converter picks the name from a hash of the Hugging Face tokenizer's output (chkhsh). A hand-mapped hash, a newer transformers that tokenizes the probe text differently, or a regex bug in llama.cpp all end here.Look up the chkhsh that tokcheck prints, fix the name in a copy, and update llama.cpp (see the table of fixes below).
unicode-normalization
NFC normalization is skipped
Only text with decomposed characters differs: e + U+0301 stays two tokens where Hugging Face first composes it to é.The tokenizer.json has an NFC normalizer. llama.cpp reads tokenizer.ggml.normalizer.nfc for Unigram vocabularies only. BPE text is tokenized as given, except pre = raon, where NFC is hard-coded (b12758 and later).Normalize prompts before sending: unicodedata.normalize("NFC", text).
bos-eos
BOS / EOS are added differently
The ids differ only at the start or the end.tokenizer.ggml.add_bos_token is missing or wrong, or the pre-tokenizer name brings its own default: llama-bpe turns BOS on.Set add_bos_token in the header; do not add BOS in the template as well.
space-after-special / normalizer
Whitespace around special tokens, or the SentencePiece space prefix
Ids differ right after a special token, or every string differs in its first token.Hugging Face decides per token (lstrip / rstrip) and per tokenizer (legacy, slow or fast). GGUF has no field for the per-token flags; llama.cpp hard-codes them for a few model families and uses tokenizer.ggml.add_space_prefix for the prefix.Compare with the chat template applied, against the Hugging Face variant the model was trained with (--slow).

How do I check whether my GGUF tokenizes like the original model?

Run tokcheck.py with the GGUF, the original tokenizer and one way to reach llama.cpp. It exits 0 when every string gives the same ids, 1 on a mismatch, 2 when it could not run.

pip install transformers        # or only: pip install tokenizers, when --hf is a tokenizer.json
curl -O https://erimaki-kobo.github.io/en/gguf-tokenizer-parity-check/tokcheck.py

# with the llama-tokenize binary from your llama.cpp build
python tokcheck.py --gguf model.gguf --hf ./original-model --llama-tokenize ./build/bin/llama-tokenize

# with a running llama-server that has the GGUF loaded
python tokcheck.py --gguf model.gguf --hf Qwen/Qwen3-0.6B --server http://127.0.0.1:8080

# header only (no llama.cpp needed): vocabulary, token types, chkhsh
python tokcheck.py --gguf model.gguf --hf ./original-model --static

It tokenizes 37 fixed strings (words, whitespace runs, digits, code, non-Latin scripts and emoji), up to six of the model's own added tokens in 8 positions each (alone, glued to text, followed by one space, two spaces, a newline), and two strings with BOS/EOS enabled. Add your own prompts with --text-file. The GGUF reader is built in; the script only needs transformers or tokenizers. This is the output for a Qwen2 vocabulary whose <|im_start|> was changed to NORMAL:

tokcheck 1.0
GGUF  ./qwen-im-start-normal.gguf
HF    ./refs/qwen2 (transformers)
  general.name           qwen2
  tokenizer.ggml.model   gpt2
  tokenizer.ggml.pre     qwen2
  gguf_vocab             151936
  hf_vocab               151936
  added_tokens           7
  chkhsh                 e636dc30a262dcc0d8c323492e32ae2b70728f4df7dfe9737d9f920a282b8aea
[FAIL] header: 1 added token(s) are not CONTROL/USER_DEFINED in the GGUF: 151644 '<|im_start|>'=NORMAL
[WARN] header: eos_token_id: GGUF 151643, Hugging Face 151645

engine: llama-tokenize, 79 strings
  words      6/6   same
  space     11/11  same
  digits     5/5   same
  code       5/5   same
  unicode    9/10  same   <-- 1 differ
      first: 'é vs é'  (differs at token 0, looks like: unicode-normalization)
        llama.cpp 68:'e' 53839:'Ìģ' 6165:'Ġvs' 3958:'Ġé'
        HF        963:'é' 6165:'Ġvs' 3958:'Ġé'
  special   32/40  same   <-- 8 differ
      first: '<|im_start|>'  (differs at token 0, looks like: special-type)
        llama.cpp 27:'<' 91:'|' 318:'im' 4906:'_start' 91:'|' 29:'>'
        HF        151644:'<|im_start|>'
  bos-eos    2/2   same

RESULT: MISMATCH - likely causes, most specific first (fix the first one, then run again):
  * special-type: A token the Hugging Face tokenizer treats as an added/special token is typed NORMAL in the GGUF, so llama.cpp never matches it as one token and splits its text into pieces. Chat templates then silently feed the model fragments. Fix the token_type entries (3 = CONTROL) or reconvert from a model directory whose tokenizer_config.json / tokenizer.json still lists the token under added tokens.
  * unicode-normalization: The Hugging Face tokenizer normalises text to NFC before tokenizing (tokenizer.json "normalizer"), llama.cpp tokenizes the bytes as given. Only text with decomposed characters (a letter followed by a combining mark) is affected, and llama.cpp gives the Hugging Face ids once the input is NFC. Normalise prompts yourself: unicodedata.normalize('NFC', text).

Tested here with --llama-tokenize (all results on this page). The --server path was run against a small relay that answers /tokenize with llama-tokenize's ids, not against llama-server itself; the request fields (content, add_special, parse_special) were checked against the server source in b12899. The llama-cpp-python path was not run. --slow loads the slow SentencePiece tokenizer and needs transformers 4.x.

What does each cause look like in the token ids?

We injected one fault at a time into copies of llama.cpp's vocabulary files and ran tokcheck.py. Each fault moves a different group of strings, which is how the script tells them apart. b12899 and b12738 gave the same result in all 14 cases. In every Qwen2 case four placeholder ids (151665 to 151668) carry the names Qwen3 gives them, so that the vocabulary has added tokens that are not special.

GGUF headerStrings that differFirst difference with the main causetokcheck.py says
Qwen2 vocabulary, nothing changedunicode 1 of 10e\u0301 vs \xe9
llama.cpp 68, 53839, 6165, 3958
HF 963, 6165, 3958
unicode-normalization
Qwen2, tokenizer.ggml.pre removedwords 1 of 6, space 5 of 11, digits 1 of 5, code 5 of 5, unicode 1 of 10I've been told he's there, we'll see
llama.cpp 40, 6, 586, 1012, 3229, 566, 6, 82, …
HF 40, 3003, 1012, 3229, 566, 594, 1052, 11, …
pre-missing, unicode-normalization
Qwen2, pre = defaultwords 1 of 6, space 5 of 11, digits 1 of 5, code 5 of 5, unicode 1 of 10I've been told he's there, we'll see
llama.cpp 40, 6, 586, 1012, 3229, 566, 6, 82, …
HF 40, 3003, 1012, 3229, 566, 594, 1052, 11, …
pre-tokenizer, unicode-normalization
Qwen2, pre = llama-bpeunicode 1 of 10, bos-eos 2 of 2Hello world
llama.cpp 151643, 9707, 1879
HF 9707, 1879
unicode-normalization, bos-eos
Qwen2, <|im_start|> typed NORMALunicode 1 of 10, special 8 of 40<|im_start|>
llama.cpp 27, 91, 318, 4906, 91, 29
HF 151644
special-type, unicode-normalization
Qwen2, <think> and </think> typed NORMALunicode 1 of 10, special 16 of 48<think>
llama.cpp 13708, 766, 29
HF 151667
special-type, unicode-normalization
Qwen2, add_bos_token = trueunicode 1 of 10, bos-eos 2 of 2Hello world
llama.cpp 151643, 9707, 1879
HF 9707, 1879
unicode-normalization, bos-eos
Qwen2, text of one token changedwords 2 of 6, unicode 1 of 10, bos-eos 1 of 2Hello world
llama.cpp 39, 68, 75, 75, 78, 1879
HF 9707, 1879
vocab
Llama 3 vocabulary, nothing changednonesame ids
Llama 3, add_bos_token = truenonesame ids
Llama 3, add_bos_token = true and pre = smaug-bpenonesame ids
Llama 3, add_bos_token = true and pre = qwen2digits 4 of 51234567
llama.cpp 16, 17, 18, 19, 20, 21, 22
HF 4513, 10961, 22
pre-tokenizer
Llama 2 SentencePiece vocabulary, nothing changednonesame ids
Llama 2 SentencePiece, add_space_prefix = falsewords 6 of 6, space 11 of 11, digits 5 of 5, code 5 of 5, unicode 10 of 10, special 21 of 24, bos-eos 2 of 2Hello world
llama.cpp 10994, 3186
HF 15043, 3186
normalizer, space-after-special

Four things in this table are easy to miss:

Which llama.cpp builds fixed known tokenizer mismatches?

Three of the reports below are fixed in current builds and one fix is not merged yet. Three more are in the GGUF or in the converter's environment, so updating llama.cpp does not help, and one we could not reproduce. Build numbers were read from the llama.cpp git history (first tag that contains the fix commit).

Model or tokenizerMismatchReportStatus on 2026-12-01
Gemma 4\n\n became two tokens (107, 107); Hugging Face gives one (108)PR #21343Fixed in b8648 (2026-04-03). The GGUF does not change; update llama.cpp.
gpt-oss, Mistral NeMo and others (pre = gpt-4o, tekken, llama4, kanana2, talkie, minimax-m2)Combining marks are split off their letters: Hindi दी gives [2587, 840] on gpt-oss, Hugging Face [25589]. The report counts 271 of 7,248 test strings.#30787The issue is still open, but commit d8d92973c (PR #30845) is in b11753 (2026-10-20) and later. Update llama.cpp and test again.
Inkling 2def f(*args, **kwargs): differs because ignore_merges was off#32798Fixed in b12641 (2026-11-22) by PR #32799.
Falcon-H1 (pre = falcon-h1)Whitespace runs before punctuation: a  # gives [588, 731, 731, 526], Hugging Face [588, 767, 526]. The report counts 190 of 676 non-empty lines.#32953Not fixed in b12899: the fix, PR #33223, is not in the history yet.
Gemma 3 exported by Unsloth<start_of_turn> and <end_of_turn> (ids 105, 106) written as NORMAL, so the chat markers are split into piecesunsloth #5070Reported with unsloth 2026.4.5 and transformers 5.5.0; the maintainers prepared a fix on a branch. Re-export with a version that includes it, or set the two types to 3.
BitNet b1.58 2B4T official GGUFNo tokenizer.ggml.pre: a test sentence is 31 tokens instead of 29BitNet #684Open. The report's fix is tokenizer.ggml.pre = llama-bpe.
DeepSeek and R1-Distill models converted with transformers 5.xThe pre-tokenizer hash changes, so the converter crashes or picks another name (R1-Distill-Llama as smaug-bpe, DeepSeek-V3 as joyai-llm)PR #31576 (draft)Not merged as of b12899. llama.cpp's requirements pin transformers==4.57.6; convert with that version.
Llama 2 / TinyLlama / Mistral 7B (SentencePiece)One space after a special token: </s> XML reported as [2, 259, 9165], Hugging Face [2, 29871, 6560]#23840We could not reproduce it with llama.cpp's own Llama 2 vocabulary file: b12738 and b12899 both give [2, 29871, 6560]. The report used a 2024 TinyLlama GGUF that we could not download here.

Token ids in this table are quoted from the reports; we did not have those models. For a pre-tokenizer hash the converter does not know (BPE pre-tokenizer was not recognized), see the chkhsh and pre-tokenizer lookup.

How do I compare the two tokenizers by hand?

Give both sides the same bytes, switch BOS off on both, and keep special-token parsing on. These four details produce false mismatches (measured on b12899):

printf '%s' "<|im_start|>user" > t.txt
llama-tokenize -m model.gguf -f t.txt --ids --no-bos --no-escape --log-disable
python -c "from transformers import AutoTokenizer as A; print(A.from_pretrained('./original-model').encode(open('t.txt').read(), add_special_tokens=False))"

How was this measured?

Everything ran on CPU with the real llama.cpp tokenizer code, and no model weights were involved. The method has three parts.

Not measured: real model files from the Hub, llama-server itself, llama-cpp-python, Ollama, and any effect on output quality. Scripts: make_refs.py, check_refs.py, gguf_patch.py, run_matrix.py. Raw results: results.jsonl, inp_out.txt.

Questions people ask about GGUF tokenizer mismatches

Why does my GGUF give different token ids than Hugging Face transformers?
A correct conversion normally gives the same ids: llama.cpp b12899 matched the Hugging Face ids on all 708 reference strings of the 15 vocabularies in its test set. When yours differ, the usual causes are a special token typed NORMAL in the GGUF, a missing or wrong tokenizer.ggml.pre, a BOS setting that differs, NFC normalization that llama.cpp does not apply to BPE vocabularies, or a llama.cpp build older than a tokenizer fix.
How do I compare llama.cpp tokenization with the Hugging Face tokenizer?
Tokenize the same strings on both sides with special-token parsing on and BOS handled the same way, then find the first id that differs. tokcheck.py does this through llama-tokenize, llama-server /tokenize or llama-cpp-python (only the llama-tokenize path was tested against real llama.cpp), reads the GGUF header, and names the likely cause. By hand, use llama-tokenize -m model.gguf -f text.txt --ids --no-bos --no-escape and tokenizer.encode(text, add_special_tokens=False).
Why is <|im_start|> or <start_of_turn> split into several tokens in llama.cpp?
Its token type in the GGUF is NORMAL. llama.cpp matches a token as a whole only when tokenizer.ggml.token_type is CONTROL, USER_DEFINED or UNKNOWN. In our test, changing <|im_start|> to NORMAL split it into six tokens in every prompt that contained it, with no warning. Reconvert from a model directory that still lists the token among its added tokens.
Does --override-kv tokenizer.ggml.pre work with llama-tokenize?
No. In b12899 llama-tokenize accepts --override-kv but loads the model with default parameters, so the override is ignored and the ids do not change. To test another pre-tokenizer name, write it into a copy of the file with gguf-new-metadata in.gguf out.gguf --pre-tokenizer NAME; in our test that restored the Hugging Face ids.
Does llama.cpp apply the tokenizer's NFC normalizer?
Only for Unigram vocabularies, through the tokenizer.ggml.normalizer.nfc key (b11612 and later), and for the BPE pre-tokenizer raon, where it is hard-coded (b12758 and later). Other BPE vocabularies are tokenized as given. With the Qwen2 vocabulary, e followed by U+0301 gave [68, 53839] in llama.cpp and [963] on the Hugging Face side, where Qwen2Tokenizer applies NFC; after normalizing the input to NFC, llama.cpp gave the same ids.
Does a tokenizer mismatch change output quality?
It can. Two reports measured it: with the BitNet b1.58 2B4T GGUF that lacks tokenizer.ggml.pre, WikiText-2 perplexity was 16.74 against 14.963 with the pre-tokenizer set (BitNet issue 684), and llama.cpp issue 7476 measured about 15.5 against 6.2 on an old TinyLlama GGUF. We did not measure quality ourselves.

Sources