Skip to main content

model-tokenizer

Token-accurate support for text splitters. Use it when you need exact token counts for a context window. The same text uses different numbers of tokens depending on the language, and splitting by characters makes the context window hard to fill. Embedding models have limited context, so splitting by tokens fills it as fully as possible. Small language models on devices may also be limited to a 4k or 8k context window.

The package lets langchain-text-splitters and semantic-text-splitter count and split text by the tokens of a real llama.cpp or vLLM model, using the /tokenize and /detokenize endpoints.

Works with a plain llama-server and with routers such as llama-swap or LiteLLM (model is sent in the body and query string so the right backend is selected; a plain server ignores it).

Installation

uv add model-tokenizer
# or
pip install model-tokenizer

langchain-text-splitters

Token-based splitting has two useful forms. ModelTokenizer supports both; no adapter is needed.

1. Pure token windows: split_text_on_tokens

LangChain's generic seam is the Tokenizer dataclass. Build it from ModelTokenizer.encode / decode:

from langchain_text_splitters import Tokenizer, split_text_on_tokens
from modeltokenizer import LlamaCppHttpTransport, ModelTokenizer

tokenizer = ModelTokenizer(
    transport=LlamaCppHttpTransport(
        base_url="http://127.0.0.1:9931",
        model_name="my-model",
    ),
)

langchain_tokenizer = Tokenizer(
    chunk_overlap=32,
    tokens_per_chunk=512,
    encode=tokenizer.encode,
    decode=tokenizer.decode,
)

chunks = split_text_on_tokens(text=document, tokenizer=langchain_tokenizer)

The document is tokenized once (POST /tokenize), then each window is decoded with POST /detokenize, one round trip per emitted chunk.

Pure token windows give a hard token guarantee, but boundaries are token indices, not word or sentence boundaries, so a chunk can start or end mid-word. Overlap is exact in token units.

2. Token-measured semantic splitting: length_function

LangChain's character/recursive splitters accept a custom length function, so they can keep their paragraph/sentence/word boundaries while measuring size in server tokens:

from langchain_text_splitters import RecursiveCharacterTextSplitter

splitter = RecursiveCharacterTextSplitter(
    length_function=tokenizer,   # chunk_size and chunk_overlap are now in tokens
    chunk_size=512,
    chunk_overlap=64,
)
chunks = splitter.split_text(document)

Trade-offs:

  • Many more requests: every candidate split or merge calls tokenizer(text), so expect one POST /tokenize per probe. Token windows tokenize the document only once.
  • Not a hard guarantee: if a single separator unit is larger than chunk_size, LangChain can emit it anyway and logs a warning.
  • Overlap is not exact token overlap: LangChain keeps whole separator units, so the actual overlap can differ from chunk_overlap.

3. Note on TokenTextSplitter

langchain_text_splitters.TokenTextSplitter is hard-wired to tiktoken: it imports tiktoken and loads an OpenAI encoding by name. It has no seam for a remote tokenizer. Use split_text_on_tokens instead.

Recommendation

  • Need exact token windows: split_text_on_tokens.
  • Need semantic boundaries measured in tokens: RecursiveCharacterTextSplitter(length_function=tokenizer, ...).
  • Need Document objects or split_documents: RecursiveCharacterTextSplitter; split_text_on_tokens is only a text function.

See examples/langchain_splitter.py.

semantic-text-splitter

semantic-text-splitter can only integrate with this client through from_callback: ModelTokenizer implements Callable[[str], int], and every size probe becomes one POST /tokenize request. The from_huggingface_tokenizer and from_tiktoken_model constructors do not accept a remote tokenizer object, so they cannot use ModelTokenizer.

from semantic_text_splitter import TextSplitter

splitter = TextSplitter.from_callback(tokenizer, (256, 512))
chunks = splitter.chunks(document)

The same from_callback pattern works with MarkdownSplitter and CodeSplitter. See examples/semantic_splitter.py.

MarkdownSplitter

from semantic_text_splitter import MarkdownSplitter

splitter = MarkdownSplitter.from_callback(tokenizer, (256, 512))
chunks = splitter.chunks(markdown_document)

CodeSplitter

CodeSplitter also needs a tree-sitter language grammar. Install the grammar for your language separately (for example, tree-sitter-python):

import tree_sitter_python
from semantic_text_splitter import CodeSplitter

splitter = CodeSplitter.from_callback(
    tree_sitter_python.language(),
    tokenizer,
    (256, 512),
)
chunks = splitter.chunks(source_code)

For all three splitters, capacity is in the server's tokens, and overlap and trim behave exactly as they do with from_callback on any other sizing callback.

How to use

Examples for both libraries are in the examples/ directory.

from modeltokenizer import LlamaCppHttpTransport, ModelTokenizer

tokenizer = ModelTokenizer(
    transport=LlamaCppHttpTransport(
        base_url="http://127.0.0.1:9931",
        model_name="my-model",          # router model name
        api_key="...",                  # optional Bearer token
        timeout=7200,                   # optional timeout
    ),
)

tokenizer("Hello world!")                # -> 3
tokenizer.encode("Hello world!")         # -> [9906, 1917, 0]
tokenizer.decode([9906, 1917, 0])        # -> "Hello world!"

Development

uv run python -m unittest discover -s tests -v
ruff check src tests
ruff format --check src tests

MIT License

Metadata

Release files for model-tokenizer 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for model-tokenizer 0.1.0
File Size Uploaded Provenance Actions
model_tokenizer-0.1.0.tar.gz 8.0 kB With provenance Details for model_tokenizer-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for model-tokenizer 0.1.0
File Interpreter ABI Platform Actions
model_tokenizer-0.1.0-py3-none-any.whl Python 3 none any Details for model_tokenizer-0.1.0-py3-none-any.whl

Total release size: 17.7 kB

Release files / model_tokenizer-0.1.0.tar.gz

Download URL model_tokenizer-0.1.0.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3e1d6457992cfb1b9256a4fbfe2a2b470604723acd54b0f54c8539f750b059e8
BLAKE2b-256 checksum
How to use checksums
bfb6ecf50c5545dd2c93e9c32c78b1bfb720c8f6aad5ee64927dfcef48afa22c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Release files / model_tokenizer-0.1.0-py3-none-any.whl

Download URL model_tokenizer-0.1.0-py3-none-any.whl
Size 9.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0a3e17f8753dcf521c638d7b1896ed35c4485d0881ace62a5ccd7a63fc214053
BLAKE2b-256 checksum
How to use checksums
7de8461737397cc43696a04cbba6423183a73d721c6700553357e6b1345602e2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

Security for model-tokenizer 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for model_tokenizer-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page