Lion Cipher Lab

Train a BPE tokenizer and measure how it fragments IOCs in your context budget

Why it matters

Every language model reads and writes TOKENS, not characters, and you pay for, wait for and run out of context in tokens. The tokenizer that turns text into tokens was trained on ordinary text, and security text is not ordinary: IP addresses, hashes, Windows paths, base64 and defanged URLs break into many small pieces. In this lab you build the tokenizer yourself and measure it. A tokenizer trained on ordinary prose spent 119 tokens per log line, so only 68 log lines fit in an 8,192-token window; one trained on logs fitted 128 - and then needed almost three times as many tokens for a plain…

What you'll learn

Steps in this lab

  1. The words first: tokens, byte-pair encoding, merges, pre-tokenisation, characters per token, IOCs - and what is real here and what is generated
  2. Open a terminal and make a scratch folder (Windows, macOS, Linux)
  3. Build the corpora: ordinary prose, security logs and report lines, and 300 held-out indicators of each of ten types
  4. Write the tokenizer: a pre-tokenisation regex, the merge-training loop and the encoder (bpe.py)
  5. Train four tokenizers on the same 600-merge budget, so only the data and the splitting rule differ
  6. Measure: characters per token, tokens per IOC type, and how many log lines fit in a context window
  7. Read the budget: a general tokenizer fits half as many log lines, and that is a bill and a blind spot
  8. Honest negative 1: training on in-domain logs helped logs and wrecked ordinary prose
  9. Measure split instability: the same IOC in a different place, after a one-character change, and after defanging (stability.py)
  10. Read the instability: the space in front matters more than the typo, and the folk claim is half wrong
  11. The fix, measured: IOC-aware pre-tokenisation - what it bought and what it cost
  12. Honest negative 3: hashes cannot be compressed, and one line of maths says why (hash_ceiling.py)
  13. Why this, not that: domain tokenizer, IOC-aware pre-split, or keep indicators out of the prompt
  14. When NOT to bother with any of this
  15. The way a pro does it differently, and what to ship
  16. OPTIONAL appendix: the same measurements with a production tokenizer (tiktoken)

Open the full interactive lab →