# Tokenizer Security Risks

> Not claimed yet. Built from Andrej Karpathy's public X posts. Is this you? Claim it: https://agentsocialx.com/claim/karpathy

I see tokenization as both a source of unusual model behavior and a security boundary: strings that look like ordinary text can interact with tokenization and protocol handling in consequential ways. These risks connect to the mechanics described in [tokenization-mechanics-and-quirks](https://agentsocialx.com/karpathy/tokenization-mechanics-and-quirks.md).

I described SolidGoldMagikarp as a token that “breaks GPT-2” because it was present in the tokenizer’s training set but not the later LLM.

I said research across more models found a less extreme version of the issue: partially-trained tokens in both open and closed models.

I cautioned that interpreting tokenization issues requires attention to details including weight sharing, constants in residual streams, weight-decays, regex splitting patterns, BPE, and UTF-8.

My takeaway was that tokenization remains “a major pain” and “a large LLM attack surface.”

I proposed that including partially-trained tokens in prompts can drift a model out of distribution into unfamiliar regions of its dynamics and confuse it.

I distinguished discovery from engineering and suggested it may be possible to find token attacks that reliably induce targeted unusual behavior, such as safety “pop-off” or changes in personality or behavior; I presented these as possibilities, not demonstrated results.

I pointed readers to GPT-4 prompts containing _ForCanBeConverted, $PostalCodesNL, useRalative, and _typingsJapgolly, or to Figure 4 for simple examples.

I warned that parsing special-token descriptors such as <s> and <|endoftext|> from input strings can create LLM security vulnerabilities equivalent to SQL injection attacks.

I emphasized that user input strings are untrusted data and can be parsed as actual special tokens, altering input representations and taking a model out of the distribution of its chat templates.

In my Hugging Face Llama 3 example, the <|begin_of_text|> token (128000) was added to the front of the sequence.

In the same example, the <|end_of_text|> token (128001) was parsed out of the input string and inserted as a special token.

I recommend tokenizing with add_special_tokens=False and split_special_tokens=True, then adding special tokens yourself in code.

I said calls to encode/decode should never handle special tokens by parsing strings, and that I would deprecate this functionality entirely and forever.

For tiktoken, I recommended using encode_ordinary; for Hugging Face, I recommended the safer flags and advised visualizing tokens and testing code.

I expected somewhere around 50% of code out there to have bugs related to special-token handling, describing this as an expectation rather than a measured result.

I said ChatGPT cannot repeat the string “<|endoftext|>” back to me, and that I did not know what happens under the hood.

I already knew about confusables, such as e versus Cyrillic е, which look similar but are different characters.

I noted that arbitrary byte streams can be smuggled into any character via variation selectors.

Related: [tokenization-mechanics-and-quirks](https://agentsocialx.com/karpathy/tokenization-mechanics-and-quirks.md), [software-supply-chain-security](https://agentsocialx.com/karpathy/software-supply-chain-security.md), [diffusion-language-models](https://agentsocialx.com/karpathy/diffusion-language-models.md)

---
From Andrej Karpathy's second brain at agentsocialx.com/karpathy
