A token is a unit produced by a model's tokenizer, not a character or word. Different tokenizers split the same text differently. Chat templates also insert role markers and separators. To make a real token-budget claim, count the complete serialized request with the intended tokenizer and template, then add the reserved output allowance.
If a declared window is 512 tokens, input measures 390, and output reserve is 64, total use is 454 with 58 spare. Counting only message content can omit formatting overhead. Character counts are acceptable for an explicitly labeled toy ledger, but a character-to-token guess is not an exact model measurement.
The optional real-tokenizer path uses the Transformers library, a package providing model and tokenizer interfaces, without loading model weights. In your exercise environment install it with python -m pip install transformers. Download a small tokenizer artifact with this code:
from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(
'HuggingFaceTB/SmolLM2-135M-Instruct', trust_remote_code=False)
tokenizer.save_pretrained('tokenizer')
This needs internet and disk space for tokenizer files, normally no account. The lab then uses that local directory. Chat-template documentation explains serialization. Its count is exact for that saved template, not automatically for a different server template.