ExuluTokenizer is a class that wraps tiktoken to provide token counting, encoding, and decoding for any model supported by the tiktoken registry. It is used internally by IMP’s chunkers and context pipeline to enforce chunk-size budgets and manage context-window limits.
You receive an ExuluTokenizer instance pre-configured for your context’s embedding model via the chunker infrastructure. You can also instantiate it directly when building custom chunkers or tooling that needs precise token counts.
Constructor
create(modelName) before encoding or decoding.
API surface
create
A model name from tiktoken’s
model_to_encoding.json (e.g. "gpt-4o", "gpt-4", "claude-3-opus-20240229"). TokenizerModelName is the union of all keys in that file.The initialized
Tiktoken encoder.encode
create.
countTokens
text. Equivalent to encode(text).length.
countTokensBatch
decode
decodeBatch
free
Example
ExuluTokenizer is internal to the chunker infrastructure and is not exported from @exulu/backend. The built-in chunkers (ExuluChunkers.sentence, ExuluChunkers.recursive, etc.) create and manage a tokenizer instance internally — you do not instantiate ExuluTokenizer directly.
If you need token counting inside a custom chunker, use any tiktoken-compatible library directly. For example, using the tiktoken package:
ChunkerOperation signature and how to register a custom chunker on a context.
Notes
ExuluTokenizeris used by the base chunker class to enforcemaxChunkSizetoken budgets. It is an internal implementation detail and is not part of the public@exulu/backendAPI.- The
encoderproperty isnullbeforecreateis called. Callingencode,decode, orcountTokensbefore initialization throws"Tokenizer not initialized". - Loading the BPE data is the expensive step — it is logged with timing. Memoization on the instance means the cost is paid once per
ExuluTokenizerinstance.
Related
- ExuluChunkers — introduction: chunkers use
ExuluTokenizerto enforce chunk-size budgets.