contextcraft.TokenCounter
TokenCounter(encoding: Encoding = 'cl100k_base') -> None
Bases: Handle
Count UTF-8 text exactly with an embedded tokenizer vocabulary.
Use as a context manager or call close when finished. Special-token markers are counted as ordinary text, and embedded NUL bytes are preserved.
Functions:
- close – Release native resources, with repeated calls safely ignored.
- count – Count one UTF-8 string or byte sequence with the selected vocabulary.
- count_batch – Count several texts in one native call, preserving their input order.
Attributes:
- closed (
bool) – Whether native resources have been released.
contextcraft.TokenCounter.close
close() -> None
Release native resources, with repeated calls safely ignored.
contextcraft.TokenCounter.closed
closed: bool
Whether native resources have been released.
contextcraft.TokenCounter.count
count(text: str | bytes) -> int
Count one UTF-8 string or byte sequence with the selected vocabulary.
contextcraft.TokenCounter.count_batch
count_batch(texts: Iterable[str | bytes]) -> tuple[int, ...]
Count several texts in one native call, preserving their input order.