Claude 3 tokenizer can be reverse-engineered via generation streaming
Claude 3’s tokenizer can be accurately reconstructed by observing the model’s token-by-token generation stream.
AI/ML SPY Lab (ETH Zurich) 15 Mar 2024 Tooling Prototype evidence
Published as “The Worst (But Only) Claude 3 Tokenizer”
Why it matters
Provides researchers with a usable, though inefficient, approximation of Claude 3 tokenization despite the lack of an official tokenizer.
Summary
The authors observed that Anthropic’s Claude 3 streams output one token at a time. By prompting the model to repeat known strings and limiting output length, they matched streamed chunks to individual tokens and verified token counts against the API usage report. They built a Python implementation that captures these streams, reconstructs tokens, and aggregates a vocabulary. Their experiments suggest the reconstructed tokenizer is a close approximation, though it is inefficient and its fidelity is not formally validated.
What they found 2
- Claude streams one token at a time, allowing token reconstruction.
“It turns out that Anthropic serves one token at a time!”
- API token usage reports equal text tokens plus three.
“We find that the number of tokens reported by the API is always number of text tokens + 3”
Caveats 5
- It is unclear how faithful this tokenization will be, but our experiments suggest this is very likely a close approximation.
- This is probably the least efficient implementation of a tokenizer (but it is also the only publicly available one that we know of!).
- The implementation is inefficient compared to standard tokenizers.
- The fidelity of the reverse-engineered tokenizer is not formally validated.
- Only a single model family (Claude 3) was examined.
What’s new here 4
- Demonstrated that Claude 3 streams one token per network delta, enabling token reconstruction.
- Discovered that the API token count equals the number of text tokens plus three, likely accounting for special tokens.
- Released a Python implementation that tokenizes text by replaying the generation stream.
- Provided a method for the community to share and merge extracted token vocabularies.