Expand description
Token counting via tiktoken BPE tokenizer.
All token estimation goes through tiktoken’s cl100k_base encoding
(GPT-4, GPT-3.5-turbo). BPE tokenizers are similar enough across providers
that this gives reasonable accuracy for Anthropic, Gemini, and others.
Provider-reported exact token counts (from API responses) should always be preferred when available. This module is for pre-call budget estimation and offline token sizing where no provider response exists yet.
Constants§
- CHARS_
PER_ TOKEN - Approximate bytes per token used by the byte-length heuristic.
Functions§
- estimate_
tokens - Count the number of tokens in
textusing tiktoken BPE. - format_
token_ count - Format a token count with a compact decimal suffix for transcript labels.
- heuristic_
tokens_ from_ byte_ len - Approximate token count from a byte length without running the tokenizer.
- heuristic_
tokens_ from_ byte_ len_ u64 u64twin ofheuristic_tokens_from_byte_lenfor lifetime byte counters that never narrow tousize.- truncate_
to_ tokens - Truncate
textto at mostmax_tokenstokens.