r/LocalLLaMA • u/mukel90 • 22h ago
Resources Tok'n'Roll: Token-perfect LLM tokenizers for Python, written 100% in Java 🤯
Tok'n'Roll is the tokenization substrate of Quixotic AI. It's written 100% in Java (no Unsafe). Now available to Python too.
pip install toknroll
import toknroll
# fetches the tokenizer only, never the full weights
tokenizer = toknroll.load("Qwen/Qwen3-0.6B")
ids = tokenizer.encode("Hello, world!") # array('i', [9707, 11, 1879, 0])
tokenizer.decode(ids) # 'Hello, world!'
tokenizer.count_tokens("Hello, world!") # 4What?
- Token-perfect. Verified against Hugging Face tokenizers, llama.cpp and tiktoken on popular model families: Gemma 4, Mistral, Qwen, gpt-oss, Llama, DeepSeek, Kimi, GLM, Granite, MiniMax ...
- Performant. Written following "fintech Java" mantras, competitive with Hugging Face tokenizers 1.0rc5 and tiktoken.
- "Light". No JVM required, "only" a ~5.6 MB wheel with no dependencies; just
curlfor Hub downloads. - Hugging Face and GGUF tokenizers, local or remote. Supports a verified subset of popular models.
How?
Tok'n'Roll is compiled with GraalVM Native Image into a shared library and wrapped for Python.
Supports Linux x86-64-v3/aarch64, Windows x86-64-v3 and macOS arm64.
Why?
The popular Python tokenization libraries are thin wrappers around Rust codebases, nothing wrong with that. But some overdo unsafe, which I find hard to justify for tokenizers. Tok'n'Roll has none, by design, and it's still fast.
Mixing Python and Java turned out much better than I anticipated, both to develop and to use. Only possible thanks to GraalVM Native Image.
Source code: Tok'n'Roll
Quixotic stands on the shoulders of llama.cpp, Hugging Face, tiktoken and the whole open model community. Thank you!
PS: Author here. Happy to answer any questions and brew some technical discussions!
