Token-budget estimation measurement for Swarmauri using tiktoken encodings and prompt-length preflight checks.
Project description
Swarmauri Measurement Token Count Estimator
swarmauri_measurement_tokencountestimator is the Swarmauri token-budget
measurement for estimating text length with OpenAI's
tiktoken. It helps you approximate prompt
size before sending text into LLM, embedding, or chat-completion workflows.
Why Use Swarmauri Measurement Token Count Estimator
- Estimate prompt size before dispatching expensive model calls.
- Compare token usage across multiple
tiktokenencodings. - Reuse a standard Swarmauri
MeasurementBasecomponent in observability, cost-control, and prompt-construction flows. - Add quick token-budget checks to preprocessing or routing pipelines.
FAQ
What does this measurement return?
An integer token count, orNonewhen the requested encoding is invalid.
What encoding does it use by default?
cl100k_base.
Can I use another tokenizer?
Yes. Pass a differentencodingname supported bytiktoken.
Does it raise on unknown encodings?
No. The current implementation prints the error and returnsNone.
Features
- Token-count estimation backed by
tiktoken. - Configurable encoding name for different model families.
- Simple Swarmauri measurement component for cost and context-window checks.
- Useful for prompt budgeting, chunk sizing, and preflight validation.
- Supports Python 3.10, 3.11, 3.12, 3.13, and 3.14.
Installation
uv add swarmauri_measurement_tokencountestimator
pip install swarmauri_measurement_tokencountestimator
Usage
from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement
measurement = TokenCountEstimatorMeasurement()
text = "Lorem ipsum odor amet, consectetuer adipiscing elit."
count = measurement.calculate(text)
print(count)
Examples
Compare encodings for the same text
from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement
text = "Swarmauri agents coordinate over shared memory."
measurement = TokenCountEstimatorMeasurement()
for encoding in ["cl100k_base", "o200k_base", "p50k_base"]:
print(encoding, measurement.calculate(text, encoding=encoding))
Preflight a prompt budget
from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement
measurement = TokenCountEstimatorMeasurement()
prompt = "Summarize the following customer support conversation..."
token_count = measurement.calculate(prompt)
if token_count is not None and token_count > 4000:
print("Prompt should be chunked before dispatch.")
Handle unsupported encodings
from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement
measurement = TokenCountEstimatorMeasurement()
print(measurement.calculate("Hello", encoding="not-real"))
Related Packages
- swarmauri_measurement_mutualinformation
- swarmauri_metric_hamming
- swarmauri_measurement_tokencountestimator
Swarmauri Foundations
More Documentation
Best Practices
- Pin
tiktokenif you need stable counts across builds and CI runs. - Measure fully assembled prompts, not partial templates, when enforcing hard token budgets.
- Normalize whitespace consistently because tokenizers are sensitive to exact byte sequences.
- Treat
Noneas an invalid-encoding signal and route it through explicit fallback logic.
License
This project is licensed under the Apache-2.0 License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file swarmauri_measurement_tokencountestimator-0.11.0.dev1.tar.gz.
File metadata
- Download URL: swarmauri_measurement_tokencountestimator-0.11.0.dev1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b5381939a3abe448b895ab9e00afe5fa3f7ee0ed0de4086ff3180b7dca035361
|
|
| MD5 |
77e1542a46ebd744a0235dc615784302
|
|
| BLAKE2b-256 |
f24f1206209e8cad70dbf52db0160e8a76184b3712ab6e2334873b322abd5e9b
|
File details
Details for the file swarmauri_measurement_tokencountestimator-0.11.0.dev1-py3-none-any.whl.
File metadata
- Download URL: swarmauri_measurement_tokencountestimator-0.11.0.dev1-py3-none-any.whl
- Upload date:
- Size: 9.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.26 {"installer":{"name":"uv","version":"0.11.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cac1d9b08d4e302279718228cce8bc7848948a5644d6c1e28b8b51d57bc41964
|
|
| MD5 |
c1d93c6932b308193110eb9621663c27
|
|
| BLAKE2b-256 |
b2faea4691db4a2a24640eaccb9835b80a0a1e129485a81d5fc74fbd4587edae
|