Skip to main content

PyTokenCalc

Know your LLM costs before you hit send.

Stop guessing tokens. PyTokenCalc counts tokens across OpenAI, Anthropic, Google, Cohere, Azure OpenAI, HuggingFace/open-source models, Ollama, and custom endpoints, then estimates the dollar cost of a request from a maintained per-model pricing table.

PyPI Python 3.9+ Tests Passing License: Proprietary


30-Second Start

from pytokencalc import count_tokens, estimate_cost

# Count tokens instantly (runs locally via tiktoken -- no API key needed)
tokens = count_tokens("Tell me a story about a robot", model="gpt-4o")
print(f"Tokens: {tokens}")

# Estimate cost from the pricing table
cost = estimate_cost("gpt-4o", input_tokens=tokens)
print(f"Cost: ${cost:.6f}")

Why PyTokenCalc?

The Problem:

  • LLM costs are unpredictable (different models, different tokenizers)
  • Manual calculation is error-prone
  • No way to estimate before sending requests
  • Each provider has different pricing

The Solution:

  • One API across cloud, local, and custom providers
  • Token counting that matches each provider's own tokenizer
  • Real cost estimation from a maintained pricing table (see docs/MODELS.md for exactly which models are covered)

Key Features

  • Multi-provider: OpenAI, Anthropic Claude, Google Gemini, Cohere, Azure OpenAI, HuggingFace/open-source models, Ollama, and any custom HTTP endpoint you register
  • Accurate tokenization: uses each provider's own tokenizer/API (tiktoken for OpenAI/Azure, the HuggingFace transformers tokenizer, the live count-tokens endpoint for Anthropic/Google/Cohere)
  • Real cost estimation: estimate_cost() is backed by a per-model USD pricing table (input vs. output rates), not a guess -- see pytokencalc/pricing.py for sources and the last-updated date
  • Fast local counting: sub-millisecond in our testing for the local (tiktoken/HuggingFace) providers on typical hardware -- see Performance below
  • Batch processing: count tokens for many prompts in one call
  • Custom / BYOM models: register your own provider or fine-tuned model (see CUSTOM_PROVIDERS.md)

Real-World Use Cases

The examples below use Claude/GPT-4 interchangeably for illustration. Anthropic/Google/Cohere models require the relevant package installed and an API key set (see docs/MODELS.md); OpenAI/GPT-4 models work offline out of the box.

Budget Tracking:

from pytokencalc import count_tokens, estimate_cost

prompt = "Hello"
reply_tokens_estimate = 50  # however you estimate expected output length

input_tokens = count_tokens(prompt, model="claude-3-opus")
cost = estimate_cost("claude-3-opus", input_tokens, reply_tokens_estimate)
print(f"Estimated cost: ${cost:.4f}")

Prevent Overruns:

tokens = count_tokens(prompt, model="gpt-4")
if estimate_cost("gpt-4", tokens) > 0.10:
    print("Request too expensive, rejected")

Compare Providers:

prompt = "Explain quantum computing in one paragraph."
for model in ["gpt-4o", "gpt-4-turbo", "gpt-3.5-turbo"]:
    tokens = count_tokens(prompt, model=model)
    cost = estimate_cost(model, tokens)
    print(f"{model}: {tokens} tokens, ${cost:.6f}")

Performance

Local (tiktoken/HuggingFace-backed) counting is fast -- in informal local benchmarking, a single uncached count_tokens() call against gpt-4o on a few dozen words took well under 1ms. PyTokenCalc itself is pure Python; the speed comes from tiktoken (OpenAI/Azure) and the HuggingFace tokenizers library, both of which have compiled (Rust) cores under their Python bindings. There is no compiled/Rust code in PyTokenCalc itself. Anthropic, Google, and Cohere counting makes a live network call to the provider's API, so latency there is dominated by that round trip (with response caching to avoid repeat calls for identical input).


Installation

pip install pytokencalc
# or with uv
uv pip install pytokencalc

# with local tokenizer support (tiktoken + HuggingFace transformers)
pip install "pytokencalc[tokenizers]"

Requires Python 3.9+.


Documentation


License

Proprietary License - Free to use with explicit attribution. See LICENSE.


PyTokenCalc v1.1.0 | Python 3.9+

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytokencalc-1.1.0.tar.gz (62.0 kB view details)

Uploaded Source

Built Distribution

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

pytokencalc-1.1.0-py3-none-any.whl (58.3 kB view details)

Uploaded Python 3

File details

Details for the file pytokencalc-1.1.0.tar.gz.

File metadata

  • Download URL: pytokencalc-1.1.0.tar.gz
  • Upload date:
  • Size: 62.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for pytokencalc-1.1.0.tar.gz
Algorithm Hash digest
SHA256 48c832140e6fda43327e3ed62956c7c19a68f34a6a6ce82a4775018699e95d94
MD5 00d40bb5d2de2ee1005091cf58090249
BLAKE2b-256 90a4aa01c6639cb0db889cfe3bbe833fc1695832544d55e8acf5f7b8ed52e3ac

See more details on using hashes here.

File details

Details for the file pytokencalc-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: pytokencalc-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 58.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.11.15

File hashes

Hashes for pytokencalc-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 122151a7357d9a8a56207e2eb618224d1ead0c04936648f59ef92be66c22197b
MD5 173f65841847fb4587cc1739db7378df
BLAKE2b-256 28427efa751d58fb60bc6a83a6d57baa9dd1f21fcff1a1a85de62ef6c8c8d048

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.0

2 files

This release

1.1.0 This release

2 files

1.0.3

1 file

1.0.1

1 file

Supported by

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