Skip to main content
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

Creates an uninitialized tokenizer. Call create(modelName) before encoding or decoding.

API surface

create

Loads the BPE (byte-pair encoding) data for the given model and initializes the encoder. Memoized — subsequent calls with the same instance return the cached encoder.
modelName
TokenizerModelName
required
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.
return
Promise<Tiktoken>
The initialized Tiktoken encoder.

encode

Encodes a string into a token ID array. Throws when the tokenizer has not been initialized via create.

countTokens

Returns the number of tokens in text. Equivalent to encode(text).length.

countTokensBatch

Counts tokens for multiple strings. Returns an array of counts in the same order as the input.

decode

Decodes a token ID array back to a string.

decodeBatch

Decodes multiple token sequences in parallel.

free

Frees the internal Tiktoken encoder’s WASM memory. Call when you are done with the tokenizer in long-running processes to avoid memory leaks.

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:
See ExuluChunkers — API reference for the full ChunkerOperation signature and how to register a custom chunker on a context.

Notes

  • ExuluTokenizer is used by the base chunker class to enforce maxChunkSize token budgets. It is an internal implementation detail and is not part of the public @exulu/backend API.
  • The encoder property is null before create is called. Calling encode, decode, or countTokens before 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 ExuluTokenizer instance.