Skip to main content

This repository includes an example of a First Class Swarmauri Example.

Project description

Swarmauri Logo

PyPI - Downloads Hits PyPI - Python Version PyPI - License PyPI - swarmauri_measurement_tokencountestimator


Swarmauri Measurement Token Count Estimator

Token-count measurement plugin for Swarmauri pipelines. Uses OpenAI's tiktoken library to estimate how many tokens a piece of text will consume given a specific tokenizer (default cl100k_base).

Features

  • Implements the MeasurementBase API to slot into Swarmauri observability flows.
  • Wraps tiktoken encoders for quick token-count estimation ahead of LLM calls.
  • Configurable tokenizer name so you can match counts to different model families.

Prerequisites

  • Python 3.10 or newer.
  • tiktoken installed (pulled in automatically as a dependency).
  • If you use custom encodings, ensure they are registered with tiktoken before invoking the measurement.

Installation

# pip
pip install swarmauri_measurement_tokencountestimator

# poetry
poetry add swarmauri_measurement_tokencountestimator

# uv (pyproject-based projects)
uv add swarmauri_measurement_tokencountestimator

Quickstart

from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement

measurement = TokenCountEstimatorMeasurement()
text = "Lorem ipsum odor amet, consectetuer adipiscing elit."
count = measurement.calculate(text)
print(f"Tokens (cl100k_base): {count}")

Switching Encodings

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))

Use this to check token budgets across different model families before dispatching a request.

Handling Unknown Encodings

from swarmauri_measurement_tokencountestimator import TokenCountEstimatorMeasurement

measurement = TokenCountEstimatorMeasurement()
invalid_count = measurement.calculate("Hello", encoding="not-real")
print(invalid_count)  # Returns None and prints an error message

Wrap the call if you prefer structured error handling:

try:
    count = measurement.calculate("Hello", encoding="not-real")
    if count is None:
        raise ValueError("Unsupported encoding")
except ValueError:
    # fallback logic
    pass

Tips

  • Token counts can change as tokenizers evolve; pin tiktoken to a known version for stable measurements.
  • Normalize whitespace if your prompt assembly adds or strips spaces—tokenizers are sensitive to exact byte sequences.
  • For batch estimation, combine this measurement with Pandas or list comprehensions to preprocess entire prompt sets before sending them to an LLM.

Want to help?

If you want to contribute to swarmauri-sdk, read up on our guidelines for contributing that will help you get started.

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

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file swarmauri_measurement_tokencountestimator-0.9.3.dev22.tar.gz.

File metadata

  • Download URL: swarmauri_measurement_tokencountestimator-0.9.3.dev22.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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

Hashes for swarmauri_measurement_tokencountestimator-0.9.3.dev22.tar.gz
Algorithm Hash digest
SHA256 03f460e2459b2163321a8747dcbd8a7ab1a2b61b4abdc7da6175d44814d62e84
MD5 c1a1cb7b79093ebd14d461583cd9c2e7
BLAKE2b-256 0697d3c684be41595688a415eaa5788de7c99c47091250e8b101322a8127d2fb

See more details on using hashes here.

File details

Details for the file swarmauri_measurement_tokencountestimator-0.9.3.dev22-py3-none-any.whl.

File metadata

  • Download URL: swarmauri_measurement_tokencountestimator-0.9.3.dev22-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.12 {"installer":{"name":"uv","version":"0.10.12","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

Hashes for swarmauri_measurement_tokencountestimator-0.9.3.dev22-py3-none-any.whl
Algorithm Hash digest
SHA256 7e5f5f16e2b84b6e7212e8d8ee5287457e713c187347bd3823f9b853c7599fce
MD5 dc7debbc1d46c33138389f6ecb0f94dd
BLAKE2b-256 f8edb194cf32a0784ea2ca3c7bf34f1774abb0c4d8616e23ed9670ab6c145776

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page